My question:
Is there a way to lock a script if its already running for other users?
Like if i want to start a script, that is running by another user, there will be a message: Hey, you cant start the script because its running by another user, try it later.
my idea was that the script creates a tmp-file, what will be deleted at the end of the script. If everyone starts the script, there will be a scan: does the tmp-file exist, if yes: error message, if no: you can start the script.
The problem of my idea is that if the script has a crash, the tmp-file still exist and nobody can start the script anymore.
Do you have another, probably a better idea how i can resolve this problem?
2 possibilities:
trap various signals (eg, EOF, TERM, KILL and INT) and if they occur remove the lock file
or
Write your current PID to the lock file. On start, check if the file exists and if there is a process with that PID and if it's named the same as the script. If not, run, exit otherwise.
rev isn't required by posix, so it may not be on all systems.
By the way, just so you know, if it is very important that one and only one instance of the script be running at any instant, none of the above solutions will guarantee that. If the script is seldom run, you may never have a problem with them, but it is still possible for more than one instance to run.
thank you for the information.
is there no way to guarantee that nobody starts the script while it is running?
its a cloning script for databases, so it must not be startet twice.
the script has to be locked or something like that.
A quite safe mechanism for locking in a shell script is to create a locking directory. Call mkdir without the -p switch and if you succedd (exit code = 0) then your script can go on, or has to terminate otherwise.
But there still is the problem to remove the locking directory at program termination. If you are using ksh you can define a trap handler for the exit event like:
LOCKDIR=/tmp/mylock.dir
trap 'rm -rf $LOCKDIR' 0
if mkdir $LOCKDIR
then
# go on
else
print -u2 already running.
exit 1
fi
Thank you for your help!
The script works, if i start the script while the script is running, there is the right message "script is running". But if i start it a second time, the mylock.dir directory is removed-> so i can start it though the script is already running. how can i prevent that the script removes the mylock.dir?
Do I have to copy my whole code instead of the "go on" comment?