Executing a Shell Script in Background with PHP

Executing a shell script in background with php

$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

Running a shell script in the background from PHP

Your number 5 approach is close but you need to combine it with 6 like this

shell_exec("/script.sh > run.log 2>&1 &");

Or redirect to /dev/null like this

shell_exec("/script.sh > /dev/null 2>/dev/null &")

It appears that the PHP development server cannot fork off commands properly. Using a real web server like Apache or NGinx will resolve the issue.

run php script in background and never die linux

A simple solution is

nohup php script.php &

So you run script in background and disconnect the process from the terminal. If it doesn't help, try disown command after it. There is a good answer with detailed explanation of differences between these commands.

To get full control of your script, a good choice would be a System V init script. There is a template at https://github.com/fhd/init-script-template, which you can use.

Copy template to /etc/init.d directory and rename it. In template you need to change variables:

dir="/your/working/directory"
cmd="nohup php script.php"
user="your user"

Doing that you will be able to control your script by

/etc/init.d/your_script start
/etc/init.d/your_script stop

Make sure you have permissions to write in /var/log/ and /var/run/, or run script as sudo(leave user="" empty)

Running PHP script from command line as background process

Are you sure the script doesn't contain any errors? This is what normally makes "execution terminates very quickly".

First, append:

error_reporting(E_ALL); ini_set('display_errors', 1);

at the top of your script to display any errors it may have, then you can use :

nohup php filename.php &

nohup runs a command even if the session is disconnected or the user
logs out.

OR

nohup php filename.php >/dev/null 2>&1 &

Same as above but doesn't create nohup.out file.


You can also use:

ignore_user_abort(1);

Set whether a client disconnect should abort script execution

`set_time_limit(0);`

Limits the script maximum execution time, in this case it will run until the
process finishes or the apache process restarts.


#Notes
The php and the filename.php paths may be provided as a full-path, instead of php and filename.php, you can use /usr/bin/php and /full/path/to/filename.php.

Full Path is recommended to avoid file not found errors.

When executing a bash script with Php exec(), why does Php wait for disowned background jobs to complete?

PHP exec(..) opens a pipe to for the command, and reads from it until it closes, returning the data as a string (which you don't use).

Pipes don't close until all writers have closed it, so any background process with the pipe open as stdout will keep it open. Therefore, exec will keep waiting for them, thinking there may be more output. It's a good thing too: five seconds later, it reads a "test-script.sh COMPLETE" message which would otherwise have disappeared.

When you redirect stdout to /dev/null instead for the background process, it will not keep the pipe open, and exec therefore returns immediately since there can't possibly be any more output.

php - How to exec in background multiple bash command

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.



Related Topics



Leave a reply



Submit