ErMeglio DD-WRT User
Joined: 11 Jul 2006 Posts: 104
|
Posted: Mon Sep 11, 2017 15:46 Post subject: Manage locks from shell scripts on DD-WRT, or flock() |
|
Hi there,
Trying to port scripts from Tomato to DD-WRT, the flock() lines that double check the scripts meant to be running at all times are not being re-run twice, trice, or worse, are not working. Basically, flock is not recognised on DD-WRT for R7800 - specifically Kong's build 33010M.
The /var/run/<script_name>.pid file is being created so, evidently, there's a corresponding flock() function on DD-WRT, however, how do you go about checking whether the script you're attempting to run is already running or not?
Further info
For those who don't know what flock is have a go at https://linux.die.net/man/1/flock . While developing the scripts, I found it a huge gift, releasing tons of headaches of scripts running simultaneously and breaking things up!
An example of its use would be:
Code: |
(
# Check for the lock on ${LOCK_FILE} (fd 300) or exit
flock -xn 300 || {
logger -t dev_check.stop "${SCRIPT_NAME} Script had FLock. Aborted this instance."
${TTS_SCRIPT} "Connections check had Flock. Instance aborted." &
exit 1
}
#No lock, OK, let's go on
echo $$ 1>&300 #sets the lockfile
trap cleanup INT TERM EXIT QUIT KILL # call cleanup() if script exits
cleanup() {
logger -t dev_check.trap "${SCRIPT_NAME} Script closed."
${TTS_SCRIPT} "Connections check is being aborted. Be warned!" &
flock -u 300
rm -f "${LOCK_FILE}"
exit 0
}
### ****
### Your
### code
### goes
### here
### ****
) 300>"${LOCK_FILE}"
|
|
|