Reputation: 125
Basically, I'm working on a remote server, and connecting via SSH. Now, I can run processes / execute bash scripts normally with my SSH console, but there's one problem. As soon as I log off, those bash scripts just quit.
Is there any way for me to start a bash script, from SSH, and let it run, even if my computer, on which I initiated the bash script to the server, is switched off?
Thanks in advance, Peace! ~Tom
Upvotes: 2
Views: 2321
Reputation: 18624
The nohup
program jim suggests is good. See its command line options to change log location.
Another cool option is screen
. When you start it you can have multiple screens and if you log off an later log in, then you can reattach to the same session with screen -r
. It has many cool features but you need to read the doc.
The simplest thing is when you start some scrtips with &
at end, you can use the disown
bash
command to make these scripts not terminate with bash exit.
Hope this gives you some clues.
Update, a quick getting started with screen:
screen
to start a sessionCtrl+a+c
will create a new window and you go to it, you can start other ocmmandsCtrl+a+#
where #
is from 0 to 9 will move you to the window you selectCtrl+a+d
detach your session and you are back to normal terminalscreen -r
re-attaches you to a session, if you have multiple, you are asked to type part of its name (must be a unique sequence contained in the session name)Upvotes: 4
Reputation: 16399
Test script
script call it remote.sh:
at now <<!
/path/to/test.h> /tmp/test.out
!
copy this to the remote box: scp remote.sh thomas@remote: ssh remote 'chmod +x remote.sh'
cd /tmp;
cp *.foo /path/to/somewhere/else
copy the script to the remote server
scp test.sh thomas@remote: ssh remote 'chmod +x test.sh'
ssh remote './remote.sh'
Repeat steps 1, 2 & 3 each time you need to run your script without waiting for it.
Upvotes: 2
Reputation: 8623
In my opinion one of the easiest ways to achieve this is to use 'screen'. This will create a virtual terminal which won't kill your tasks after you logout.
Your process will now continue to work.
After you relogin type 'screen -list' and find the pid of the last screen session. Then type
screen -dr YOUR_PID
To reopen the screen and continue your work.
P.S. Screen is full of features that are super usefull for remote administration. Just have a look at a tutorial.
Upvotes: 1