1

I have a script that takes a lot of time to complete.

Instead of waiting for it to finish, I'd rather just log out and retrieve its output later on.

I've tried;

at -m -t 03030205 -f /path/to/./thescript.pl

nohup /path/to/./thescript.pl &

And I have also verified that the processes actually exist with ps and at -l depending on which scheduling syntax i used.

Both these processes die when I exit out of the shell. Is there a way to keep a script from terminating when I close the connection?

We have crons here and they are set up and are working properly, but I would like to use at or nohup for single-use scripts.

Is there something wrong with my syntax? Are there any other methods to producing the desired outcome?


EDIT:
I cannot use screen or disown - they aren't installed in my HP Unix setup and i am not in the position to install them either
darch
  • 4,200
  • 1
  • 20
  • 23
CheeseConQueso
  • 5,831
  • 29
  • 93
  • 126

5 Answers5

12

Use screen. It creates a terminal which keeps going when you log out. When you log back in you can switch back to it.

Rob Agar
  • 12,337
  • 5
  • 48
  • 63
  • 2
    You can't go wrong with screen. Take a few minutes to learn and get familiar with it. It will be worth your time down the line. – holygeek Mar 04 '11 at 07:35
  • GNU `screen` is awesome for lots of reasons, including this. Definitely recommend it. – Alex Reynolds Mar 04 '11 at 09:36
  • i dont think i have screen - theres no man doc for it and im in no position to install it unless it could be done on a local level. thanks for the reading material though – CheeseConQueso Mar 05 '11 at 00:39
5

If you want to keep a process running after you log out:

disown -h <pid>

is a useful bash built-in. Unlike nohup, you can run disown on an already-running process.

First, stop your job with control-Z, get the pid from ps (or use echo $!), use bg to send it to the background, then use disown with the -h flag.

Don't forget to background your job or it will be killed when you logout.

Community
  • 1
  • 1
Alex Reynolds
  • 95,983
  • 54
  • 240
  • 345
4

This is just a guess, but something I've seen with some versions of ssh and nohup: if you've logged in with ssh then you may need to need to redirect stdout, stderr and stdin to avoid having the session hang when you exit. (One of those may still be attached to the terminal.) I would try:

nohup /path/to/./thescript.pl > whatever.stdout 2> whatever.stderr < /dev/null &

(This is no longer the case with my current versions of ssh and nohup - the latter redirects them if it detects that any is attached to a terminal - but you may be using different versions.)

Mark Longair
  • 446,582
  • 72
  • 411
  • 327
1

Syntax for nohup looks ok, but your account may not allow for processes to run after logout. Also, try redirecting the stdout/stderr to a log file or /dev/null.

Laur Ivan
  • 4,117
  • 3
  • 38
  • 62
  • thanks for the info... the script, by nature, directs all output to a `.csv` file so its not that important (as far as i know) to redirect to anything else – CheeseConQueso Mar 05 '11 at 00:48
1

Run your command in background.

/path/to/./thescript.pl &

To get lits of your background jobs

   jobs

Now you can selectively disown any of the above jobs, with its jobid.

disown <jobid>

All the disowned process should be keep on running even after you logged out.

Francisco R
  • 4,032
  • 1
  • 22
  • 37
Zimbabao
  • 8,150
  • 3
  • 29
  • 36