How to run a php script in background

When we need to run some script without waiting the fronted user till the process is not completed, For that we need to execute some script in background to hiding the execution time to user.This process is hidden to the end user. It improves your Website efficiency.

The concept, In LINUX there is a shell script which is used to run the process in background. You can put a task (such as command or script) in a background by appending a & at the end of the command line. The & operator puts command in the background and free up your terminal. The command which runs in background is called a job. You can type other command while 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 the time you log-in into remote server via ssh. If you start a shell script or command and you exit (abort remote connection), the process / command will get killed. Sometime job or command takes a long time. If you are not sure when the job will finish, then it is better to leave job running in background. But, if you log out of the system, the job will be stopped and terminated by your shell.

What do you do to keep job running in the background when process gets SIGHUP?

The answer is simple, use nohup command line-utility which allows to run command/process or shell script that can continue running in the background after you log out from a shell:

nohup command syntax: nohup command-name &

What is exec?

This command is used to execute a process in Linux. It can process one or more process at a time.

How to use this PHP library on your code?

Step 1: create two file name index.php and process.php

step 2: include the PHPBackgroundProcesser.php file in the index.php

step 3: create a instance of the class BackgroundProcess
We can use this:

Type 1:

$proc=new BackgroundProcess('exec php <BASE_PATH>/process.php hello world');

Type 2:

$proc=new BackgroundProcess();
$proc->setCmd('exec php <BASE_PATH>/process.php hello world');

Type 3:

$proc=new BackgroundProcess();
$proc->setCmd('exec php <BASE_PATH>/process.php hello world')->start();

Alternatively you can execute PHP URL in background with out the direct (.php) file.

$process=new BackgroundProcess("curl -s -o <Base Path>/log/log_storewav.log <PHP URL to execute> -d param_key=<Param_value>");

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.

That’s all. You can grab this library from my github repository by clicking below link. You can also comment if you have any query or just wanna say Hi.


  • Stephen Ngethe

    Thanks for,

    this Do you have an example I am having a hardtime following up. like
    where do i put the

    $proc=new BackgroundProcess(‘exec php /process.php hello world’);

    is it in the file index or what ? what is in the process file?

  • Surender

    I have PDO Connection MYSQL exec command returns “could not find driver”

  • Omar Faruk

    I have downloaded a websocket example and tested with xamp server locally and it is working fine. It has a php file called sever.php which i have to run it from the command prompt first to run the server. After that i can able to connect to the websocket server and can send message. Everything is running fine with my laptop. Now I have uploaded the same project to my web server (lets say Since without running the server.php the client can not able to connect to the server, so how can i run the server.php in background into web server.

    Please look at the attached screen. this is how i run the server. The same thing i want to do it in my domain.

  • Umar

    will this script work on mac osx?

    I am trying like this

    $proc=new BackgroundProcess();
    $proc->setCmd(‘exec /Applications/MAMP/bin/php/php5.5.38/bin/php /Users/umar/Development/codeignitor_app/index.php reports bulk_reports generate_scheduled_report_batch’); // where reports is module bulkreports is controller and gener… is a function (HMVC CI Setup)
    echo “”; print_r($proc->showAllPocess());

    What does /dev/null do?

  • Manish Soni

    I am using this code, and starting background process, but on refresh again that file getting called.

    I want to get status of the current running file on refresh. On refresh again status getting blank.