12

I have a PHP script with infinite loop. I need this script running forever. So, I run

php /path/to/script.php > /dev/null &

And it works in background in my current user's security context. But when I close terminal window (log off), of course, CentOS Linux kills my program.

I see two guesses: run from a different user in background or make a daemon. I need help in each situation.

Thanks a lot!

  • possible duplicate of http://stackoverflow.com/questions/285015/linux-prevent-a-background-process-from-being-stopped-after-closing-ssh-client – Ether Apr 10 '10 at 17:07

5 Answers5

26

nohup is your friend.

nohup command &
Aaron
  • 55,518
  • 11
  • 116
  • 132
Andrew B
  • 839
  • 9
  • 14
6

I think the general solution to that is nohup:

nohup is a POSIX command to ignore the HUP (hangup) signal, enabling the command to keep running after the user who issues the command has logged out. The HUP (hangup) signal is by convention the way a terminal warns depending processes of logout.

nohup is most often used to run commands in the background as daemons. Output that would normally go to the terminal goes to a file called nohup.out if it has not already been redirected. This command is very helpful when there is a need to run numerous batch jobs which are inter-dependent.

Community
  • 1
  • 1
Pekka
  • 442,112
  • 142
  • 972
  • 1,088
1

nohup is your friend.

futureelite7
  • 11,462
  • 10
  • 53
  • 87
1

You could:

  • Install screen and run the command from there. screen is a persistent terminal session that you can leave running.
  • Write an init/upstart (whatever you use) script so it loads on boot
  • Use the pear lib system_daemon
  • Use cron if batch work fits the scenario better (just remember to check for running instances before you launch another, iff concurrency is an issue)
  • Edit: or as everybody else and their brother has just said, nohup
Oli
  • 235,628
  • 64
  • 220
  • 299
1

Using command

nohup your_command &

For example
nohup phantomjs highcharts-convert.js -host 127.0.0.1 -port 3003 &

here "phantomjs highcharts-convert.js -host 127.0.0.1 -port 3003" was my command

Prahlad
  • 716
  • 8
  • 17