5

I can't seem to figure out a decent way to manage jobs over ssh. I want to start a job in an ssh session, logout, log back in, do a fg, check on the job, logout, then repeat the whole process. nohup doesn't seem to wrok well.

I used this command

 $ nohup script & > nohup.out 
 [1] 28128
 $ nohup: ignoring input and appending output to `nohup.out'
 [1]+  Exit 255                nohup script

If I do either

$ jobs
$ ps -ef |  grep script

I get nothing, i.e. the process isn't found.

Also, a related question I have is: If I'm in an ssh session, and want to send a job to the background, ctrl-z doesn't work. What to do?

Cany anyone help me out?

devin
  • 1,226
  • 3
  • 20
  • 27

6 Answers6

19

well - one of the options is to use screen.

  1. you log on
  2. you fire up screen
  3. you run your job
  4. you press ctrl+a d to detach
  5. you log off
  6. after few hours you log on arain and run screen -r - you have your shell / job result in front of you. or maybe it still runs...

more examples here.

another - probably worse if it's one time job - option is to demonize your shell script, add proper logging and use start-stop-daemon to run it in the background until it finishes. status - can be checked in logs produced.

pQd
  • 29,561
  • 5
  • 64
  • 106
  • You beat me. By 1 minute. +1. – Joseph Kern Jun 13 '09 at 19:21
  • 2
    I have most hosts I connect to automatically start/reconnect screen when I SSH in so I don't forget until a connection drop that screen would have stopped being a problem. See http://tlug.dnho.net/?q=node/239 for one example of setting this up - there are other variations out there that might be preferable depending on your setup/preferences so try a quick Google if this one doesn't work for you. – David Spillett Jun 13 '09 at 19:31
  • Screen is the solution here. – Amandasaurus Aug 10 '09 at 11:54
8

I use screen as well for that..typically I login and run

screen -S $meaningful-name

Then when I reconnect I just run

screen -r $meaningful-name

I also usually put my username in the screen session name if I'm running as root on one of our servers so other folks can see it and connect to it if need be.

Cube_Zombie
  • 169
  • 1
  • 5
6

You can use the program screen. I use it to manage several interactive shell programs.

Usage:

screen
startyoujob.sh
<C-A D> (Ctrl A then D, will detach your session)
screen -r (this will reattach your session)

A few screen tips over at the Arch Linux Wiki, the official screen web site, and some tips and tricks for your screenrc (with screenshots).

Joseph Kern
  • 9,809
  • 3
  • 31
  • 55
3

If you'd like to use screen in an unattended manner, use :

# screen -d -m script.sh

That would put your script in the background, detached! Now you can easily log out and continue what you were doing.

1

If you don't want to use screen, the only other option would be to send all output to a logfile, and run tail on the logfile to view progress. This assumes the job isn't interactive, which should probably be the case for any long running job anyway. If you need to stop the job prematurely, you can use ps to find the pid and just kill it.

Looking at your command above, you should just be able to check on the contents of nohup.out. Nohup isn't necessary however, you can start the job, run ^Z, then type 'disown' (in bash anyway - this will remove the job from the jobs list, but there is no way around this).

Mark
  • 2,846
  • 19
  • 13
0

You can combine screen and ssh into one with with this command:

ssh hostname -t screen -RD

Press Control-A Control-D to disconnect.

Amandasaurus
  • 30,211
  • 62
  • 184
  • 246