I have main php script on web-server nginx+php-fpm and try to run another php script in background using GET request from web-browser. Line code in main.php to call detect.php:
exec("/usr/bin/php -f /var/www/detect.php 6 > /dev/null 2>&1 &");
detect.php does not start. I don't have any errors.
If to remove "&":
exec("/usr/bin/php -f /var/www/detect.php 6 > /dev/null 2>&1 ");
detect.php starts successfully.
From shell bash with "&" :
sudo -u www-data /usr/bin/php -f /var/www/detect.php 6 > /dev/null 2>&1 &
Script detect.php starts successfully.
try this and make sure your php path are correct
$dir=dirname(__FILE__);
$file_name="detect.php";
$php_path="/usr/bin/php";
$args = array(6);
$command1="cd $dir";
$command2="$php_path $file_name ".implode(" ",$args) ." > /dev/null 2>/dev/null &";
$final_command=$command1."; ".$command2;
shell_exec($final_command);
Related
I need to execute in one command two php files.
the second file need to run right after the first finish.
This is what i did, not sure if it's ok:
/usr/bin/wget -O /dev/null -o /dev/null https://example.com/scripts/cron.php; sleep 2; wget -q -O - https://example.com/cron2.php > /dev/null 2>&1
I added sleep between the commands, it will work?
You can use && for sequential execution of command,
check https://www.steveroot.co.uk/2010/07/05/cron-multiple/ And What is the purpose of "&&" in a shell command?
In Your case You can try :
01 00 * * * //usr/bin/wget -O /dev/null -o /dev/null https://example.com/scripts/cron.php && wget -q -O - https://example.com/cron2.php > /dev/null 2>&1
Hope these will Help.
I am trying to execute a command with exec() and redirecting stdout and stderr to a file.
exec("nohup python main.py -i 1 > /var/scripts/logs/1_out.log 2>&1 &");
It will create the file but it will not print anything to it.
If I run the command in a terminal everything outputs without a problem.
Got it working. Python does its own output buffering which kept it from writing to the file. Running it with the -u option disables this. Final code looks like this:
exec("nohup python -u main.py -i 1 > /var/scripts/logs/1_out.log 2>&1 </dev/null &");
Thanks.
I'm trying to run in background two bash command using exec function.
$action[] = "/path/script1 par1 > log1 2>&1";
...
$action[] = "/path/script2 par2 > log2 2>&1";
...
$cmd = "( " .implode(' && ',$action). " ) &";
exec($cmd);
script1 and script2 are bash scripts.
Scripts are properly executed and their output is correctly saved in their log files, but my application hangs. It seems that last & doesn't work.
I have already tried:
$cmd = "bash -c \"" .implode(' && ',$action). "\" &";
If I run a single command, it works in background.
I captured $cmd and if I run:
( /path/script1 par1 > log1 2>&1 && /path/script2 par2 > log2 2>&1 ) &
from command line, it works in background.
I'm using Apache/2.0.52 and PHP/5.2.0 on Red Hat Enterprise Linux AS release 3 (Taroon Update 2)
The answer is hidden in the PHP exec documentation:
If a program is started with this 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.
Add a redirection to the top level of your command line:
exec( "bash -c \"( ./script1 par1 > log1 2>&1 && ./script2 par2 > log2 2>&1 ) \" >/dev/null 2>&1 & " );
and without bash -c:
exec( "( ./script1 par1 > log1 2>&1 && ./script2 par2 > log2 2>&1 ) >/dev/null 2>&1 & " );
Tested with PHP 5.3.3-7 (command line invocation): Program hangs before adding the redirect operators, and terminates afterwards.
I have a long running PHP script that i want to be executed in background on server after a user action. and the user should be redirected to other page while command should be running in background.
Below is the code
$command = exec('php -q /mylongrunningscript.php');
header("Location: /main.php?action=welcome");
The above script is running fine, but page does not redirected until $command = exec('php -q /mylongrunningscript.php'); is executed.
I want that user should be immediately redirected to the welcome page.
Is there any other way to achieve this task.
The other idea is that that $command = exec('php -q /mylongrunningscript.php'); should be executed on welcome page, but welcome page HTML is shown after the command has executed. command takes about 5,6 minutes and this time page does not redirects.
I am On Cent os Linux with PHP 5.3
Can you try this instead:
$result = shell_exec('php -q /mylongrunningscript.php > /dev/null 2>&1 &');
PS: Note that this is redirecting stdout and stderr to /dev/null If you want to capture output then use:
$result = shell_exec('php -q /mylongrunningscript.php > /tmp/script.our 2>&1 &');
Alternatively use this PHP function to run any Unix command in background:
//Run linux command in background and return the PID created by the OS
function run_in_background($Command, $Priority = 0) {
if($Priority)
$PID = shell_exec("nohup nice -n $Priority $Command > /dev/null & echo $!");
else
$PID = shell_exec("nohup $Command > /dev/null & echo $!");
return($PID);
}
Courtesy: A comment posted on http://php.net/manual/en/function.shell-exec.php
As noted in the exec() manual page of PHP:
If a program is started with this 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 let's do that, using 2>&1 (basically 2 is stderr and 1 is stdout, so what this means is "redirect all stderr messages to stdout"):
shell_exec('php -q /mylongrunningscript.php 2>&1');
or if you want to know what it outputs:
shell_exec('php -q /mylongrunningscript.php 2>&1 > output.log');
Send the script output to /dev/null and the exec function will return immediately
$command = exec('php -q /mylongrunningscript.php > /dev/null 2>&1');
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