Tuesday, November 17, 2009

Linux: runing app in daemon/background mode

Every cool guy knows that launching command with ampersand ('&') at the end leads to the background (let's say, console non-blocking) command processing. For sure, we talk about the honest Linux world. Meanwhile, there is a task which is widely used in a day-to-day IT life:
  • log in to a Linux server via ssh
  • launch some application, script which 1) is not designed as a daemon, 2) produces output to std out/ std error:

    ./myApp.sh &

  • log off from the server.
But when you are trying to access your server-side application you've got it stopped! Why?

You do remember that you've launched it with the ampersand at the end. The answers - below.
First of all, if your application provides any data to the std out or std error streams then you have to redirect those outputs somewhere, for example, to a log file, or to /dev/null if the output won't be taken into account.
./myApp.sh &> log.txt &
Here we've done redirect of both system out and system error streams to the file 'log.txt'. Redirect should be done to detach your app from console which will be destructed upon the logout.
But it not enough to fix our problem: launched command should live after our logout.
And here nohup goes on:
nohup sh myApp.sh &> log.txt &
That's it! When you do logout the sshd application which hosted your appearance on the server is killed so the same signal is sent to each child process of the sshd - even for the 'myApp.sh'. You may run pstree on the server to check real process tree. Nohup makes your application immune to the hangup signal, qoute from man page: "so that the command can continue running in the background after you log out". It should be stated that with nohup it is not necessary to tun application in background mode.
nohup sh myApp.sh &> log.txt
It's just a matter of console - it won't be blocked if application has been run with ampersand.

There is another one useful and mature program called screen which gives you more opportunities but this another long story to tell. You'll easily find info with "screen linux" request to any search engine.

3 comments:

  1. just two notes:
    - it's not necessary to redirect standard output, nohup redirects it by default to $HOME/nohup.out if the output is a terminal;
    - sshd won't be killed when you log out, it's a daemon.

    and i have a question, why do you put nohup in the background?

    ReplyDelete
  2. concerning redirect- agree, thanks for the correction.
    2nd- Well, we are both right: sshd daemon creates new self instance for each ssh-connection and exactly this sshd-intance will be killed upon logout.

    3rd You are right, it's not necessery to run application in background mode with nohup. The only reason to do it - just to have a non-blocked console after the given app has been launched. Anyway, I've corrected my post.
    Thank you much, dear_Anonymous!

    ReplyDelete
  3. hello. thank you very much for letting me comment. very good article I would like more information on this item

    ReplyDelete