Search Ali's Blog

Thursday, September 30, 2010

Run as daemon in Linux

In Linux, when a command is executed from a terminal window, it will be child of that window's PID. For example, see below how xlogo is parent of the terminal:
# open terminal window:
# echo $$
  4830
# xlogo &
  4956
# ps -l | grep xlogo
  0 S 1049 4956 4830 0 75 0 - 9979 - pts/11 00:00:00 xlogo

As a result, whenever you close the terminal (parent) window, the xlogo will terminate.

If you wish to run your application as daemon, use the command NOHUP(1), this will make the running application immune to hangups. For example, see how xlogo is child of PID 1 (the init process):
# open terminal window:
# echo $$
  4830
# nohup xlogo &
  5013
# close the terminal window
# ps -l|grep xlogo
  0 S 1049 5013 1 0 75 0 - 9979 - pts/11 00:00:00 xlogo


No comments: