Continue SSH background task/jobs when closing SSH

27

20

I have the same question as here but even beyond that.

If I logout/exit the SSH connection, I need my program running in background. Before exit, I type bg and jobs to check background running task, but after exiting SSH, the program doesn't run, it closes when I close the SSH window.

What can I do if I want my program still running after closing the SSH window? I do use nohup screen but it also doesn't work, or am I missing something?

Huei

Posted 2013-08-15T09:47:52.827

Reputation: 443

Welcome to Super User! Please don't put your answer into the question. Post it as an answer below once you can (in about 7 hours) using the Answer your question button. Thank you. (Also note that there's no need to add <br/>s to your post. Just make paragraphs as needed. And while you're at it, please use proper capitalization. It makes your posts easier to read.) – slhck – 2013-08-15T10:10:35.303

BTW no need to use bg or nohup when using screen. Also this question is related.

– justbrowsing – 2013-08-15T10:38:59.760

you make check this thread @ stack overflow http://stackoverflow.com/questions/285015/linux-prevent-a-background-process-from-being-stopped-after-closing-ssh-client

– Ashildr – 2013-08-15T10:48:50.537

Answers

47

When you use screen you need to detach with CTRL+A+D before you exit ssh.

Alternatively, if you want to run a process directly with screen you can use

screen -dmSL [session name] [commands]
  • -d starts a screen session and immediately detaches from it
  • -m forces creating a new screen session
  • -S lets you give the session a name
  • -L turns on logging to ~/screenlog.0

example:

screen -dmSL workstuff myscript.sh

You can then either:

resume later using screen -x workstuff

or

check the log file less -r ~/screenlog.0

justbrowsing

Posted 2013-08-15T09:47:52.827

Reputation: 2 411

14

Use tmux. I'm not even sure if screen is even still under active development. It's kind of a preference thing, but as someone who uses tmux, I can testify that it set up in about 5 minutes, and worked perfectly.

On a Debian-based system (on the remote machine):

sudo apt-get install tmux

then enter tmux to enter, run whatever process you need, and hit Ctrl+B then D (this leaves the tmux session). Then, you can log out of SSH.

When you need to come back/check on it again, start up SSH, and enter tmux attach. It will take you back to your tmux session.

tmux has many more capabilities, as detailed here. But, this should solve your immediate problem.

Good luck!

evamvid

Posted 2013-08-15T09:47:52.827

Reputation: 393

7

At last, I solved it using screen:

In screen mode, detach it using CtrlAD. Then you can close the SSH window.

Huei

Posted 2013-08-15T09:47:52.827

Reputation: 443

5

nohup use: The first of the commands below starts the program abcd in the background in such a way that the subsequent logout does not stop it.

$ nohup abcd &

$ exit

Ashildr

Posted 2013-08-15T09:47:52.827

Reputation: 2 574