PHP Run background script from cli script [duplicate] - php

This question already has answers here:
Running PHP script from command line as background process
(4 answers)
Closed 2 years ago.
From a php script I need to launch a new php script in background.
I expect this to be working:
shell_exec("php mySecondScript.php &");
Execution of main script hangs and secondScript is not even started. Of course if I remove '&' the script is executed, but synchronously. Any reason for this?

You don't redirect output of command.
shell_exec("php mySecondScript.php > /dev/null &");

Related

Executing Bash script from PHP but nothing happens [duplicate]

This question already has answers here:
How can I debug exec() problems?
(4 answers)
Screen -X isn't working ("No screen found")
(2 answers)
Closed 6 years ago.
I know that this question has been discussed before but i still can't find a solution. I'm trying to execute a Bash script from a PHP file on a (really small) website which is run using the PHP built in webserver. When running the PHP file in the terminal with
php -f test.php
it works fine and the bash script executes. However when i start the webserver and call the PHP function from the website, nothing happens. The Bash script is invoked with:
exec('/home/user/website/bash_script', $out, $return);
inside the PHP-file and i get that return = 1.
On the website the PHP script that executes the bash-script is called by another PHP script through:
<input type="button" value="..." onclick="location='test.php'"/>
I first thought that it was a problem with my permissions, but i added the script-file in visudo to be able to run it as super-user without password but still no luck. I also changed the file permissions to 755.
Here is test.php (which works fine when run through the terminal):
<?php
exec ('/home/user/website/test');
?>
Also i CAN execute exec('cp bla bla2'); from the webserver so theres no problem with exec()
EDIT------------------------------------------------------------------------
The Bash script is sending a command to a different screen and it seems that this is the root of the problem. I think that this can solve my problem:
Screen -X isn't working ("No screen found")

Running unix process in background from php [duplicate]

This question already has answers here:
php execute a background process
(21 answers)
Closed 9 years ago.
If I run this unix command directly in shell:
$ sleep 100 &
sleep runs in the background as expected, and I can continue working in the command line.
but trying the same thing with shell_exec() and php I get different results.
<?php
$sleep = $argv[1];
$shell="sleep " . $sleep . " &";
shell_exec($shell);
?>
when executing php sleep.php 100 the command line hangs and wont accept any more commands until sleep finishes. I am not sure whether this is a nuance I am missing with shell_exec() / $argv of php or with the unix shell.
Thanks.
The shell_exec function is trying to capture the output of the command, which it can't do while simultaneously continuing processing. In fact, if you look at the php source code, the php shell_exec function does a popen C call, which does a wait syscall on the command. wait guarantees that the subprocess doesn't return until the child has exited.

How to run a background process in PHP and get the return value [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Make PHP wait for Matlab script to finish executing
Okay, starting from php execute a background process
to run a background process works great. The problem is, I need the return of that process also. The obvious solution to me is :
$cmd = "($cmd > $outputfile 2>&1 || echo $? > $returnfile) & echo $! > $pidfile";
exec($cmd);
When I run the generated command on the command line, it backgrounds and the files are filled out as expected. The problem is that when php exec() runs, the command doesn't go to the background (at least, exec doesn't return until the command finishes). I tried variations with nohup and wait $pid, but still no solution.
Any thoughts?
This is tricky- you could potentially fork the process to do something else, leaving the original process in place.
http://php.net/manual/en/function.pcntl-fork.php
However, if this is a web application, there's no built-in way to retrieve the return code or STDOUT back into the parent process since it's technically async (your request-response cycle will likely end before a result can be produced).
You could store the return code and / or STDOUT to files to check later, though.

PHP shell_exec wait for script to finish? [duplicate]

This question already has answers here:
php exec command (or similar) to not wait for result
(7 answers)
Closed 7 years ago.
I have a PHP script that queries a database for a list of jobs to be done and fires off other PHP scripts based on what it finds in the database (basically a process queue).
Some of the scripts that the queue runner script executes may take 30 seconds or so to finish running (generating PDFs, resizing images, etc).
The problem is that shell_exec() in the queue runner script calls the processing scripts, but then doesn't wait for them to finish, resulting in the queue not being completed.
Queue runner script:
#!/usr/bin/php
<?php
// Loop through database and find jobs to be done
shell_exec(sprintf("/root/scripts/%s.php", $row['jobName']));
?>
Job script:
#!/usr/bin/php
<?php
shell_exec("/usr/bin/htmldoc -t pdf --webpage test.html > test.pdf");
// Update database to mark job as completed
?>
Running the job script directly from the command line works and the PDF is created.
Any ideas on how to fix this? Or a better way to run a process queue?
Try this:
shell_exec("nohup /usr/bin/htmldoc -t pdf --webpage test.html > test.pdf 2>&1 &");

php exec in background not working [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Call another PHP script and return control to user before the other script completes
Hello,
I'm executing this command in php:
exec('php /path/to/script.php one_argument_passed &");
But it's not executing it in the background. Is there a setting or something in php I need to do to make this work?
Maybe you have a permissions problem.

Categories