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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
11-04-2010, 05:04 AM
|
#1
|
|
LQ Newbie
Registered: Apr 2010
Posts: 29
Rep:
|
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.
|
|
|
|
11-04-2010, 05:23 AM
|
#2
|
|
Senior Member
Registered: Jan 2003
Posts: 2,786
|
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.
|
|
|
|
11-04-2010, 06:00 AM
|
#3
|
|
LQ Newbie
Registered: Apr 2010
Posts: 29
Original Poster
Rep:
|
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.
|
|
|
|
11-04-2010, 08:56 AM
|
#4
|
|
LQ 5k Club
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian Squeeze (server), Slackware 13.37 (netbook), Slackware64 14.0 (desktop),
Posts: 8,357
|
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.
|
11-05-2010, 04:12 AM
|
#5
|
|
LQ Newbie
Registered: Apr 2010
Posts: 29
Original Poster
Rep:
|
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..
|
|
|
|
11-05-2010, 04:13 AM
|
#6
|
|
LQ 5k Club
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian Squeeze (server), Slackware 13.37 (netbook), Slackware64 14.0 (desktop),
Posts: 8,357
|
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.
|
|
|
|
11-05-2010, 05:16 AM
|
#7
|
|
Senior Member
Registered: May 2005
Posts: 4,392
|
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.
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 08:40 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|