php Icon

How to run a php script in background

How to run a php script in background

When we need to run up some script without waiting front end user until the process which is not completed, For that we need to execute some script into background to hiding execution time to users. This process is said to be hidden to end user which improves Website efficiency.
This concept in Linux there are shell script is used to run process in background. You can put task such as script or command into a background by appending a and at the end of command line. Command runs in background so called as job. You can type other command as background command is running.

Syntax : {command} &
Example: ls -l & exec php index.php > /dev/null 2>&1 & echo $!

How to check the background process in Linux?
ps -l (list all process)
ps -ef (all full details of process)

What is the command to execute a php script in background?
Syntax: nohup exec arg1 arg2 > /dev/null &

Example: nohup exec php process.php hello world > /dev/null &

What is nohup?
Most of time you can able to login to remote server through ssh. If you start a command and you exit or abort remote connection, process or command to get killed. Sometime exit or job (stop remote connection), process or command to get killed. Sometime command or job will take a long time. If you are not sure about this when job is said to stop then its better to stop job running towards background. If u log out of system then job is said to be stopped and terminated by shell.

What do you keep job running in background when process gets SIGHUP?
This answer is simple, use nohup command line-utility which will allows to run command/process or shell script that helps to continue running at the background after you log out from a shell:
nohup command syntax: nohup command-name &

What is exec?
Command is said to be used to execute a process at Linux which can process one or more process at any time.

How to use this PHP library on your code?
Create two file name process.php and index.php
Just include PHPbackgroundProcesser.php file at the index.php
Just create an instance of class background process

We can use this:
Type 1:
$proc=new BackgroundProcess(‘exec php /process.php hello world’);

Type 2:
$proc=new BackgroundProcess();
$proc->setCmd(‘exec php /process.php hello world’);

Type 3:
$proc=new BackgroundProcess();
$proc->setCmd(‘exec php /process.php hello world’)->start();
Alternatively you will able to execute PHP URL in background with out the direct (.php) file.

$process=new BackgroundProcess(“curl -s -o /log/log_storewav.log -d param_key=”);

How to get all process which is running?
$proc=new BackgroundProcess();

How to kill a process ?
$proc=new BackgroundProcess();
$proc->setProcessId(101)->stop(); //set the process id.