Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Question about using kill to stop a process.

Status
Not open for further replies.

kungphu

Programmer
Joined
May 23, 2006
Messages
13
Location
GB
Hi, perl newbie here. I need some help with how to use "kill" properly in perl. I have looked at other forum posts but I wasn't to sure about how to use them in my program.

I have an array called @piddatta which reads in active process numbers from a pid file. I am using a loop in my main program to run through the array and then kill off a process if it meets a certain criteria. I have a variable called $i which i increment as the program is exectuted which is then used to reference the array.

Code:
my $i = 0;
@piddata = <FILE>; # the file opens correctly i tested it with  
                   # a print statement.

Now if i want to kill off a process, is it right to presume it to be
Code:
kill $piddata[$i];

Im hoping that the above would then look at element equal to $1 in the array and then kill that proceses stored in that array number.

Is this a viable sollution to the problem?

 
If you are using a for loop and traversing the array, $piddata[$i] will be the ith element of the array.
So it seems ok to me.

--------------------------------------------------------------------------
I never set a goal because u never know whats going to happen tommorow.
 
On the face of it, it looks OK, but it will depend on where and how you increment $i. You probably don't need to use an index number, you can do something like this:

Code:
foreach my $pids (@piddata) {
   if ($pid <some condition here>) {
      kill $pid;
   }
}

make sure you understand the difference between the perl kill function on the shells kill function, I believe they work a bit differently.
 
Thanks for the response guys, either way works fine
 
What I have done is create a class which looks for a state file. All the apps use this class at an appropriate time where it is safe to exit.

If the state file is not present, everything continues to run.

Put a state file out there and entries are interogated whether to stop or not.

Note: I always use the # as a comment field so that it is
easy to comment out a line.

<contents of file>
##########################################################
# #
# FTP control
# Presence in \Public directory will control
# ftp process
###########################################################
FTPSleep=300
FTPStop=Mongoose
FTPStop=Ferret
FTPStop=LogServer
FTPStop=Service_starter
FTPStop=1237
###FTPPause=ALL
 
If we had processed line 2346 and killed it off. It would still remain in the pid file. How would I go about removing it from the pid file. When I initially load the pid file, I load its values into an array and then close it, like so
Code:
open(FILE, "geturl.pid") or die ("unable to find file"); 
@piddata = <FILE>;
close(FILE);
;

I presume I would just close the file at a later stage so we can leave it opened for editing. How would i use the array data values to remove the value from the pid file?


Here is what the pid file looks like
Code:
2345
2346
2347
....
....
2350

 
I have managed to sort the problem out myself. I simply slice the array at index i with an length of 1. And then i output the array to the same file i originally read the values in from, using the code below.



Code:
open(OUT,">pidfile.pid") || die "Cannot oben file for output\n";
foreach $line (@array){
    print OUT @line;
}
close OUT;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top