echo exec("sudo top -n1 | awk '/30100/ {print $9}'"); not printing anything - php

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");

Related

System() function of php not working

$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

How to find out the process or script which call php bash script

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.

Bash how to get full script name from PID

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;
}

ffmpeg php exec not working

I have ffmpeg installed
safe_mode is off
when i do this: $movie = new ffmpeg_movie('Bear.wmv');
I can use getDuration(), getFilename().... wthout any problems, so it's all seems to be working
exec is working fine, cos when i do: $output = exec('ls -lart'); I get a nice little result.
but when I do this:
exec('ffmpeg -i Bear.wmv outputfile.flv')
nothing happens
if I add: $command_output, $result
the only result i guess is: array { }
I have tried everything I could think of:
exec('ffmpeg -i Bear.wmv outputfile.flv')
exec('ffmpeg.so -i Bear.wmv outputfile.flv')
exec('/usr/lib/php5/20090626/ffmpeg -i Bear.wmv outputfile.flv')
I've tried all sizes and folders and codecs but still don't get anything back
All i wanna do is convert that video, Bear.wmv to an flv file.
I'm very close to crying like a baby and/or jumping out of the window (im only on the first floor but still lol)
so Please help!!??!
FFMPEG is a application wich don't output to STDIO but STDERR, so you can redirect it to standard output:
$cmd = $FFMPEGDIR . " -i somefile.avi 2>&1"; // SEE 2>&1 !!
Extracting size:
exec( $cmd , $info );
echo "<pre>".print_r($info,true)."</pre>";
$resolution = preg_match( '#[ ,\t]([0-9]{3,4}x[0-9]{3,4})[ ,\t]#si' , implode( " " , $info ) , $durmatches );
$rtab = explode( "x" , $durmatches[1] );
$videowidth = $rtab[0];
$videoheight = $rtab[1];
Recently set ffmpeg up for audio stuff... it's a bit of a black art, ffmpeg is notorious for not playing nice (or consistently) - what works (worked) for me might not work for you!
try using: shell_exec()
or:
$command="{$FFMPEG_BINARY} ... rest of your options";
$process=proc_open($command, $descriptors, $pipes);
if (!$process)
{
// failed to exec...
}
else
{
// command ran...
}
my ffmpeg was in... "/usr/bin/ffmpeg" just check you've got right path too.

Getting script exit status code in php

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

Categories