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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

perl process questions

Status
Not open for further replies.

dabits

Programmer
Apr 28, 2000
18
US
Hi! I created the following perl subroutine to prevent multiple occurrances of the same script from running. <br>I supply a maximum task count due to the subprocesses perl seems to create for things like globs and the actual grep task! <br>I now would like to kill any previous running occurrances of the script. Given the subprocesses perl creates, it there a best way to do this? For example, if&nbsp;&nbsp;I kill the main perl task, will the child processes die gracefully?<br>I know they will become defunct, but will they also be removed from the process table on Solaris? Or do I need to kill all the child processes first? <br>Any and all suggestions are welcome!<br>Thanks <br><br>sub process_ck{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;my $mcount = $_[0]; # max task count<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;my $parm = $0;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;chomp(my @procs = qx!ps -aux¦grep '$parm'!);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;my ($count, $element);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;foreach (@procs){<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (index($_, $parm) ne -1){<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$count++;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if ($count &gt; $mcount){<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;die &quot;Task $parm already running!\n&quot;;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>}<br>
 
Hi dabits,<br><br>Just running a Perl script doesn't create child processes - so unless you're explicitly calling fork() in your scripts you can just kill the Perl process.<br><br>A common way to make sure you're only running one copy of a process in Unix is to create a lock file - on startup check for the existence of a particular file, '/tmp/myprog.lock' for instance, if it's there your prog is already running. If it's not - create it and write your process ID (that's $$) to the file and close it. <p>Mike<br><a href=mailto:michael.j.lacey@ntlworld.com>michael.j.lacey@ntlworld.com</a><br><a href= Cargill's Corporate Web Site</a><br>
 
Don't kill your processes if you're using mod-perl!&nbsp;&nbsp;It keeps open a bunch of processes so that it doesn't have to reinitialize the perl interpreter every time, thus making things much faster.&nbsp;&nbsp;Otherwise, Perl generally cleans up after itself.<br><br>Sincerely, <br><br>Tom Anderson<br>CEO, Order amid Chaos, Inc.<br><A HREF=" TARGET="_new">
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top