Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I have a script that I needs to be continously run from 9am to 4pm. I know that I can start the script running at 9am using crontab but I'm not sure how to stop it at 4pm? Is there a similar command in crontab that would kill a script at a certain time?

Thanks.

Replies are listed 'Best First'.
Re: Kill a script at a certain time
by Tomte (Priest) on Feb 17, 2003 at 16:37 UTC

    I'd write a crontoling controling script to start stop and monitor the script.

    #/bin/sh MY_BIN=/path/to/my/prog MY_PID=/var/run/my.pid if [ ! -x $MY_BIN ] ; then echo -n "My Program not installed ! " exit 5 fi case "$1" in start) echo -n "Starting my program " /sbin/checkproc -p $MY_PID $MY_BIN if [ $? -eq 0 ] ; then echo -n "- Warning: my program already running ! " else [ -e $MY_PID ] && echo -n "- Warning: $MY_PID exists ! " fi /sbin/startproc -p $MY_PID $MY_BIN ;; stop) echo -n "Shutting down my program " /sbin/checkproc -p $MY_PID $MY_BIN [ $? -ne 0 ] && echo -n "- Warning: my program not running ! " /sbin/killproc -p $MY_PID -TERM $MY_BIN ;; status) /sbin/checkproc -p $MY_PID $MY_BIN [ $? - ne 0 ] && echo "my program not running! help" ;; *) echo "usage" exit 1; ;; esac exit 0;

    Just have a look at one not so big /etc/init.d file (or /etc/rc.d or whatever your unix has) and adapt it to your needs, this one should work on any fairly recent linux-box (taken and adapted from a SuSE box (I'm at work, don't blame me ;))

    With something like this, you can easily control the script from cron.
    You have to change it so that it to only echos things if something went wrong, cause cron will spam you mad if you don't. If you are not alone interested in the results, consider a mail-wrapper

    regards,
    tomte


    Edit:added missing ;; in default case

Re: Kill a script at a certain time
by physi (Friar) on Feb 17, 2003 at 17:06 UTC
    Why not use alarm ?
    SIG{ALRM} = /&exit; alarm(7*60*60);

    this should send an ALRM SIG and exit your application after 25200 seconds, or at 4pm, aka 16:00 ;-)

    -----------------------------------
    --the good, the bad and the physi--
    -----------------------------------
    
Re: Kill a script at a certain time
by Joost (Canon) on Feb 17, 2003 at 16:31 UTC
    You could kill the script with killall scriptname in your crontab (or in a shell script).

    Or you could try something like this in your script (assuming it is a perl script):

    my $top_at_hour = 4; while ((localtime(time))[2] < $stop_at_hour) { # do some specific stuff here.. } exit;
    Ps: I never remember if 4pm == 04.00 hr or 16.00 hr.
    -- Joost downtime n. The period during which a system is error-free and immune from user input.

      Be very careful about suggesting 'killall' as a solution to anything unless you are sure what platform they are running on. On linux killall means 'kill all the processes with this name', on Solaris killall means 'kill every process on the machine in preparation for shutdown'. Obviously if you run it on Solaris and want the Linux behavior, you will be disappointed. :)

      P.S. 4pm is 1600 hours

        Thanks for the tip. I'm glad I never used killall in the scripts I wrote for solaris. :-)
        -- Joost downtime n. The period during which a system is error-free and immune from user input.
Re: Kill Script
by zengargoyle (Deacon) on Feb 17, 2003 at 16:46 UTC

    assuming your script goes into some sort of work loop you can check how long you've been running and exit if needed.

    ... my $stop_time = time + 60 * 60 * 7; # now + 7 hours while (1) { do_work; last if time > $stop_time; } ...

    your script will stop the first chance it gets.

    if you need immediate gratification the script will need to write a pidfile so you'll know which script to kill. (if there's only ever a single instance of your script running you can get away with the 'killall' solution.)

    another alternative would be something like this...

    ... while (1) { do_work; if (-f '/tmp/script.die.die.die') { unlink '/tmp/script.die.die.die'; last; } ...

    then you could have a crontab entry at 4p.m that just does a 'touch /tmp/script.die.die.die' and again your script would stop as soon as possible.

Re: Kill a script at a certain time
by steves (Curate) on Feb 17, 2003 at 16:39 UTC

    You could have another job come up and kill it, or it could just check the time and stop itself, something like this:

    my $hour = (localtime)[2]; exit if ($hour >= 16);

    If you have another job kill it you'll either need to create a PID file, using something like Proc::PID::File or find the PID by parsing ps output ... assuming you're on a UNIX-like system since you mention using cron.

Re: Kill a script at a certain time
by Anonymous Monk on Feb 17, 2003 at 21:55 UTC
    Write another script to run at 4PM search for the PID of your previous cron job and kill it.Simple !!
Re: Kill a script at a certain time
by Anonymous Monk on Feb 18, 2003 at 15:53 UTC
    One way of achieving what you want is this. Make the script write its PID to a file which is be to read by a second script to send a "kill -9 PID". The killer script, which can be launched by crontab, can simply do this:

    kill -9 `cat script.pid`

    Djoao.

Re: Kill a script at a certain time
by Starky (Chaplain) on Feb 18, 2003 at 20:41 UTC
    If your script is named script.pl, you can put something like the following in your crontab:
    00 16 * * * kill `ps -auxww | grep script.pl | grep -v grep | perl -an +e 'print "$F[1] "'`
    (I couldn't help but slip some Perl in there ;-)

    Obviously, you may need to customize it to whatever environment you're using (e.g., `ps -ef` if you are using HP-UX etc.) and whatever other contingencies you may anticipate (e.g., you'll note that in its current incarnation, if file script.pl appears in the argument list of the call to your editor, your editor will be killed as well every afternoon at 4:00pm). But that's the basic idea.

    Hope this helps!