Doing a long-running command on ssh
The best way is to use screen
(on the server) to start a session to run the command in and then disconnect the screen so it will keep running, and you can do other things, or just disconnect from the server. The other option is to use nohup
in combination with &
so you would have nohup <command> &
The existing answers can work well, but I needed something for BusyBox (a shell and set of tools for minimal hardware like home routers). My system does not have screen
, dtach
, at
, disown
, or even nohup
! So thanks to tbc0 on SO (link), I found this gem. It returns immediately but the server process continues to run:
ssh myserver 'sleep 100 >&- 2>&- <&- &'
Or, if multiple commands are needed:
ssh myserver '(echo one; sleep 100; echo two; sleep 200) >&- 2>&- <&- &'
Explanation:
-
>&-
- close stdout handle -
2>&-
- close stderr -
<&-
- close stdin -
&
- put process in background
This uses no external programs and should work across ksh, ash, Bourne shell, bash, etc.
You can also use disown
if you've already started the process without screen
or nohup