Running a background process and then logging off
Hi All
I've got a script that I've written that allows me to backup all my files onto a removable hard drive. Nothing clever really... What I want to do ideally is to be able to log in remotely via SSH, run the script and log off - I've found that running the script as follows: # ./usbbackup.sh & ... just runs it as a background task on the jobs. However, when I logoff it kills the process. Ideally what I'd like to do is find a way of: - logging on via ssh - run the script in the background - logoff ssh session - go down the pub - come back home, job done! Does anyone have any ideas? Cheers, Simp. |
Yeah, use the `screen` command.
Its pretty powerful so probably best if you read its manual pages but essentially once you have logged in via ssh (or anything else) run `screen` and then you will be in what is termed a screen session. You can run whatever long running tasks you like and when you detach from the screen (by pressing ctrl-a, d) you will be back at your normal terminal session. You can then log out. Log back in again. If you run `screen` again it actually creates a new screen session, to reattach to your previous existing screen session run `screen -r`. Screen is amazingly useful and has a lot of nice features, I suggest you check it out. |
Nice - I'll check it out and get back to you with the results. Thanks for the pointer!
|
'nohup' may also do the trick for you if you want something simpler. 'screen' is a better choice for anything interactive.
|
Quote:
|
Quote:
But normally I just run "screen" to get the screened shell and then hack away in there. It's useful to get into the habit of running "screen" as soon as you log in so that if you're accidentally disconnected you can carry on from where you left off. I recently discovered all the split-screen stuff it can do, very timesaving! |
Quote:
Another is to submit the script to batch: Code:
at -f path-to-script now Code:
at -f `pwd`/script now You do need to have the "atd" daemon running in order to do this. Output from your script will be mailed to you. HTH... -- Rick |
Hi All
Many thanks for all of your responses! As with anything *nix it's cool that there's several ways to do the same thing - with us all with our favourites. I have to say that the nohup method was the one that I liked the best as my script does log the changes made along with any errors. However, to anyone reading this thread that didn't know how to do it don't forget the "&" at the end of the command for nohup. I'm embarrassed to say that I was banging my head against the wall until I had a "oh yeah, of course" moment and popped the "&" in. For example, if you wanted to ping another machine 100 times you would use: $ nohup ping -c 100 192.168.1.1 & Just thought that I'd point that out for anyone not in the know, caught my out! Thanks again all, Simp. |
Yeah, '&' puts a process in the background, but its still 'attached' to the originating terminal session. Hence the need for nohup to disconnect as well.
FYI nohup = no hangup, from the orig arpanet when everything was done over phone lines. :) |
Quote:
Try man bash and scan for "jobspec" for more details/options. In addition, to bg the disown -h option will apparently do what "nohup" would after the fact. - NVRAM |
Hey NVRAM, nice tip.. I like it! I only wished I'd had it a few days back ;-)
Simp. |
All times are GMT -5. The time now is 05:36 PM. |