Doing a long-running command on ssh
Solution 1:
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> &
Solution 2:
You can also use disown
if you've already started the process without screen
or nohup
Solution 3:
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 handle2>&-
- close stderr<&-
- close stdin&
- put process in background
This uses no external programs and should work across ksh, ash, Bourne shell, bash, etc.