CheeseConQueso
CheeseConQueso

Reputation: 6041

How do I keep a Perl script running on Unix after I log off?

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

Upvotes: 1

Views: 10828

Answers (5)

Zimbabao
Zimbabao

Reputation: 8240

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.

Upvotes: 1

Alex Reynolds
Alex Reynolds

Reputation: 96937

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.

Upvotes: 5

Mark Longair
Mark Longair

Reputation: 467181

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

Upvotes: 4

Laur Ivan
Laur Ivan

Reputation: 4177

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.

Upvotes: 1

Rob Agar
Rob Agar

Reputation: 12459

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

Upvotes: 12

Related Questions