In the script to update our debian mirror, we use this code snippet
under debian sarge:
=======================================================
# Define lock name, check if another rsync is in progress
LOCK_NAME="${DEST_DIR}/Mirror-Update-in-Progress-${HOSTNAME}"
if [ -f "${LOCK_NAME}.lock" ]; then
echo "rsync on ${HOSTNAME} is unable to start: ${LOCK_NAME}"
exit 1
fi
# Set up lock file
/usr/bin/lockfile-create "${LOCK_NAME}"
/usr/bin/lockfile-touch "${LOCK_NAME}" &
LOCKFILE_PID="$!"
# Note: on some non-Debian systems, trap doesn't accept "exit" as signal
# specification. If that's the case on your system, try using "0".
trap "/usr/bin/lockfile-remove ${LOCK_NAME}" exit
<here go your update/work actions>
# Kill lock process, delete lockfile
[ -d "/proc/${LOCKFILE_PID}" ] && kill "${LOCKFILE_PID}"
[ -f "${LOCK_NAME}.lock" ] && /usr/bin/lockfile-remove "${LOCK_NAME}"
========================================================
In debian, package "lockfile-progs" provides the file locking programs.
HTH, Hendrik Schaink
Roy Souther wrote:
> Is there anyway to do file locking in a bash script? I need to remove
> the race hazard from a script that has multiple running instances per
> user, each trying to make changes to a text file. I need to get
> exclusive access to a file and make the other instances wait their turn.
>
> Any ideas? Flock is supported by more advances script engines like Perl
> but flock seems to be beyond the ability of a simple shell like bash.
_______________________________________________
clug-talk mailing list
[email protected]
http://clug.ca/mailman/listinfo/clug-talk_clug.ca
Mailing List Guidelines (http://clug.ca/ml_guidelines.php)
**Please remove these lines when replying