Subj : Re: Remove Oldest Process Every Minute. How To? To : comp.os.linux From : nobody Date : Thu Aug 26 2004 11:23 pm Juul spewed this unto the Network: > I've build a picturegrabber in PHP that starts every minute from within a > shellscript that runs as a cronjob. > > The PHP-script needs about 5 minutes to complete and sometimes it will > hang. That's why I wrote a shellscript for it. > > Every minute a new session of the PHP-script starts. There can be more than > 1 process of the PHP-script active but not more than 6. So the old > instances can complete when a new instance of the PHP-script starts. When > the 6th process starts then all processes of the PHP-script would be killed > by the shellscript. This is my problem... > > By killing all the processes of the PHP-script it kills also the just > started processes and I actually want to kill only the oldest process when > the 6th process starts. How can I do this? Create a directory for your PHP scripts to put PID files in. In this directory, each PHP script will put a file that contains the PID in the name: You can then use the file-creation times to determine which PID is the oldest. The shell script might look like this: #!/bin/sh LS=/bin/ls GREP=/bin/grep WC=/usr/bin/wc PHP=/usr/bin/php AWK=/usr/bin/awk PATH=/local/www/data/cgi-bin HEAD=/usr/bin/head # If your PHP script fails to remove the PID file after a # normal (not killed by this script) termination, and # another process running as the same user gets the same # PID, then this script will accidentally kill the # wrong process. However, the stale .pid file will get # removed. while [ $($LS /local/www/data/pids/ | $WC -l) -ge 6 ]; do $OLDEST = $($LS -r --sort=time /local/www/data/pids | $HEAD -n 1) kill -9 $(basename $OLDEST .pid) rm -f $OLDEST done .