Advertisement
Guest User

Untitled

a guest
Apr 27th, 2015
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.85 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # usage
  4. if [ "$#" -ne 1 ]; then
  5. echo "Usage:"
  6. echo " run_script_with_lock-dbus_in_crontab.sh <command_or_script_name>"
  7. exit 1
  8. fi
  9.  
  10. # aliases
  11. USERNAME=`whoami`
  12. SCRIPT=`basename $1`
  13. LOCKFILE=/tmp/$SCRIPT.lock
  14. LOGFILE=/tmp/$SCRIPT.log
  15.  
  16. # without this line the python3-notify2 inside the crontab is not worked
  17. 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`
  18.  
  19. # script is already running
  20. if [ -e ${LOCKFILE} ] && kill -0 `cat ${LOCKFILE}`; then
  21. echo "$SCRIPT: already running"
  22. exit
  23. fi
  24.  
  25. # make sure the lockfile is removed when we exit and then claim it
  26. trap "rm -f ${LOCKFILE}; exit" INT TERM EXIT
  27. echo $$ > ${LOCKFILE}
  28.  
  29. # execute the script
  30. echo ">>>>>>>>>>>>> " `date` " <<<<<<<<<<<<<" >> $LOGFILE
  31. $1 >> $LOGFILE 2>&1
  32.  
  33. # remove lock
  34. rm -f ${LOCKFILE}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement