I have this php function that checks the script's name from the given PID, and compares it to itself.
function isRunning($pid) {
$filename = exec('ps -p '.$pid.' -o "%c"');
$self = basename($_SERVER['SCRIPT_NAME']);
return ($filename == $self) ? TRUE : FALSE;
}
From what I know, I usually use this command to get the script name from the PID:
ps -o PID -o "%c"
It returns me the filename, but only the first 15 characters.
Since my script's name is
daily_system_check.php
the function always returns FALSE, because it's comparing itself with
daily_system_ch
Is there another bash command for Centos 6 that will return me script's full name?
You didn't specify what is your OS, but in Ubuntu Linux I can see full name of the script with adding --context to the ps call:
# ps -p 17165 --context
PID CONTEXT COMMAND
17165 unconfined /bin/bash ./testing_long_script_name.sh
#
read the the proc cmdline file:
cat /proc/$pid/cmdline | awk 'BEGIN {FS="\0"} {print $2}'
There seems to be no flag or collumn in "ps" command to show the whole filename without the filepath or it being cutoff. PHP's basename() gets the job done.
function isRunning($pid) {
$filename = basename(exec('ps -o cmd= '.$pid));
$self = basename($_SERVER['SCRIPT_NAME']);
return ($filename == $self) ? TRUE : FALSE;
}
Related
The title is pretty self explanatory. I'm trying to get the CPU usage of a process by PID in PHP so I can display it in a webpage. The code works perfectly when written in the terminal, but doesn't print anything when done via PHP.
Tried
$cmd = "sudo top -n1 | awk '/30100/ {print $9}'";
echo exec($cmd);
where 30100 is the pid
and
$cmd = "sudo sh -c \"top -n1 | awk '/30100/ {print $9}'\" ";
echo exec($cmd);
I've also tried to $var = exec() and then var_dump($var) , the result was string(0)
I'd like to add that I'm using other commands on the system similar to this and they work fine. An example would be
$cmd = 'sudo -u server' . $sid . ' sh -c "pidof hlds_i686"';
$pid = exec($cmd);
which returns process pid
It might be an sderr / stdout issue , try using
$cmd = "sudo sh -c \"top -n1 | awk '/30100/ {print $9}'\" ";
echo shell_exec($cmd." 2>&1");
$output = system('SET PATH=%PATH%;C:\xampp\GCC G++ compiler\bin && cd/ && c: && cd xampp\htdocs\College Coding\lessons\HTML && g++ -o temp temp.cpp && temp.exe > output.txt',$ret);
temp.cpp is not getting compiled in my machine. I tried on another and it is working.
it is not creating output.txt
What the output.txt should contain ? Are you trying to get the command output in $output var ?
You can use passthru
passthru("your_command", &$output);
Then $output will contain all outputs of your commands
Anas
I have php bash script that do some database processing. All I need to know is the caller of this script. I don't know if it's process or other script. So is there some way to know process id or script name of caller?
Script is running by some process and its code starts with interpreter path "#!/usr/bin/php". This file called as bash script only.
OS: Centos 6.5
You can try something like this
<?php
Check for a current process by filename
function processExists($file = false) {
$exists = false;
$file = $file ? $file : __FILE__;
// Check if file is in process list
exec("ps -C $file -o pid=", $pids);
if (count($pids) > 1) {
$exists = true;
}
return $exists;
}
?>
This will check against the filename that you're trying to track. If you need the process id, just adjust what's in in the exec or return the $pids.
try this ---
$mystring = "script_running";
exec("ps aux | grep \"${mystring}\" | grep -v grep | awk '{ print $2 }' | head -1", $out);
print "The PID is: " . $out[0];
ps aux is more descriptive of what's running.
Here is part of the code that I have in php file:
function checkSetStatus($checkSet) {
//other if options here
if ($checkSet == '2') {
exec('echo 0 > ~/cctv_config/cctvstatus.log');
sleep(1);
exec('echo "$(date) | setOff" >> weblog.log');
return (int) exec('cat ~/cctv_config/cctvstatus');
}
}
Problem is with line:
exec('echo 0 > ~/cctv_config/cctvstatus.log');
When I trigger command
echo 0 > ~/cctv_config/cctvstatus.log
in linux commandline, it works fine. However if it is triggered indirectly by exec function in php file it does not make any changes in targeted file.
In apache error log file there is infomation that
file or directory ~/cctv_config/cctvstatus.log can not be created
(I already changed mode of cctvstatus.log to 777). Similar information is logged as a result of triggering this line:
exec('cat ~/cctv_config/cctvstatus');
In this case it is logged that such file or directory does not exist (There is one more issue - in exec with cat I am wondering if rather passthru function should not be used).
The home directory of the user of the PHP instance is not the same as the user you are testing the command with.
You are reffering to the home directory by using the ~ (tilde) before the path.
Change the path to the full path instead:
function checkSetStatus($checkSet) {
//other if options here
if ($checkSet == '2') {
exec('echo 0 > /home/myuser/cctv_config/cctvstatus.log');
sleep(1);
exec('echo "$(date) | setOff" >> weblog.log');
return (int) exec('cat /home/myuser/cctv_config/cctvstatus');
}
}
Is there anyway to get the exit status code for a php script run in the background via exec($cmd, $output, $exitCode)?
For example:
exec('php script.php &', $output, $exitCode);
If the trailing '&' isn't included then $exitCode works as expected, it's always 0 otherwise.
For anybody that finds themselves here, my solution was to make a php script that takes a script as an argument. The new script is called to the background and handles exit statuses appropriately.
For example:
$cmd = 'php asynchronous_script.php -p 1';
exec("php script_executor.php -c'$cmd' &");
The second script looks something like
$opts = getOpt('c:');
$cmd = rtrim($opts['c'], '&');
$exitCode = 0;
$output = '';
exec($cmd, $output, $exitCode);
if ($exitCode > 0) {
...
}
I found something that is probably quite equivalent to Pradeep's solution. Someone posted about this on the function's documentation page.
http://www.php.net/manual/en/function.exec.php#101506