kill the process

B

blackdog

I have a perl script, I like to kill it (commit suicide) if the script
is running on the system for more than one hour. What is the best way
to do it?
 
P

Paul Lalli

blackdog said:
I have a perl script, I like to kill it (commit suicide) if the script
is running on the system for more than one hour. What is the best way
to do it?

Are you saying you want the program to automatically exit if it's been
running for an hour? At the start of your program, get the current
time (see: perldoc -f time). Periodically in your code, presumably in
a main central loop of some kind, check the current time. If the two
times are more than 3600 seconds apart, exit (see: perldoc -f exit).
You may also wish to make use of the END { } block (see: perldoc
perlmod) for any cleanup code that you wish to happen.

If, on the other hand, you want some external process to periodically
check your Perl program to see if it's been running for an hour, then
your question is off topic for this group, as the question does not at
all depend on the language in which the hour-long script was written.

If, on a mysterious third hand, you want this external process which
periodically checks your Perl script to also be written in Perl, then
your question is back on topic here. You may wish to use the CPAN
module Proc::processTable to determine how long your program has been
running for, and then send it a signal (see: perldoc -f kill). Your
hour-long script would have to handle that signal, of course. (see:
perldoc perlipc and perldoc perlvar (searching for %SIG)).

Once you've made your attempt to solve your problem, if you cannot get
satisfactory results, feel free to post what you've tried here. Be
sure to read and follow the Posting Guidelines for this group, which
are posted here twice a week, to maximize your chances of getting good
help.

Paul Lalli
 
C

Charles DeRykus

blackdog said:
I have a perl script, I like to kill it (commit suicide) if the script
is running on the system for more than one hour. What is the best way
to do it?

Here's a possible Unix solution if you mean what I think you mean:

# near the top of your script
$SIG{ALRM} = sub { die 'internal timeout'; };
alarm(3600);

Of course, you should read all the caveats in the docs about
mixing sleep and alarm, stacking alarms, etc. Also, sometimes
signals may be lost, particularly across fork boundaries.

Hth,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top