# getopts
while getopts 'c:d:' OPTION
do
case $OPTION in
c) client=$OPTARG
;;
d) date=$OPTARG
;;
?) printf "Usage: %s: [-c client] [-d YYYY-mm-dd]\n" $(basename $0) >&2
exit 2
;;
esac
done
shift $(($OPTIND - 1))
mardi 17 mai 2011
usage de getopts
mardi 10 mai 2011
empecher 2 scripts de rouler en même temps
# lets make sure only one instance is running
EXEC=$(/bin/basename $0)
RES=$(/usr/bin/pgrep ${EXEC} | wc -l)
if [ ${RES} != 2 ]
then
echo "WARNING : ${EXEC} already running, exiting.";
exit;
fi
Il y a aussi :
#!/bin/sh
ME=`basename "$0"`;
LCK="/tmp/${ME}.LCK";
exec 8>$LCK;
flock --nonblock --exclusive 8
if [ $? = 0 ];
then
echo "Do job!!!"
sleep 20
echo "Jobs done"
fi
et aussi :
#!/bin/sh
IAM=(`pgrep -d " " -f ${0//*\//}`)
if [ ${#IAM[@]} -gt 1 ]
then
echo "Exiting because another instance of `basename $0` is already running with PID [$IAM]";
exit;
else
echo "Running now with PID [$$]"
sleep 10
fi
et aussi :
# check for existing lockfile
if [ -e "$LOCKFILE" ]; then
# lockfile exists
if [ ! -r "$LOCKFILE" ]; then
echo error: lockfile is not readable
exit 1
fi
PID=`cat "$LOCKFILE"`
kill -0 "$PID" 2>/dev/null
if [ $? == 0 ]; then
echo error: existing instance of this task is already running
exit 1
fi
# process that created lockfile is no longer running - delete lockfile
echo "process that created lockfile is no longer running - delete lockfile"
rm -f "$LOCKFILE"
if [ $? != 0 ]; then
echo "error: failed to delete lockfile"
exit 1
fi
fi
# create lockfile
echo $$ >"$LOCKFILE"
if [ $? != 0 ]; then
# error: failed to create lockfile
exit 1
fi
# -----
echo 'Simulated processing...sleeping 15secs'
sleep 15
# -----
# delete lockfile
rm -f "$LOCKFILE"
if [ $? != 0 ]; then
# error: failed to delete lockfile
exit 1
fi
autre méthode encore :
critical_section(){
echo "we are alone"
caller 0
sleep 15
}
lockfile=`basename $0`.`echo "$@" | sed "s/[\/\ \'\"]/_/g"`.lck
echo ${lockfile}
if ( set -o noclobber; echo "$$" > "$lockfile") 2> /dev/null;
then
# trap 'rm -f "$lockfile"; exit $?' INT TERM EXIT
critical_section
rm -f "$lockfile"
# trap - INT TERM EXIT
else
echo "Failed to acquire lockfile: $lockfile."
echo "Held by $(cat $lockfile)"
echo "Attempting to unlock"
# The file exists so read the PID
# to see if it is still running
MYPID=`head -n 1 $LCK_FILE`
echo "[$MYPID]"
if [ -n "`ps -p ${MYPID} | grep ${MYPID}`" ];
then
echo `basename $0` is still running [$MYPID].
exit
else
echo "rmfing lckfile..."
rm -f "$lockfile"
fi
fi
Inscription à :
Articles (Atom)