Home > linux > Daemonize any process in Linux

Daemonize any process in Linux

February 28th, 2010 Leave a comment Go to comments

Any user or admin more than likely faced or will face a situation where their command will take a long time to complete and at the same time you’d want to logout and go home.  If user’s shell exits, it sends a SIGHUP signal to it’s children killing them all.

Well one of the reasons why Linux is so awesome is that it has a solution for almost any situation.  So in this case, if the job is preceded by the nohup command, the program ignores SIGHUP and will continue to run on the system until completion. Processes run by nohup are immnue to SIGHUP and SIGQUIT signals.

By using nohup, it releases the command to the system level and it becomes independent of your shell and functions as a system daemon.  You can also run your command and redirect output to a log to scan through later.  Here are some examples of using nohup:

$ nohup myprogram &

  • The above command starts myprogram in the background in such a way that the subsequent logout does not stop it.  By default, nohup.out is created and captures all the output.

$ nohup myprogram > /tmp/output.log 2>&1 &

  • The above redirect output to a file other than the nohup.out.

If you already have something running in bg without using the nohup command, you can easily bring nohup to the picture:

$ myprogram &
$ nohup -p `pgrep myprogram`

  • myprogram is now preceded by nohup.

$ nohup find / -name ‘*’ -size +1000k > log.txt

  • The above example runs the find command, detaches itself in bg as a daemon and continues to run until completion.
Categories: linux Tags:
  1. orkan
    March 1st, 2010 at 03:07 | #1

    isn’t it better to use “screen” for such purposes? you can have multiple screens with environments for each of the tasks and you can control the output of all of them

  2. criptych
    June 12th, 2010 at 12:06 | #2

    There’s also “daemon,” which does the same thing (pretty much) and with less typing. :)

  1. March 1st, 2010 at 08:12 | #1
  2. March 1st, 2010 at 21:17 | #2
  3. March 24th, 2010 at 22:55 | #3