Should I use "| now" or ampersand (&) to run the script in the background?

I was looking at answers about running a php script in the background, and they seem to be distributed in two ways.

Some people suggest using this (or something similar):

/usr/bin/php command >/dev/null 2>&1 & 

Others suggest using the "at" command:

 echo '/usr/bin/php command'| at now 

What are the main differences regarding these two methods? What are the pros and cons?

What I'm trying to do is when the user submits the form, I need to run a few minutes of a script, which obviously should run in the background. I tried both ways, and they both worked for me, but I'm not sure what to choose.

+4
source share
2 answers

The at command is a scheduler that accepts lines from stdin or files that contain commands to run at a specific time. Your example:

 echo '/usr/bin/php command'| at now 

Gives the 'at' command to your command as a string and plans to run it immediately.

The first method is a typical background process through a shell:

 /usr/bin/php command >/dev/null 2>&1 & 

The "> / dev / null" part tells the shell to send your command stdout file to / dev / null, and the "2> & 1" part says to send the stderr output of your command to stdout (which then goes to / dev / null) . Together they suppress the exit. You can do this in one step:

 /usr/bin/php command &>/dev/null & # bash 

The "&" at the end is what the shell says to execute the background process.

The advantages of using 'at' is the flexibility of planning your team to launch at a different time than now (by the way). Read the man page. The disadvantages are that it cannot be installed on all systems.

The advantages of using and using shells are that there are no overheads based on the background. Using the β€œat” command to run the command immediately causes excessive overflow because it involves creating a new process, scheduling the command, knowing that it is configured to run, and then running it in the background. Whereas the & command in the shell will just run this command in the background.

+6
source

When you call it with the at command, the program runs in the background with the at daemon.

When you use & , the process is still bound to the current shell. When you close the shell, the process ends. You can also do nohoup /usr/bin/php command so that the process continues to run when you close the shell.

+2
source

Source: https://habr.com/ru/post/1385666/


All Articles