Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

how to automatically kill a program by itself?

by lightoverhead (Pilgrim)
on Feb 16, 2009 at 03:08 UTC ( [id://743976]=perlquestion: print w/replies, xml ) Need Help??

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

Dear monks, I would like to write a script which will automatically kill itself if it can not finish the job in a certain time,e.g. after running 2 hours? Could you let me know your methods? Thanks.
  • Comment on how to automatically kill a program by itself?

Replies are listed 'Best First'.
Re: how to automatically kill a program by itself?
by kennethk (Abbot) on Feb 16, 2009 at 03:12 UTC
Re: how to automatically kill a program by itself?
by GrandFather (Saint) on Feb 16, 2009 at 03:21 UTC

    Special variable $^T gives the epoch time when the application started executing. time - $^T gives the number of seconds the application has been running.


    True laziness is hard work
Re: how to automatically kill a program by itself?
by ikegami (Patriarch) on Feb 16, 2009 at 07:22 UTC

    You could also use a watchdog if you don't trust the application to end itself.

    watchdog -t 7200 script

    Update: Fixed fork error check.
    Update: Added command line options and moved code into its own (CUFP) node.

Re: how to automatically kill a program by itself?
by CountZero (Bishop) on Feb 16, 2009 at 06:04 UTC
    And of course exit is the function to call to end the program cleanly once its time has come.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Re: how to automatically kill a program by itself?
by Bloodnok (Vicar) on Feb 16, 2009 at 11:44 UTC
    You don't identify the type of script - if it were sh/ksh i.e. a half decent shell :-), I'd let the OS do the work ... something like
    at now + 2 hours "kill $$"

    A user level that continues to overstate my experience :-))
Re: how to automatically kill a program by itself?
by madhu.hanigadde (Novice) on Feb 16, 2009 at 10:27 UTC
    use localtime() like ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time); then compare the $hour with ur system time then in subrouten write this module sub kill { $pid=getpgrp(); ######## To get the Process ID system("kill $pid"); }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://743976]
Approved by GrandFather
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (3)
As of 2024-04-26 04:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found