LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 11-06-2007, 09:09 PM   #1
JustinK101
Member
 
Registered: Jan 2007
Posts: 55

Rep: Reputation: 15
Limit A Process Cron Execution Time


So I have a naughty perl script which runs via cron every 10 minutes. After a few days of uptime on server, for some reason the process doesnt seem to die and the perl process eats all my CPU and doesnt close.

I am wondering if there is a parm you can pass to the perl program which limits execution to say 30 seconds and then automatically kills the process? If not through perl, how about through cron tab, again limit to 30 seconds and after that time automatically kill the perl process.

Thanks for the help.
 
Old 11-06-2007, 11:40 PM   #2
checkmate3001
Member
 
Registered: Sep 2007
Location: Folsom, California
Distribution: Ubuntu, Mint, Debian, Suse
Posts: 307

Rep: Reputation: 32
You could create another script and tell it to run every 10 mins (like your perl script) and then have it wait 30 secs - check to see if the perl script is running (vi ps or something) and then kill it if it is still running.

Just an idea.
 
Old 11-08-2007, 06:55 PM   #3
JustinK101
Member
 
Registered: Jan 2007
Posts: 55

Original Poster
Rep: Reputation: 15
checkmate3001,

So how would this script look? Currently perl is being naughty and has stuck open. How do I check if the process is running? Do I just check for perl? How do I know I am not killing other perl processes that are running, but are not the naughty processes?

I suppose I could run it under a custom username (perlCron) in cron, then I check for perl running under user (perlCron), if it is running then kill PID.

Could you write a script which does what I explained above? I not good at scripting.

Thanks.

Last edited by JustinK101; 11-08-2007 at 06:56 PM.
 
Old 11-17-2007, 02:05 AM   #4
checkmate3001
Member
 
Registered: Sep 2007
Location: Folsom, California
Distribution: Ubuntu, Mint, Debian, Suse
Posts: 307

Rep: Reputation: 32
Sorry JustinK101 - I've been away for awhile.

It would take me too long to write. I'm not that good at scripts either.
 
Old 11-17-2007, 03:01 AM   #5
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Say your cronjob spawns a process with the commandline "/usr/sbin/larch.pl /etc/trees.conf", then you could add a cronjob like this:
Code:
#!/bin/sh
# Sleep for 30 seconds.
sleep 30s
# Find process by commandline and -KILL it.
pkill -9 -f "/usr/sbin/larch.pl /etc/trees.conf"
# Exit the shellscript the right way.
exit 0
It should be scheduled to run at the same interval as your other cronjob.
 
Old 11-19-2007, 11:44 AM   #6
JustinK101
Member
 
Registered: Jan 2007
Posts: 55

Original Poster
Rep: Reputation: 15
UnSpawn,

Thank you very much, I tweaked your scipt a little bit since when I first tried it, it did not kill the processes. Basically I run the perl process under its own username in the crontab, perlCron. So the script now looks like:

Code:
#!/bin/sh
sleep 60s
pkill -9 -u perlCron perl
exit 0
 
Old 11-19-2007, 12:13 PM   #7
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
OK, thanks for posting the feedback.
 
Old 11-20-2007, 01:23 AM   #8
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,360

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
try
man ulimit
also, you might want to try posting the problem script (or a simplified ver) to the prog forum
 
  


Reply



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
How to limit program's execution time? xlq Linux - General 3 05-02-2007 09:40 AM
Change Execution Time for cron.* Rednarb Ubuntu 2 03-27-2007 03:27 PM
retrieving execution time for process dipenchaudhary Programming 3 02-24-2006 10:27 AM
randomizing execution of some cron entries markus1982 Linux - General 2 06-28-2003 09:49 AM
killing process at time limit hfawzy Linux - General 0 05-31-2003 03:46 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

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

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration