LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 11-04-2010, 05:04 AM   #1
smc2
LQ Newbie
 
Registered: Apr 2010
Posts: 29

Rep: Reputation: 0
Perl:Suicide if take long


Hi..
Do you have any idea how I may kill my own Perl script if it is running for more than threshold minutes?



Thanks.
 
Old 11-04-2010, 05:23 AM   #2
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 369Reputation: 369Reputation: 369Reputation: 369
Does the system you're using have the at command? If so, have your Perl script execute an appropriate "at" command immediately after the script starts. You use "at" to launch a another script that kills the original Perl script if it's still running after X minutes/hours/days.
 
Old 11-04-2010, 06:00 AM   #3
smc2
LQ Newbie
 
Registered: Apr 2010
Posts: 29

Original Poster
Rep: Reputation: 0
Dear Dark_Helmet,

I use cronetab to run my script every minute, therefore
I want to be sure that it is closed after for example 30 second so that at each minutes just one instance of the script is running..

Thanks for your attention.
 
Old 11-04-2010, 08:56 AM   #4
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,551
Blog Entries: 28

Rep: Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176
How about a bash wrapper script?
Code:
#!/bin/bash

/path-to-perl-script &
sleep 30
kill <signal as required> $!
 
1 members found this post helpful.
Old 11-05-2010, 04:12 AM   #5
smc2
LQ Newbie
 
Registered: Apr 2010
Posts: 29

Original Poster
Rep: Reputation: 0
Wise and nice script, for sure it may help, but do you have any idea how to do it within shell script that makes the script be killed independent of OS it is running on?

Thanks..
 
Old 11-05-2010, 04:13 AM   #6
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,551
Blog Entries: 28

Rep: Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176
Sorry -- I don't perl

EDIT:

But surely perl can do it -- it's a powerful language. In case it helps, I'd do it like this in bash
Code:
#!/bin/bash

( sleep 30 ; kill <appropriate signal> $$ )&

<rest of script>
The concept is to start a backgrounded sub-process that will kill the parent process after 30 seconds. I'm not certain without experimenting whether the background process will be killed when the parent exits; it might be necessary for the parent to kill the sub-process before it itself exits (which would require the excellent programming discipline of a single function to do the exit ).

Last edited by catkin; 11-05-2010 at 04:24 AM.
 
Old 11-05-2010, 05:16 AM   #7
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453
Why shouldn't the Perl script in question just kill itself ?

For that you can use threads.

I.e. use threads (or forks; threads work on any system), create a thread which just waits for the given timeout to expire, and when it expires, it calls built-in Perl 'kill' function (see perldoc -f kill) with PID of the script (the $$ variable).

The other thread will be the script proper itself, i.e. it will do the useful job.

Read about threads here: http://perldoc.perl.org/threads.html .

And about forks: http://search.cpan.org/~rybskej/forks-0.34/lib/forks.pm .

If you want cross-platform, as I said, you need threads and not forks.
 
  


Reply

Tags
perl, runtime


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
perl getopt::long help kdelover Programming 4 11-23-2009 02:27 PM
Have you considered suicide? mitchell7man General 75 05-31-2009 07:19 AM
When harddrives almost commit suicide. squall14716 Linux - General 8 05-29-2004 05:53 PM
Suicide by Redhat ravenswood1000 Linux - Software 10 03-27-2003 05:39 AM
Samba Suicide! dbCooper Linux - Networking 1 01-16-2002 07:19 AM


All times are GMT -5. The time now is 12:45 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration