Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/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}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement