LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Running a background process and then logging off (https://www.linuxquestions.org/questions/linux-general-1/running-a-background-process-and-then-logging-off-674834/)

simplified 10-07-2008 10:38 AM

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.

timnp 10-07-2008 11:07 AM

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.

simplified 10-07-2008 11:33 AM

Nice - I'll check it out and get back to you with the results. Thanks for the pointer!

GazL 10-07-2008 11:43 AM

'nohup' may also do the trick for you if you want something simpler. 'screen' is a better choice for anything interactive.

trickykid 10-07-2008 12:44 PM

Quote:

Originally Posted by GazL (Post 3303038)
'nohup' may also do the trick for you if you want something simpler. 'screen' is a better choice for anything interactive.

Screen has the capability to launch a command to run within a new screen session. Once the command is complete, it will usually exit the screen session or you can invoke an exit yourself. Makes it nice to be able monitor the command if its still running til its done. read the screen man page for all available options.

timnp 10-07-2008 05:51 PM

Quote:

Originally Posted by trickykid (Post 3303104)
Screen has the capability to launch a command to run within a new screen session. Once the command is complete, it will usually exit the screen session or you can invoke an exit yourself. Makes it nice to be able monitor the command if its still running til its done. read the screen man page for all available options.

Correct if you lauch it as "screen <command>"

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!

rnturn 10-07-2008 10:27 PM

Quote:

Originally Posted by simplified (Post 3302983)
[snip]

- 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.

Someone else already mentioned "nohup" as a way to do this.

Another is to submit the script to batch:
Code:

at -f path-to-script now
or
Code:

at -f `pwd`/script now
The first example is sort of generic. The second example is one I use most since I'm in the directory containing the script already (and I'm too prone to mistyping the full path).

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

simplified 10-08-2008 02:58 AM

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.

chrism01 10-08-2008 09:29 PM

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.
:)

NVRAM 10-09-2008 04:41 PM

Quote:

Originally Posted by simplified (Post 3303807)
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.

If you forget the ampersand, you can usually:
  1. type Ctrl-Z to suspend the job,
  2. type bg to resume the job in the background.

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

simplified 10-10-2008 03:25 AM

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.