How to keep the process running on remote server even if the ssh connection is closed?
-
Use: `nohup programname &` – sat Apr 29 '14 at 14:51
-
@sat I started dropbox on server in this way but it doesn't work. dropbox closed after I exit the terminal. – wsdzbm Aug 20 '15 at 16:52
-
Asked and answered *ad nauseam*. Possible duplicate of [How to make a programme continue to run after log out from ssh?](https://stackoverflow.com/q/954302/608639), [How to prevent a background process from being stopped after closing SSH client in Linux](https://stackoverflow.com/q/285015/608639), [How to keep processes running after ending ssh session?](https://askubuntu.com/q/8653), etc. – jww Jul 31 '19 at 19:54
4 Answers
You can use screen to detach a session. You connect to your ssh server, launch screen and then your computation...
At your next connection, screen -a
to attach previous sessions

- 285
- 4
- 10
Another idea would be to use tmux which works like screen but it is more flexible and easier to use (in my opinion). To keep a process running you could simply start a tmux session with the command tmux
.
This will "open a new terminal" in which to run your programs, if you want you can give a name to the session to easily find it in case you have multiple sessions with the command ctrl+b $
instead of using the default name (e.g. 0).
After running the programs you need in the tmux
session, you simply detach from the session with ctrl+b d
and go back to the normal terminal from which you can call exit
and close your ssh connection.
When you log in again, you simply run tmux attach -t <session_name>
to go inside the session where you left your tasks running.
tmux
offers a lot of other functionalities too, like screen splitting inside the session with ctrl+b %
or ctrl+b "
for vertical and horizontal splitting. (you can move between different screens with ctrl+b <arrow in the direction of the screen you want to go to>
.

- 1,518
- 1
- 13
- 35
-
https://superuser.com/questions/236158/tmux-vs-screen for a tmux vs screen comparison – ninazzo Sep 10 '21 at 18:04
You can run your process/command inside a screen or tmux session, Or you can do:
yourcmd &
disown

- 401
- 5
- 7
All these Answers are correct but if you are initiating SSH from a code and giving bash commands then they don't work. The best way is to use this command
nohup command > /dev/null 2>&1 &

- 776
- 6
- 11