How to stop an external process after a user defined time

Posted on 2004-09-22
Last Modified: 2008-01-09
I need to kick off an external process either with back-tics or the system command (please compare that to the exec() I actuall try below -- is it the same result?), allow it to run for 5, 10, 100, or some other user def. minutes, then kill it in order to restart the process with a different set of inputs.

I imagine something like the code below, but I admit I have not tried it yet, since I'm at home.  Questions are, is this the best way?  Is this likely to work?  Anyone have a better solution?  Running a process for x minutes, then killing it seems like a common thing, but I find no examples that target that.

"my_process" will run indefinitely if it's not stopped, so I must figure out how to stop it after "user-defined" minutes, and then I still need to be able to start a new iteration and handle it the same way, till I'm done with all input sets...

if (!defined ( $kidpid = fork() )) {
    # fork returned undef, so failed
    die "cannot fork: $!";
}  elsif ($kidpid == 0) {
                # fork returned 0, so this branch is the child
                # if the exec fails, fall through to the next statement
    die "can't exec my_process: $!";
}  else {
                # fork returned neither 0 nor undef,
                # so this branch is the parent
    kill 9, $kidpid
Question by:mark_at_acc
LVL 84

Assisted Solution

ozo earned 100 total points
ID: 12129993
Yes, your code should work.

Author Comment

ID: 12131612
Thanks ozo, I think it *should* work too, but I was hoping for more feedback, other ideas (maybe this is not the only solution), possible gotchas with the code, etc.

My experience with writing (for me, it was usually "maintaining" and "fixing") forking code in C was that it is rarely as simple as it looks at first...

Accepted Solution

knel1234 earned 300 total points
ID: 12133752

Are you trying to monitoring something (ie System Performance, Database, webserver)?  I have done a few thing for persistent processes.  Regardless of wether the process is for a database or not, if you can connect to a database create a 1 row table that contains start time, stop time, frequency etc.  You can use the same code to update this row etc etc.  If you do not have a database to utilize the just make a flat file with tokens for start time, stop time, frequency etc.  Regardless, you can simply have your program poll this table (or flatfile) to determine wether continued processing or termination of the process (pid) is required.  
On the otherhand, I have also feed the result of ps -ef | grep <process_name> to verify my process was running so that I would not create multiple instantiation of my process (ps -aux | grep <process_name if you are in that world).  You could do this or have another function (or a sybling process) to terminate the existing one.  For example, if the user starts you original process call a function to determine the execution status.  If the process is running, then terminate and restart the process.  If the process was not executing then simply start the process.  Furthermore, if you have this addition process (or function) then simply call it to terminate the process (pid).  
Lastly, I know the 'kill 9 pid#' concept but It can occasion cause problems from zombie to worse.  If you have your process reference a table or flatfile to terminate you will definately have a 'cleaner' ending of your process

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.


Author Comment

ID: 12134731
A.  I am in Win2000 land, cannot count on cygwin tools being available.

B. I am writing the script for a QA group, I am sticking to the requirements they gave:
   1.  they want to kick off my script with an imput file they will write, and a time like 60 minutes given at the command line.
   2.  My script starts their exe file, feeding it item 1 from their input file, run it for 60 minutes, kill it.
   3.  restart the exe, feeding it item 2 from their input file, run for 60 min, then kill it.
   4.  If no more entries are available from the input file, quit.
   5. log some data along the way.

So, it's length of time that matters, not time of day. a QA Rep will be happy to manually start the process, it's the repeated changes in input and the restart they need automated.  they also don't like to guess when they should stop the process, because they need benchmark data.

Author Comment

ID: 12143622
Actually I have now tested my fork and exec.

The problem now is $kidpid contain a negative value like -1416, and the process it tries to kill has a pid in task manager like 1724.  So, the process doesn't get killed.

The rest of my code seems to work fine, except for the kill.  Any ideas?

Assisted Solution

CmdrRickHunter earned 100 total points
ID: 12153715
I cannot speak for the perl interface with windows, but cygwin has to do some funky stuff to make pids work.
Cygwin PIDs are not the same as taskmanager pids.  C has functions to convert one into the other, hopefully perl has similar.

Look into cygwin-specific perl modules?  Anyone else want to pick it up from here?

Author Comment

ID: 12184022
Win32::process is what I went with, I'll assign points, thanks.

Author Comment

ID: 12184078
I gave knel1234 most points based on his volume of feedback, but no one really gave me the help I was hoping for.  Oh well, can't win em all, this question must have been too specialized in an area people usually don't have to code in.  Thanks for the feedback!  Mark

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (,  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question