whether a shell script can be executed if another instance of the same script is already running -
i have shell script runs 10 mins single run,but need know if request running script comes while instance of script running already, whether new request need wait existing instance compplete or new instance started.
i need new instance must started whenever request available same script.
how it...
the shell script polling script looks file in directory , execute file.the execution of file takes 10 min or more.but during execution if new file arrives, has executed simultaneously.
the shell script below, , how modify execute multiple requests..
#!/bin/bash while [ 1 ]; newfiles=`find /afs/rch/usr8/fsptools/www/cgi-bin/upload/ -newer /afs/rch/usr$ touch /afs/rch/usr8/fsptools/www/cgi-bin/upload/.my_marker if [ -n "$newfiles" ]; echo "found files $newfiles" name2=`ls /afs/rch/usr8/fsptools/www/cgi-bin/upload/ -art |tail -n 2 |head $ echo " $name2 " mkdir -p -m 0755 /afs/rch/usr8/fsptools/www/dumpspace/$name2 name1="/afs/rch/usr8/fsptools/www/dumpspace/fipsdumputils/fipsdumputil -e -$ $name1 touch /afs/rch/usr8/fsptools/www/dumpspace/tempfiles/$name2 fi sleep 5 done
when writing scripts 1 describe, take 1 of 2 approaches.
first, can use pid file indicate second copy should not run. example:
#!/bin/sh pidfile=/var/run/$(0##*/).pid # remove pid if exit or terminated trap "rm -f $pidfile" 0 1 3 15 # write pid symlink if ! ln -s "pid=$$" "$pidfile"; echo "already running. exiting." >&2 exit 0 fi # stuff
i using symlinks store pid because writing symlink atomic operation; 2 processes can't conflict each other. don't need check existence of pid symlink, because failure of ln
indicates pid cannot set. that's either permission or path problem, or it's due symlink being there.
second option make possible .. nay, preferable .. not block additional instances, , instead configure whatever script permit multiple servers run @ same time on different queue entries. "single-queue-single-server" never "single-queue-multi-server". since haven't included code in question, have no way know whether approach useful you, here's explanatory meta bash:
#!/usr/bin/env bash workdir=/var/tmp # set better $workdir this. a=( $(get_list_of_queue_ids) ) # command? function? you. qid in "${a[@]}"; # set "lock" item .. or don't, , move on. if ! ln -s "pid=$$" $workdir/$qid.working; continue fi # stuff $qid. ... # , finally, clean after ourselves remove_qid_from_queue $qid rm $workdir/$qid.working done
the effect of transfer idea of "one @ time" handler data. if have multi-cpu system, have enough capacity handle multiple queue entries @ same time.
Comments
Post a Comment