I want to run a bash script on a server in background?
I know I can use ssh to run a script, but the script may last for a long time, so can I run it in background? If so, how to do this?
To run a command in the background in bash, you use &
at the end.
./myscript.sh -opt1 > /tmp/myscript.out &
nohup
is your friend, but according to that Wikipedia entry, you can also use screen
.
nohup script &
exit
One option is to use a screen manager like GNU Screen. You start this (by running screen
, and then run your process inside it. You can then feel free to simply disconnect your SSH session - the process will continue to run.
Later on, you can reconnect, and run screen -r
to resume. It will redraw everything like you never even disconnected - allowing you to see the output of your process.