In PHP,
I need to execute background tasks. So I have three choices to run script from
following commands:
1)exec
2)shell_exec
3)passthru
I randomly decided to use exec command in PHP.
Because All of these have similar characteristics.
Now I want to know that Is there any disadvantage of exec() command
which will run in background ? Actually I am not much aware about this command.
Is It make separate thread from main thread ? If yes then
,Is there any way to clean up or suspend this background thread ?or it simply forget it after firing the command
?
I am asking this because in my code there is need to run this script very fast one after one within moments
. So Is it create any server load ?
EDIT : I am using exec command like this :
exec("/usr/bin/php /path/to/Notification.php >> /path/to/log_file.log 2>&1 &");
Please guide me on this.Any help will be appreciated.
It depends entirely on the kind of call you make if
exec()
will wait for the command to finish or not. Of course, running a lot of background threads on a server could potentially create lots of CPU load.Now, normally any command you run with
exec()
will NOT be run in background, that means your PHP script will wait until the command finishes executing. In order to have your commands run in background, you have to redirect its output stream and tell it explicitly to run in background by adding this to your command:Of course the downside of this will be that you can never retrieve any results from your commands back to PHP. It will be fire and forget.