Also des Rätsels Lösung ist die Prozssnamen in beiden Fällen mittels ps zu ermitteln.
Die Lösung ist nun:
Code:
#!/bin/bash
# Testscript (testlock.sh) for locking with lockfile and several checks.
# Based on scripts from "Linux Server Hacks" and "Wicked Cool Shell Scripts".
# It checks if the lockfile a) contains a PID of a runnig process, b) if the
# name of that process is the name of this script ($0) and c) if the locking
# process has been started by the same user ($UID).
# With these checks it should work without problem.
# For locking from multiple users or scripts it has to be modified and renamed.
#
# This script MUST be run without the explicit shell (so NOT /bin/bash testlock.sh).
#
# License: GPL
# Author: verinder at pop.ms (Dr. R. F.)
# 2007-08-06
# Version 0.9
########## Lockfile Part #####################
# parameters
sleeptime="1" # sleeptime for creating the lockfile
retries="10" # default number of retries of creating the lockfile: 10 (should be > locktimeout*sleeptime)
locktimeout="5" # default timeout : 5 s. The lockfile will be removed
# by force after locktimeout seconds have passed since the lock-
# file was last modified/created. Lockfile is clock skew immune.
lockdir="/var/tmp"
this_process="$(ps -p $$ -o comm=)"
lockfile="$lockdir/lockfile.$this_process" # lockfile name
# ascertain whether we have lockf or lockfile system apps
check ()
{
if [ -z "$(which lockfile | grep -v '^no ')" ] ; then
echo "$0 failed: 'lockfile' utility not found in PATH." >&2
exit 1
fi
}
# make lockifle
lock ()
{
# check if a lockfile is present
if [ -f $lockfile ]; then
# check the PID in the lockfile
if kill -0 $(cat $lockfile) 2> /dev/null; then
echo "The locking executable with pid $(cat $lockfile) appears to be already running."
locking_process=$(ps -p $(cat $lockfile) -o comm=)
# check if the process with the found PID has the name of this skript (run this skript always without /bin/bash)
#if [ "$locking_process" == "$0" ] ; then
if [ "$locking_process" == $(ps -p $$ -o comm=) ] ; then
echo "The locking executable has the same name (without the path) as this script"
echo "$this_process"
echo "."
# check if the process with the found UID
if [ $(ps -p $(cat $lockfile) -o uid=) == $UID ] ; then
echo "The locking process has been created from the same user $UID which is running this script; exiting."
exit 1
else
echo "The locking process has been created from the different user"
echo $(ps -p $(cat $lockfile) -o uid=)
echo "; the user (UID) of this script is $UID."
# If you want to (try to) kill the blocking process, uncomment the following line.
kill -9 "$(cat $lockfile)"
# Maybe in the line befor fi you should send an email to root@localhost that a user tried (or maybe caused)
# a DOS attack and that the blocking process (here undocumented because already killed) was killed.
fi
else
echo "The locking executable"
echo "$locking_process"
echo "DOES NOT has the same name as this script,"
echo $(ps -p $$ -o comm=)
echo "."
echo "Removing that lockfile $lockfile."
rm -f "$lockfile"
fi
else
echo "The locking executable with pid $(cat $lockfile) has completed or was killed without cleaning up its lockfile"
echo "or the locking executable has another name than this script or it is run by an other user;"
echo "removing that lockfile $lockfile."
rm -f "$lockfile"
fi
fi
# create the lockfile; wait
if ! lockfile -$sleeptime -r $retries -l $locktimeout "$lockfile" 2> /dev/null; then
echo "$0: Failed: Couldn't create lockfile in time" >&2
exit 1
fi
chmod u+rw "$lockfile"
# store the pid
echo $$ > "$lockfile"
chmod u-wx "$lockfile"
# A trap to delete the lockfile when the script gets killed by SIGHUP SIGINT or SIGTERM.
# In many cases, e. g. a kernel hangup, this does not work and the checks above are necessary.
trap "rm $lockfile; exit" SIGHUP SIGINT SIGTERM
}
# cleanup
unlock ()
{
rm -f "$lockfile"
}
#################### "main" ##############################
echo "Start of main part at"
date | xargs echo
# variable i
typeset -i i=0
# lockfile: first check, then set for locking
check
lock
echo "lock set; waiting 20 seconds"
echo "now you, or another user can test with starting this script twice etc."
sleep 20
unlock
exit 0
Lesezeichen