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

I would like to set a script to exit gracefully after a specified time. Specific use case is a script would start at 11:30AM on day one and self terminate at 11:30AM the next day. I have tried looking at TIME::Piece, localtime, and similar but maybe I am overthinking this. Does anyone have any examples where this kind of logic was done? I would imagine this should be relatively simple where you get curtime as epoch, get tomorrow as epoch and routinely compare. Perhaps I can see the forest for the trees.
  • Comment on Self terminating a script after a specified time

Replies are listed 'Best First'.
Re: Self terminating a script after a specified time
by blindluke (Hermit) on Jan 07, 2015 at 19:14 UTC

    Look at the Time::Limit module. It terminates the script after a given time. In your case, the usage would be:

    use Time::Limit '86400.0'; # terminate after 86400 seconds (24h)

    Could be just the thing you need.

    - Luke

      Looking at this.. My concern is what the exit code would be on self termination. If non zero, the automated scheduling system will flag this as a job failure which is not the intention.

        Time::Limit sends the process a SIGTERM, which you can catch.The following exits with a zero exit code after 5 seconds:

        use Time::Limit -quiet, '5'; $SIG{TERM} = sub { exit 0 }; while(1) {}
Re: Self terminating a script after a specified time
by salva (Canon) on Jan 07, 2015 at 20:40 UTC

      Some notable things about alarm:

      Only one timer may be counting at once. Each call disables the previous timer

      It is usually a mistake to intermix alarm and sleep calls, because sleep may be internally implemented on your system with alarm.

      If you want to use alarm to time out a system call you need to use an eval/die pair. ... caveats given in Signals in perlipc.

      Portability issues: alarm in perlport.

Re: Self terminating a script after a specified time
by Anonymous Monk on Jan 07, 2015 at 19:20 UTC

    If this is *NIX, another way to do it might be to arrange for cron to call killall (or kill if you know the PID; e.g. your script could write it to a file at a known location) at 11:30 the next day, and set up a %SIG handler for that signal in your script for a graceful exit.

      If this is *NIX, be very, very careful before you type the word "killall" into your terminal session. Or arrange for cron to call it. On traditional UNIX systems, AIX in particular, it kills all processes except the one calling killall. Kills as in 'SIGKILL', not 'SIGTERM'. Be very, very careful.

      A safer way would be to set up an 'at' job calling 'kill' with the PID of the running process, at the beginning of the script. Or just use Time::Limit. :)

      - Luke

        Using at or cron is not an option. We are mandated to use the corporate approved scheduling system. The goal is to run the process right up until the next 'build' of the daily schedule. Failure to terminate gracefully prior to the next schedule build means I would have a growing number of the same process running which is less than ideal.