How to Keep A Program Running with Ssh logout

snj

Nic

Posted on December 31, 2021

How to Keep A Program Running with Ssh logout

Some times we need to keep jobs running without a ssh session, here there are three ways to achieve this. I suggest to learn a terminal multiplexer in long term, it’s really useful when you have long time working on Linux/Unix system.

Option 1: nohup

The best and simplest way is:

nohup long-running-command &
Enter fullscreen mode Exit fullscreen mode

It was made specifically for this, by default it will log stdout to nohup.log, if standard output is not a terminal, logs will be appended to ~/nohup.log. If you want to specify the output log into a different file:

nohup COMMAND > FILE
Enter fullscreen mode Exit fullscreen mode

More details can refer to man nohup. What nohup does, is to effectively separate the process from the terminal:

  • It closes standard input (the program will not be able to read any input, even if it is run in the foreground. it is not halted, but will receive an error code or EOF).
  • It redirects standard output and standard error to the file nohup.out, so the program won’t fail for writing to standard output if the terminal fails, so whatever the process writes is not lost.
  • It prevents the process from receiving a SIGHUP (thus the name).

Option 2: bg + disown

If you already have started the program and don’t want restart it, we can use ctrl+z to stop current execution and then use bg to resume it and switch the running program to background (We can use fg to pull the job to frontground)

Finally we use disown to remove the process from the shell’s job control.

So, even you logout from a ssh session, the program will still keep running in background.

ctrl+zbgdisown -h
Enter fullscreen mode Exit fullscreen mode

Option 3: Use terminal multiplexer

We have three choices for this, [[screen]], [[tmux]], and [[byobu]].

screen and tmux is mostly used ones. On Ubuntu or Debian system, we install it with:

sudo apt-get install screen sudo apt-get install tmux
Enter fullscreen mode Exit fullscreen mode

Byobu is an enhancement for the GNU Screen, you can install it with this commands:

sudo add-apt-repository ppa:byobu/ppasudo apt-get updatesudo apt-get install byobu
Enter fullscreen mode Exit fullscreen mode
💖 💪 🙅 🚩
snj
Nic

Posted on December 31, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related