I need to run a command in PHP like this:
exec('dosomething > saveit.txt');
Except I don't want PHP to wait for it to be complete. I also don't want to throw away the output, and I don't want to use nohup because I'm using that for something else in the same directory.
I also tried pclose(popen('dosomething > saveit.txt','r')); and that didn't work, it still waited.
Add an ampersand to the end of the command, so:
exec('dosomething > saveit.txt &');
in the documentation of exec() there is an interesting comment that says:
Took quite some time to figure out the line I am going to post next. If you want to execute a command in the background without having the script waiting for the result, you can do the following:
<?php
passthru("/usr/bin/php /path/to/script.php ".$argv_parameter." >> /path/to/log_file.log 2>&1 &");
?>
Related
I'm trying to run my server from PHP script as a background process, but it's hanging the PHP script anyway. I call it like this:
$exec_result = exec('./myapp option1 option2 &> /dev/null &');
I tried things from PHP hanging while exec() bash script like adding "set -m && " or "shopt -u checkjobs && " but that doesn't help. I also tried to call in exec() my C++ utility that runs command in background (basically just calls std::system with " &"), but that didn't help either. Using "nohup" doesn't change anything. Also, the problem is not in my server because same thing happens when I call "sleep" command.
Calling exactly the same command from bash runs process in background as expected. Honestly I'm so confused and frustrated. What am I doing wrong? Maybe PHP needs some kind of permissions to run a background task? I'm kinda new to Linux.
I'm doing it all from Debian 10 and PHP 7.3 if it matters.
I've managed to fix it, but I have no idea why the new solution works while the old one doesn't. Maybe it has something to do with exec() build-it parser? Both lines work identically in bash so I'm blaming PHP on this.
So, I've replaced
$exec_result = exec('./myapp option1 option2 &> /dev/null &');
with
$exec_result = exec('./myapp option1 option2 > /dev/null 2>&1 &');
and that did it. I've checked it back and forth multiple times and the second line works consistently while the first one fails every time.
I have the following code on my index.php file. But it is not working properly.. When i directly visit domain.com/script.php it works. I need this script to be executed in the background while accessing index page. Can anyone help me?
shell_exec('php script.php > /dev/null 2>/dev/null &');
check php is running in safe mode or not shell_exec is disabled in safe mode for the sake of security why don't you use
curl
to run the code
Well I think there are 2 possible issues in your case
1) try this:
shell_exec("script.php 2>/dev/null >/dev/null &");
OR
shell_exec("script.php 2>&1 | tee -a /tmp/mylog 2>/dev/null >/dev/null &");
2) A simple way to handle the problem of capturing stderr output when using shell-exec under windows is to call ob_start() before the command and ob_end_clean() afterwards
ob_start();
ob_end_clean();
Well instead of using shell_exec, you can make an ajax call to script.php when the user visits index.php.
Another option is to run the script.php as a cron job every 5 minutes or so. When the user visits index.php, some data can be saved to database indicating that script.php should run. script.php should check if it is marked for running.
I need to make a php script that execute another function (or another script) in background: When i call it, the flow of the script must continue even if the second script is not finished.
I found http://php.net/manual/en/function.sleep.php (sleep function), but is not exactly what i need.
Some help?
Edit:
What i want to acomplish?
I need to make a change in my database. Then run a php script, but i need to make another change in my database 1 second (or whatever lapse) after i ran the php script.
Since you apparently don't care about a response from the script you are calling.
And if your server allow...
You could call your script as if it was from a shell and say that it should discard output with "> /dev/null 2>/dev/null &"...
shell_exec('php yourotherscript.php > /dev/null 2>/dev/null &');
I thought that this would run without waiting for an output:
php /scripts/htdocs/summaries.live/app/scripts/generate-pdfs.php live 1 > /dev/null 2>&1
But it's not happening. PHP's exec() function is waiting for an output. How can I work around this to prevent this from happening?
you're missing & on the end of command
php /scripts/htdocs/summaries.live/app/scripts/generate-pdfs.php live 1 > /dev/null 2>&1 &
If running something with exec, the documentation states
Note:
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, when executing:
php '_cliScript.php' "535c862d53269d7c027962fccd3f9823" "12f9f2bc98d120848b883a9632e4048d" "444948150528855c6620042134857e6a805e8f92">"/var/www/output.txt" 2>&1 &
It works like a charm, on the other hand, trying to execute:
php '_cliScript.php' "535c862d53269d7c027962fccd3f9823" "12f9f2bc98d120848b883a9632e4048d" "444948150528855c6620042134857e6a805e8f92" 2>&1 &
It just hangs, doesn't even execute the script. Also tried:
php '_cliScript.php' 535c862d53269d7c027962fccd3f9823 12f9f2bc98d120848b883a9632e4048d 444948150528855c6620042134857e6a805e8f92 2>&1 &
php '_cliScript.php' 535c862d53269d7c027962fccd3f9823 12f9f2bc98d120848b883a9632e4048d 444948150528855c6620042134857e6a805e8f92 &
And they hang too.. :) any suggestions? I'm going mental over here.
Well, if you want to try to get information back from the script then don't put the & on the end, otherwise, if you don't want to output to a file, but don't need any info from the script, then >/dev/null instead of a file.