LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 10-07-2008, 10:38 AM   #1
simplified
Member
 
Registered: May 2007
Location: London, UK
Distribution: Kubuntu 9.04 x64 / Ubuntu Server 9.04 x64
Posts: 50

Rep: Reputation: 15
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.
 
Old 10-07-2008, 11:07 AM   #2
timnp
Member
 
Registered: Feb 2008
Location: London
Distribution: FC8, FC9, Centos 4, Centos 5, Knoppix
Posts: 52

Rep: Reputation: 17
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.
 
Old 10-07-2008, 11:33 AM   #3
simplified
Member
 
Registered: May 2007
Location: London, UK
Distribution: Kubuntu 9.04 x64 / Ubuntu Server 9.04 x64
Posts: 50

Original Poster
Rep: Reputation: 15
Nice - I'll check it out and get back to you with the results. Thanks for the pointer!
 
Old 10-07-2008, 11:43 AM   #4
GazL
LQ Veteran
 
Registered: May 2008
Posts: 6,882

Rep: Reputation: 4988Reputation: 4988Reputation: 4988Reputation: 4988Reputation: 4988Reputation: 4988Reputation: 4988Reputation: 4988Reputation: 4988Reputation: 4988Reputation: 4988
'nohup' may also do the trick for you if you want something simpler. 'screen' is a better choice for anything interactive.
 
Old 10-07-2008, 12:44 PM   #5
trickykid
LQ Guru
 
Registered: Jan 2001
Posts: 24,149

Rep: Reputation: 269Reputation: 269Reputation: 269
Quote:
Originally Posted by GazL View Post
'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.
 
Old 10-07-2008, 05:51 PM   #6
timnp
Member
 
Registered: Feb 2008
Location: London
Distribution: FC8, FC9, Centos 4, Centos 5, Knoppix
Posts: 52

Rep: Reputation: 17
Quote:
Originally Posted by trickykid View Post
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!
 
Old 10-07-2008, 10:27 PM   #7
rnturn
Senior Member
 
Registered: Jan 2003
Location: Illinois (SW Chicago 'burbs)
Distribution: openSUSE, Raspbian, Slackware. Previous: MacOS, Red Hat, Coherent, Consensys SVR4.2, Tru64, Solaris
Posts: 2,795

Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
Quote:
Originally Posted by simplified View Post
[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
 
Old 10-08-2008, 02:58 AM   #8
simplified
Member
 
Registered: May 2007
Location: London, UK
Distribution: Kubuntu 9.04 x64 / Ubuntu Server 9.04 x64
Posts: 50

Original Poster
Rep: Reputation: 15
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.
 
Old 10-08-2008, 09:29 PM   #9
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,348

Rep: Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749
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.
 
Old 10-09-2008, 04:41 PM   #10
NVRAM
LQ Newbie
 
Registered: Oct 2004
Location: Western USA
Distribution: Fedora,Suse,Redhat,Puppy,etc.
Posts: 13

Rep: Reputation: 1
Quote:
Originally Posted by simplified View Post
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
 
Old 10-10-2008, 03:25 AM   #11
simplified
Member
 
Registered: May 2007
Location: London, UK
Distribution: Kubuntu 9.04 x64 / Ubuntu Server 9.04 x64
Posts: 50

Original Poster
Rep: Reputation: 15
Hey NVRAM, nice tip.. I like it! I only wished I'd had it a few days back ;-)

Simp.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Process running in background baddah Linux - General 3 02-22-2008 12:44 AM
running process in background forever danran Linux - General 2 11-19-2006 09:44 AM
logging script, running background how lhrt Linux - General 2 06-09-2006 10:06 AM
background running process elbriga Programming 2 09-21-2004 06:08 PM
trouble with background process and logging out successfully isaia Linux - Newbie 3 04-05-2004 12:03 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 12:50 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration