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
Related
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 &');
I have to start a NodeJS server from my script in PHP without waiting for it. To get started the NodeJS server I use.
$command = "cd /d " . $row["pathServer"] . " && node server";
exec($command, $output);
but it doesn't work because the webserver waits until the command is finished. How can I separate the two processes?
Just redirect the stdout of command to a file, if you want to see it later. But if you don't want it, just redirect to /dev/null
$command = "cd /d " . $row["pathServer"] . " && node server > /dev/null &";
exec($command);
I am on windows 7, using a WampServer, and trying to use FFMPEG.
The encoding works, but I can't get a process id back from either the exec()
method nor the shell_exec() methods.
This is my code :
$cmd = C:\ffmpeg\bin\ffmpeg.exe -i "C:\...\4ch.wav" -ar 44100 -ab 48000 -f mp3 -y "C:\...\enc_4ch.mp3"
This what I was trying to do with shell_exec :
shell_exec("nohup $cmd > /dev/null & echo $!");
And with exec :
exec("nohup " . $this->_command . " > /dev/null 2>/dev/null &") ; // returns null
exec("nohup " . $this->_command . " > /dev/null 2>&1 &"); // also returns null
Please let me know what I do wrong, as I'd like later to use the following method to check if my process is still running :
private function is_process_running($proccess_id)
{
exec("ps $proccess_id", $process_state);
return (count($process_state) >= 2);
}
thank you in advace
You are using echo $! to get the process ID, and that specific command is not available on Windows as it is a unix shell command. The procedure link should work: How to get PID from PHP function exec() in Windows?
I think there are tons of similar posts but I haven't yet found a solution after searching around.
Basically, I'm trying to run two scripts in the background. When I run them in the commandline, I see after calling my first script:
/usr/bin/nohup php script.php > nohupoutput.log & echo $!
I've tried ...script.php > /dev/null & with the same result. I get:
/usr/bin/nohup: ignoring input and redirecting stderr to stdout
which I ignore and run the second one. I noticed that it seemed to be hanging there, and pressing Enter brought me back to machine:~folder>
/usr/bin/nohup php script2.php > nohupoutput.log & echo $!
Both scripts work. I tried to then convert this to a shell_exec command and nothing seems to work. I suspect that the ignoring input bit is causing difficulties, but I'm not sure. Regardless, the following does not work. It just hangs in the browser:
$output = shell_exec('/usr/bin/nohup php script.php > /dev/null &');
$output = shell_exec('/usr/bin/nohup php script2.php > /dev/null &');
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 &');
This shoul work:
shell_exec('nohup /usr/bin/php path/to/script.php > output.txt &');
<?php
function execInBackground($cmd) {
if (substr(php_uname(), 0, 7) == "Windows"){
pclose(popen("start /B ". $cmd, "r"));
}
else {
exec($cmd . " > /dev/null &");
}
}
// take note: to get your PHP_PATH, try looking at your phpinfo :)
echo execInBackground("/usr/local/php53/bin/php 'example2.php'");
?>
First put your php command in a shell file script, e.g. myscript.sh:
#!/bin/bash
# myscript.sh file
php script.php
Run nohup with myscript.sh:
sudo nohup ./myscript.sh &
Verify with ps:
ps aux | grep myscript.sh
I have a command I want to run, but I do not want PHP to sit and wait for the result.
<?php
echo "Starting Script";
exec('run_baby_run');
echo "Thanks, Script is running in background";
?>
Is it possible to have PHP not wait for the result.. i.e. just kick it off and move along to the next command.
I cant find anything, and not sure its even possible. The best I could find was someone making a CRON job to start in a minute.
From the documentation:
In order to execute a command and have it not hang your PHP script while
it runs, the program you run must not output back to PHP. To do this,
redirect both stdout and stderr to /dev/null, then background it.
> /dev/null 2>&1 &
In order to execute a command and have
it spawned off as another process that
is not dependent on the Apache thread
to keep running (will not die if
somebody cancels the page) run this:
exec('bash -c "exec nohup setsid your_command > /dev/null 2>&1 &"');
You can run the command in the background by adding a & at the end of it as:
exec('run_baby_run &');
But doing this alone will hang your script because:
If a program is started with exec function, in order for it to continue running in the background, the output of the program must be redirected to a file or another output stream. Failing to do so will cause PHP to hang until the execution of the program ends.
So you can redirect the stdout of the command to a file, if you want to see it later or to /dev/null if you want to discard it as:
exec('run_baby_run > /dev/null &');
This uses wget to notify a URL of something without waiting.
$command = 'wget -qO- http://test.com/data=data';
exec('nohup ' . $command . ' >> /dev/null 2>&1 & echo $!', $pid);
This uses ls to update a log without waiting.
$command = 'ls -la > content.log';
exec('nohup ' . $command . ' >> /dev/null 2>&1 & echo $!', $pid);
I know this question has been answered but the answers i found here didn't work for my scenario ( or for Windows ).
I am using windows 10 laptop with PHP 7.2 in Xampp v3.2.4.
$command = 'php Cron.php send_email "'. $id .'"';
if ( substr(php_uname(), 0, 7) == "Windows" )
{
//windows
pclose(popen("start /B " . $command . " 1> temp/update_log 2>&1 &", "r"));
}
else
{
//linux
shell_exec( $command . " > /dev/null 2>&1 &" );
}
This worked perfectly for me.
I hope it will help someone with windows. Cheers.
There are two possible ways to implement it.
The easiest way is direct result to dev/null
exec("run_baby_run > /dev/null 2>&1 &");
But in case you have any other operations to be performed you may consider ignore_user_abort
In this case the script will be running even after you close connection.
"exec nohup setsid your_command"
the nohup allows your_command to continue even though the process that launched may terminate first. If it does, the the SIGNUP signal will be sent to your_command causing it to terminate (unless it catches that signal and ignores it).
On Windows, you may use the COM object:
if(class_exists('COM')) {
$shell = new COM('WScript.Shell');
$shell->Run($cmd, 1, false);
}
else {
exec('nohup ' . $cmd . ' 2>&1 &');
}