Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save AlekseyDurachenko/2027114608e4863eb038 to your computer and use it in GitHub Desktop.
Save AlekseyDurachenko/2027114608e4863eb038 to your computer and use it in GitHub Desktop.
#!/bin/bash
# usage
if [ "$#" -ne 1 ]; then
echo "Usage:"
echo " run_script_with_lock-dbus_in_crontab.sh <command_or_script_name>"
exit 1
fi
# aliases
USERNAME=`whoami`
SCRIPT=`basename $1`
LOCKFILE=/tmp/$SCRIPT.lock
LOGFILE=/tmp/$SCRIPT.log
# without this line the python3-notify2 inside the crontab is not worked
export DBUS_SESSION_BUS_ADDRESS=`ps -u $USERNAME e | grep -Eo 'dbus-daemon.*address=unix:abstract=/tmp/dbus-[A-Za-z0-9]{10}' | tail -c35`
# script is already running
if [ -e ${LOCKFILE} ] && kill -0 `cat ${LOCKFILE}`; then
echo "$SCRIPT: already running"
exit
fi
# make sure the lockfile is removed when we exit and then claim it
trap "rm -f ${LOCKFILE}; exit" INT TERM EXIT
echo $$ > ${LOCKFILE}
# execute the script
echo ">>>>>>>>>>>>> " `date` " <<<<<<<<<<<<<" >> $LOGFILE
$1 >> $LOGFILE 2>&1
# remove lock
rm -f ${LOCKFILE}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment