Why does my backgrounded script/app die after I close my terminal window?

Short answer: because you’re a freaking idiot.

Long answer: My shell script is on a Linux server. I access via ssh using Putty. I run it from the command line, from Window #1,  like this:

$ bin/myapp.sh &

when I close my ssh window. In Window #2, I watch the logs, and I can see that myapp.sh has exited. Specifically, it caught a SIGHUP.

How to fix this? Don’t close the window. Instead, exit from the ssh session by typing “exit”.

Short answer: I’m a freaking idiot.

This entry was posted in Coding. Bookmark the permalink.

3 Responses to Why does my backgrounded script/app die after I close my terminal window?

  1. givemeascream says:

    launching commands with nohup is also a good option :)

  2. You should check out the “screen” command. It depends on the distro whether it’s installed by default or not, the package name is “screen”.
    If you are inside a screen session, not only you can close the terminal window, you can re-attach to the session any time later, so you can check on the output of your long running program.

    I write a (bit) more about screen here:
    http://titan2x.com/productivity-tips.html

    And my cheat sheet for screen’s key bindings is here:
    http://wiki.titan2x.com/index.php?title=Screen_cheat_sheet

  3. ralph says:

    with “screen” you already have to have it running beforehand. Another way is to background the job with ctrl-z and then “bg” command, then use “disown” command with process as argument, or percent and number for job, e.g. disown %1. But not as good as “screen”, you can’t re-attach to it later, have to kill it.

Leave a Reply