Can you use something similar for lock files?
For instance, I have some script which looks like this:
function cleanup() {
pkill -P $$ # Kill child processes.
rm -f myscript.lock
}
trap cleanup exit
lockfile -r 0 myscript.lock # Try to create lock file once.
if [[ $? != 0 ]]; then
echo ":("
exit 1
fi
# Work work...
cleanup
Like you said, the cleanup handler is not invoked if the script receives a SIGKILL.
Since the lock file needs to be present in the FS (contrary to a tmp file), maybe this is some inherent limitation, but I would be curious if some better approach exists.