Run php command in background

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

The greater-thans [>] in commands like these redirect the program’s output somewhere. In this case, something is being redirected to /dev/null, and something is being redirected to &1

Standard in, out, and error

There are three standard sources of input and output for a program. Standard input usually comes from the keyboard if it’s an interactive program, or from another program if it’s processing the other program’s output. The program usually prints to standard output, and sometimes prints to standard error. These three file descriptors [you can think of them as “data pipes”] are often called STDIN, STDOUT, and STDERR.

Sometimes they’re not named, they’re numbered! The built-in numberings for them are 0, 1, and 2, in that order. By default, if you don’t name or number one explicitly, you’re talking about STDOUT.

the command above is redirecting the standard output to /dev/null, which is a place you can dump anything you don’t want, then redirecting standard error into standard output [you have to put an & in front of the destination when you do this].

The short explanation, therefore, is “all output from this command should be shoved into a black hole.” That’s one good way to make a program be really quiet!

& at the end puts the command in background.

ref: //www.xaprb.com/blog/2006/06/06/what-does-devnull-21-mean/

What is the background process and why do we need them?

A process that runs behind the scenes [i.e. in the background] and without user intervention is called the background process. Debug/error logging, monitoring any system with grafana or kibana,  user notification,  scheduling jobs, and publishing some data are a typical example of background process. The background process is usually a child process created by a control process for processing a computing task. After creation, the child process will run on its own, performing the task independent of the parent process’s state.

There are a lot of ways to run a job in the background. Crontab, multithreading in java, go routines in golang are examples of such cases. 

To run a process in ubuntu, we simply type a command on the terminal. For example, to run a PHP file, we use the following command :

php filename.php

In PHP, we can not directly run any process job in the background even if the parent job terminates. Before we get to know how to run the process in the background let’s know how to execute terminal commands from PHP script or program. To achieve this functionality we can use exec and shell_exec functions in PHP.

A sample command to run any command with PHP is:

PHP

There are the important points of the above code:

  • The output [STDOUT] of the script must be directed to a file. /dev/null indicates that we are not logging the output.
  • The errors [STDERR] also must be directed to a file. 2>&1 means that STDERR is redirected into STDOUT and therefore into the nirvana.
  • The final & tells the command to execute in the background.

 We can check the status of any running process with command ps, for example:

ps -ef

So, to run any background process from PHP, we can simply use either exec or shell_exec function to execute any terminal command and in that command, we can simply add & at the last so that, the process can run in the background.

Below is the implementation of the above approach:

PHP

Output

processID of process in background is: 19926
current processID is: 19924


Can a PHP script run 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.

How do I run a PHP script continuously?

The best way to achieve OP's goal is to use one cron job to poll a script every 1, 5, or however many minutes. That script should check a file [or db value] for a date + time. If the date + time is ≤ the current time, it should look through the full queue of tasks and do any that are ready.

How do I know if PHP script is running in background?

If you started it in background use ps aux | grep time. php to get PID. Then just kill PID . If process started in foreground, use to interrupt it.

How do I run a script in the background?

Running shell command or script in background using nohup command. Another way you can run a command in the background is using the nohup command. The nohup command, short for no hang up, is a command that keeps a process running even after exiting the shell.

Chủ Đề