Php exec() function is not working on heroku with command nice - php

I am using the following lines of php code in my heroku php app, but these lines are not working :
$exec = "ps -p pid | grep pid.php";
$cmd='nice -n15 ' . php path . ' external.php ';
$cmd.=" > /dev/null 2>/dev/null & echo $!";
$out=array();
exec($cmd, $out);
Could you please suggest any other alternative of this or what mistake this code have?

Related

Cannot kill a process by a PHP Script running behind nohup

I have a php script that perform some actions after killing some old processes.
act.php
$pids = shell_exec('ps aux | grep "saso" | awk \'{print $2}\'');
$pids = str_replace("\n", ' ', $pids);
$pids = array_filter(explode(' ', $pids));
foreach ($pids as $pid) {
shell_exec('kill -9 ' . $pid . ' > /dev/null 2>&1 &');
}
// reset of the code . ..
The script works well by running php act.php. It fetch process ids, kill it, then run the rest.
But it is not working when I run nohup php act.php & or nohup php act.php. The process is not killed.
I need nohup to run the script in the background with no hang up.
Can't PHP script fetch pids behind nohup ? and are there any alternatives ?
Thanks in advance.
If you search properly, you can find the result.
Try:
$output = shell_exec('/usr/bin/nohup php script.php >/dev/null 2>&1 &');
Or:
exec('/usr/bin/nohup php script.php >/dev/null 2>&1 &');

Open MATLAB script with PHP

I need to run a MATLAB script from PHP file.
This is what I tried, which doesn't work:
$cmd = '/Applications/MATLAB_R2016b.app/bin/matlab -nodisplay -nosplash - nodesktop -r "run('/Applications/MAMP/htdocs/files/appResponse.m')" ';
shell_exec($cmd);
exit;
But this code works:
$cmd = '/Applications/MATLAB_R2016b.app/bin/matlab -nodisplay -nosplash - nodesktop -r ';
shell_exec($cmd);
exit;
I couldn't figure out how to fix the first command to run the .m script file. Any suggestions?
You need to properly escape the quotes. Notice the backslashes:
$cmd = '/Applications/MATLAB_R2016b.app/bin/matlab -nodisplay -nosplash - nodesktop -r "run(\'/Applications/MAMP/htdocs/files/appResponse.m\')"';
shell_exec($cmd);
exit;
Now the $cmd variable contains a valid path to the Matlab executable, with proper arguments as far as PHP is concerned:
/Applications/MATLAB_R2016b.app/bin/matlab -nodisplay -nosplash - nodesktop -r "run('/Applications/MAMP/htdocs/files/appResponse.m')"

exec() is not working

I am doing the conversion from youtube link to gif image ,but i faced some problem while executing exce() function.
echo $ret = exec("youtube-to-gif -u https://www.youtube.com/watch?v={$vidID} -b $start_second -d $different_second -s 480x? -o {$filePath}{$fileName} -f 10", $out, $err);
I am using exec() ,but its not returning any value .Here i am not getting why it is not working.
Thanks ,any suggestion will highly appreciate.
First, store your command in a variable and try echoing it and runnining in a terminal to see if it's valid at all:
$command = "youtube-to-gif -u https://www.youtube.com/watch?v={$vidID} -b $start_second -d $different_second -s 480x? -o {$filePath}{$fileName} -f 10";
echo $command . PHP_EOL;
echo $ret = exec($command, $out, $err) . PHP_EOL;
If it works fine when you run it manually, try full path to youtube-to-gif. Assuming you are running php on Linux, you should be able to do it with this command:
which youtube-to-gif
Now replace youtube-to-gif with the full path in $command.

Convert a Unix-based PHP script into a batch file for Windows

I want to convert this PHP script which works great on a Unix system into a script for Windows.
I don't know how to rewrite it. Can someone help me?
Here is the script:
$cmd = 'nohup sudo -u '.$user.' bash -c "cd ' .
escapeshellarg($path) . '; VVERBOSE=true QUEUE=' .
escapeshellarg($queue) . ' APP_INCLUDE=' .
escapeshellarg($bootstrap_path) . ' INTERVAL=' .
escapeshellarg($interval) . ' CAKE=' .
escapeshellarg(CAKE) . ' COUNT=' . $count .
' php ./resque.php';
$cmd .= ' > '. escapeshellarg($log_path).' 2>&1" >/dev/null 2>&1 &';
passthru($cmd);
passthru is a method in PHP see http://php.net/manual/en/function.passthru.php
Do you know what the script does? Maybe reverse engineer it into windows batch script?
From the looks of it, the script came from a *nix system it:
execute a command as another user
change the current directory
set environment variables
calls a PHP script directly from the PHP interpreter and dumps the log into a file, and pipes the stderrs to /dev/null
That script is imo, is impossible to "covert" to Windows batch script (since Windows != POSIX), you need to rewrite it.

PHP: get PID of a specific process

I have a QNAP box, that runs a flavor of linux and I am having problems getting the PID of a process using a php script. What I have so far:
$command = "PATH=$PATH:/share/MD0_DATA/.qpkg/Optware/bin: nohup /opt/bin/plowdown -o /share/MD0_DATA/Qdownload/plowshare http://www.megaupload.com/?d=m7duotr1 2> /share/MD0_DATA/Qdownload/plowshare/outputeeds.txt > /dev/null &";
exec($command, $out);
$result = $out[0];
echo $result;
If I run the command through PUTTY, I get:
[~] # nohup /opt/bin/plowdown -o /share/MD0_DATA/Qdownload/plowshare http://www.megaupload.com/?d=m7duotr1 2> /share/MD0_DATA/Qdownload/plowshare/outputteeds.txt > /dev/null &
22526
What am I doing wrong?
Thanks,
Cristian.
The shell does not normally print the PID of a process it starts in background, unless it's interactive. Otherwise, you would get tons of output during bootup just from the PIDs of all the processes that get started.
So you need to make the shell print the PID. Do
exec("(PATH=$PATH:/share/MD0_DATA/.qpkg/Optware/bin: " .
"nohup /opt/bin/plowdown -o /share/MD0_DATA/Qdownload/plowshare " .
"http://www.megaupload.com/?d=m7duotr1 2> " .
"/share/MD0_DATA/Qdownload/plowshare/outputeeds.txt > /dev/null &);" .
"echo $$;", $out);
http://nl2.php.net/manual/en/function.getmypid.php

Categories