Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php exec command (or similar) to not wait for result

Tags:

php

exec

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.

like image 449
greg Avatar asked Sep 29 '10 06:09

greg


People also ask

Does PHP exec wait until finished?

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.

What is PHP exec function?

The exec() function is an inbuilt function in PHP which is used to execute an external program and returns the last line of the output. It also returns NULL if no command run properly. Syntax: string exec( $command, $output, $return_var )

What is the difference between exec and Shell_exec?

1. The shell_exec() function is an inbuilt function in PHP that is used to execute the commands via shell and return the complete output as a string. The exec() function is an inbuilt function in PHP that is used to execute an external program and returns the last line of the output.

How do you check if exec is enabled in PHP?

php phpinfo(); ?> You can search for disable_functions and if exec is listed it means it is disabled. To enable it just remove the exec from the line and then you need to restart Apache and you will be good to go. If exec is not listed in the disable_functions line it means that it is enabled.


2 Answers

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 &"');

like image 101
Cristian Avatar answered Sep 25 '22 13:09

Cristian


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 &'); 
like image 32
codaddict Avatar answered Sep 25 '22 13:09

codaddict