When I run ps ax command in console via putty I get:
1053 ?? Ss 0:45.47 /usr/local/sbin/nrpe2 -d -c /usr/local/etc/nrpe.cfg
1085 ?? Is 0:00.03 /bin/sh /usr/local/bin/mysqld_safe --defaults-extra-file=/var/db/mysql/my.cnf --user=mysql --datadir=/var/db/mysql --pid-file=/var/db/mysql/MYNAME.pid
But when I run this command via php:
exec('ps ax', $o);
print_r($o);
I get the same, but cutted!
[27] => 1053 ?? Ss 0:45.48 /usr/local/sbin/nrpe2 -d -c /usr/local/etc/nrpe.cfg
[28] => 1085 ?? Is 0:00.03 /bin/sh /usr/local/bin/mysqld_safe --defaults-extra-f
Why are all characters from position >=79 truncated?
Here's the technique we used on our script:
exec("export COLUMNS=1000; ps ax | grep $parameter", $results);
Here's what COLUMNS means:
COLUMNS
Used by the select builtin command to determine the terminal width when
printing selection lists. Automatically set upon receipt of a SIGWINCH.
Related
I have just built 3 different versions of PHP from source on an Ubuntu server (alongside NGINX and MySQL 5.7). I am looking for a way to run php --ini for the currently running version. I know I have to add the location to the file PATH in .bashrc so I don't have to add the full path.
I have added this to my .bashrc which allows me to get the currently running PHP version, which then allows me to run the command:
# parallels#ubuntu:~$ ps aux | grep php
# root 6948 0.0 0.2 153724 4620 ? Ss 16:48 0:00 php-fpm: master process (/opt/php-7.0.0/etc/php-fpm.conf)
PHP_VERSION=$(ps aux | grep -o php-[[:digit:]].[[:digit:]].[[:digit:]])
export PATH="/bin:/usr/bin:/opt/$PHP_VERSION/bin:/sbin"
It works, but I am a bash novice and I'm thinking their might be a different way to do it. Would I be correct?
PHP_VERSION=$(php -v | tail -r | tail -n 1 | cut -d " " -f 2 | cut -c 1-3)
cd /usr/local/etc/php/$PHP_VERSION/
# cd /usr/local/etc/php/7.1/
This command works while running in PHP
<?php
echo PHP_VERSION;
You can get it in bash, like
PHP_VERSION=$(php -r "echo PHP_VERSION;")
Here is all of PHP Predefined Constants
I got it to work with the following commands:
# Full version
php -v | head -n 1 | cut -d " " -f 2
# Major.Minor version
php -v | head -n 1 | cut -d " " -f 2 | cut -f1-2 -d"."
should be able to get it done with awk.
php -v | awk 'NR<=1{ print $2 }'
print the second column from the first row of input.
I have a php script that runs via a cron job.
I have an exec command in the script like so:
exec("ps -u bob -o user:20,%cpu,cmd | awk 'NR>1' | grep vlc | tr -s ' ' | cut -d ' ' -f 2",$cpu,$return)
This gets me the cpu form a process run by a specific user, if the process exists. When run via the command line I get say 21 or nothing at all depending on if the process is running or not. However, when running vai the PHP script, I get the following:
[0] => bob 0.0 /bin/sh -c php /home/bob/restart.php bob
[1] => bob 0.0 php /home/bob/restartStream.php bob
[2] => bob 0.0 sh -c ps -u bob -o user:20,%cpu,cmd | awk NR
It seems to be returning all the recent commands executed as opposed to the result of the command executed.
I have seen some posts which show the use of 2>&1 which I believe redirects the stdin and stdout or soemthing similar. However I have tried this in my command like so:
ps -u bob -o user:20,%cpu,cmd | awk 'NR>1' | grep vlc | tr -s ' ' | cut -d ' ' -f 2 2>&1
But it does not seem to make a difference. Can any give me any pointers as to why this is occurring and what can possibly be done to resolve this.
You need to clear out $cpu before you call exec. It appends the new output to the end of the array, it doesn't overwrite it.
You can also get rid of grep, tr, and cut and do all the processing of the output in awk
$cpu = array();
exec("ps -u bob -o user:20,%cpu,cmd | awk 'NR>1 && /vlc/ && !/awk/ {print $2}'",$cpu,$return);
The !/awk/ keeps it from matching the awk line, since that contains vlc.
There is an issue I couldn't find the exact problem, or is it a problem anyway I don't know.
I execute below command through shell_exec in php. I'm user I called just once.
curl -o ./server.log --request POST 'crawlserver.xxx.local/workerCallback.php' --user-agent 'xxx' --data 'workerid=worker1&jobid=25&offercount=72&file='/path/to/xxx-2014-02-07-serialized.txt'
this command working like 30 seconds.
while when I ask to bash as ps aux | grep workerCallback
I see 2 different commands triggered, and different process ids. But when look to server.log file it is looking a single request, also I check db and other stuff, the request worked single. But why it is looking twice in commandline by different pids, and commands has little differences. What is the "sh -c" before the command.
1000 27384 0.0 0.0 4404 612 ? S 14:00 0:00 sh -c curl -o ./server.log --request POST 'crawlserver.xxx.local/workerCallback.php' --user-agent 'xxx' --data 'workerid=worker1&jobid=25&offercount=72&file=path/to/xxx-2014-02-07-serialized.txt'
1000 27385 0.0 0.0 88056 3756 ? S 14:00 0:00 curl -o ./server.log --request POST crawlserver.xxx.local/workerCallback.php --user-agent xxx --data workerid=worker1&jobid=25&offercount=72&file=/path/to/xxx-2014-02-07-serialized.txt
Its not the same command twice, the first command is the shell (sh) running your curl command.
The second one is the command itself.
So your code is working fine :)
I want to run only one proccess at a time. So I need to check. I found there was suggestions to use exec()
so I made test functions - one which sleeps 1 minute and one which tests if process is running.
public function test($a='', $b='') {
exec("ps ax | grep 'php -q /var/www/glab/index.php ajax/test2'", $pids);
if (count($pids) > 2) {
$exists = true;
echo 'exists' . count($pids);
print_r($pids);
}
}
And I get result:
exists3Array
(
[0] => 30680 pts/8 S+ 0:00 php -q /var/www/glab/index.php ajax/test2
[1] => 30684 ? S 0:00 sh -c ps ax | grep 'php -q /var/www/glab/index.php ajax/test2'
[2] => 30686 ? S 0:00 grep php -q /var/www/glab/index.php ajax/test2
)
I did not expect 3 processes but I see its ok. Can I be sure that my function is working ok - detecting running when there is > 2, am I not missing something? For example maybe if some user will run some program on linux maybe this will not work anymore?
Or can you sugesst some check which matches only the one process, without sh and grep? I mean exact string. I was trying but cannot make it work to match only one which I am searching.
Edit:
googled bit more and found more examples, adjusted and have this:
exec ('ps -efa | grep "php -q /var/www/glab/index.php ajax/test2" |grep -v "grep " | awk "{print $10 $NF}"', $pids);
print_r($pids);
When process runs:
Array
(
[0] => darius 2046 12877 5 09:23 pts/8 00:00:00 php -q /var/www/glab/index.php ajax/test2
)
It matches now 1 processs. Could you check if this is ok, am I not missing something?
I would like to have a shell scipt that runs infinitely and keeps checking status of a php script (say my.php) and restarts it if the script has terminated somehow. I have the idea to go for -
ps -aux | grep "my.php"
and then use the result of this to check the status and do accordingly. Thanks in advance.
You can simply say:
ps -aux | grep -q "my.php" || php -f my.php
The way it works is that grep -q will not output anything but will return an "OK" exit code if it found something. when it returns a "NOT OK" exit code, the part after the || ("or") gets executed (because of boolean short-circuit evaluation - look it up).
You also need to make sure that:
you run the new script in the background and detach it from your console so that your script can keep monitoring
when you run ps | grep sometimes ps also lists your grep and then the grep "greps itself", so you have to filter that out.
It should look something like this:
while true
ps -aux | grep -v grep | grep -q "my.php" || ( nohup php -f "my.php" & )
sleep 1
done
or some-such..
Another approach is, start your php-program in a loop:
for ((;;))
do
my.php
done
With Linux ps, you could use
ps -C "my.php"
instead of grep, to identify my.php. Grep commands often find themselves. Maybe your ps has a similar switch?
If you DO really feel the need to grep the output of ps, beware of your grep finding itself.
[ghoti#pc ~]$ sleep 60 &
[1] 66677
[ghoti#pc ~]$ ps aux | grep sleep
ghoti 66677 0.0 0.0 3928 784 11 S 4:11PM 0:00.00 sleep 60
ghoti 66681 0.0 0.0 16440 1348 11 S+ 4:12PM 0:00.00 grep sleep
[ghoti#pc ~]$
There's an easy way to avoid this. Just make part of your grep into a more complex regular expression.
[ghoti#pc ~]$ sleep 60 &
[2] 66717
[ghoti#pc ~]$ ps aux | grep '[s]leep'
ghoti 66677 0.0 0.0 3928 784 11 S 4:11PM 0:00.00 sleep 60
ghoti 66717 0.0 0.0 3928 784 11 S 4:13PM 0:00.00 sleep 60
[ghoti#pc ~]$
On the other hand, if you just want to make sure that your PHP script always runs, you can wrap it in something that re-runs it when it dies:
while true; do
php /path/to/my.php
done
If you want this to run at startup, you can edit your crontab on the server, and use a #reboot tag, assuming you're using "Vixie" cron (common on Linux and BSD):
#reboot /path/to/wrapperscript
You can man crontab and man 5 crontab for more details on how to use cron and the #reboot tag.