I call a python script from PHP but i run script in background because i don't want to wait after script to finish, so i use > /dev/null 2>/dev/null & with shell_exec.
This is the code where i call:
shell_exec("C:\\Python27\\python C:\\xampp\\htdocs\\testing.py > /dev/null 2>/dev/null &");
in python script i have a simple write file:
fileName = "temp.txt"
target = open(fileName, 'w')
for x in range(1, 59):
target.write("test" + str(x) + "
")
target.close()
When i call script from PHP with shell_exec("C:\\Python27\\python C:\\xampp\\htdocs\\testing.py"); it's working but webpage it's waiting after script to finish and i don't want that, so i called with > /dev/null 2>/dev/null & but now, the code doesn't run.
Any ideas why code doesn't working when i want to run in background?
Thanks!
LE:
Changed /dev/null/ with nul and still no working.
Now i call shell_exec("C:\\Python27\\python C:\\xampp\\htdocs\\testing.py > NUL 2> NUL &"); and it's working but page still wating after script to finish.
The answer is:
pclose(popen("start /B ". $cmd, "r"));
You can start a program without cmd and PHP will not wait after program to finish.
Related
I have the following exec() command with an & sign at the end so the script runs in the background. However the script is not running in the background. It's timing out in the browser after exactly 5.6 minutes. Also if i close the browser the script doesn't keep running.
exec("/usr/local/bin/php -q /home/user/somefile.php &")
If I run the script via the command line, it does not time out. My question is how do i prevent timeout. How do i run the script in the background using exec so it's not browser dependent. What am i doing wrong and what should i look at.
exec() function handle outputs from your executed program, so I suggest you to redirect outputs to /dev/null (a virtual writable file, that automatically loose every data you write in).
Try to run :
exec("/usr/local/bin/php -q /home/gooffers/somefile.php > /dev/null 2>&1 &");
Note : 2>&1 redirects error output to standard output, and > /dev/null redirects standard output to that virtual file.
If you have still difficulties, you can create a script that just execute other scripts. exec() follows a process when it is doing a task, but releases when the task is finished. if the executed script just executes another one, the task is very quick and exec is released the same way.
Let's see an implementation. Create a exec.php that contains :
<?php
if (count($argv) == 1)
{
die('You must give a script to exec...');
}
array_shift($argv);
$cmd = '/usr/local/bin/php -q';
foreach ($argv as $arg)
{
$cmd .= " " . escapeshellarg($arg);
}
exec("{$cmd} > /dev/null 2>&1 &");
?>
Now, run the following command :
exec("/usr/local/bin/php -q exec.php /home/gooffers/somefile.php > /dev/null 2>&1 &");
If you have arguments, you can give them too :
exec("/usr/local/bin/php -q exec.php /home/gooffers/somefile.php x y z > /dev/null 2>&1 &");
You'll need to use shell_exec() instead:
shell_exec("/usr/local/bin/php -q /home/gooffers/somefile.php &");
That being said, if you have shell access, why don't you install this as a cronjob? I'm not sure why a PHP script is invoking another to run like this.
How to execute a php script from another ?
I want to execute 3 php scripts from my php file without waiting for the 3 scripts to finish. In other words, the 3 php files need to be executed all at once (parallel) instead of one-by-one (sequentiell).
The 3 scripts are in the same folder of my main php file (script).
If you do not want to wait for them to finish, run them with either
exec('php script.php &> /dev/null &');
shell_exec('php script.php &> /dev/null &');
system('php script.php &> /dev/null &');
`php script.php &> /dev/null &`
Any of those should accomplish the job, depending on your PHPs configuration. Although they are different functions, their behaviour should be similar since all output is being redirected to /dev/null and the proccess is immediately detached.
I use the first solution in a production environment where a client launches a bash SMSs sending script which can take up to 10 minutes to finish, it has never failed.
More info in: http://php.net/exec · http://php.net/shell_exec · http://php.net/system
how about using exec("php yourscript.php")
do consider using queuing system to store your php script names and worker to fetch data from queue and do the execution e.g. beanstalkd
You need to run them as detached jobs, and it is not really easy - or portable. The usual solution is to use nohup or exec the scripts with stdout and stderr redirected to /dev/null (or NUL in Windows), but this often has issues.
If possible, make the three scripts available as scripts on the web server, and access them through asynchronous cURL functions. This has also the advantage of being able to test the scripts through the browser, and supplying you the scripts output.
Other ways include using popen(), or if under Linux, the at or batch utility.
taken from http://board.phpbuilder.com/showthread.php?10351142-How-can-I-exec%28%29-in-a-non-blocking-fashion:
In order to execute a command have 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 &"');
For windows http://www.php.net/manual/en/function.exec.php:
function execInBackground($path, $exe, $args = "") {
global $conf;
if (file_exists($path . $exe)) {
chdir($path);
if (substr(php_uname(), 0, 7) == "Windows"){
pclose(popen("start \"bla\" \"" . $exe . "\" " . escapeshellarg($args), "r"));
} else {
exec("./" . $exe . " " . escapeshellarg($args) . " > /dev/null &");
}
}
To the shell_exec, add the '/B' parameter, this allows you to run several executables at once.
See my answer at this question: PHP on a windows machine; Start process in background
It's the same.
shell_exec('start /B "C:\Path\to\program.exe"); /B parameter is key
here. I tried to find the topic for you again, but I can't seem to
find it anymore. This works for me.
I hope this solves the problem for you.
I need to execute a shell script. The catch is I want to do this
$Command = "nohup cvlc input --sout '#transcode {vcodec=h264,acodec=mp3,samplerate=44100}:std{access=http,mux=ffmpeg{mux=flv},dst=0.0.0.0:8083/".output"}' &";
$str = shell_exec($Command);
I dont want it to wait till the command is finished, i want it to run in a background process.
I do not want another php thread as it will timeout the command can take up to 3 hours to finish.
$str = shell_exec($Command.' 2>&1 > out.log');
You need to redirect the output of the command.
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.
http://php.net/manual/en/function.exec.php
You can try running your command in background using a function like this one:
function exec_bg($cmd) {
if (substr(php_uname(), 0, 7) == "Windows"){
pclose(popen("start /B ". $cmd, "r"));
}
else {
exec($cmd . " > /dev/null &");
}
}
This makes your shell command runs, but the php flow continues.
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 &');
}