run-crons-0.3.1 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #!/bin/bash
  2. #
  3. # Mostly copied from SuSE and Gentoo
  4. #
  5. # this script looks into /etc/cron.[hourly|daily|weekly|monthly]
  6. # for scripts to be executed. The info about last run is stored in
  7. # /var/spool/cron/lastrun
  8. LOCKDIR=/var/spool/cron/lastrun
  9. LOCKFILE=${LOCKDIR}/lock
  10. mkdir -p ${LOCKDIR}
  11. # Make sure we're not running multiple instances at once.
  12. # Try twice to lock, otherwise give up.
  13. for ((i = 0; i < 2; i = i + 1)); do
  14. ln -sn $$ ${LOCKFILE} 2>/dev/null && break
  15. # lock failed, check for a running process.
  16. # handle both old- and new-style locking.
  17. cronpid=$(readlink ${LOCKFILE} 2>/dev/null) ||
  18. cronpid=$(cat ${LOCKFILE} 2>/dev/null) ||
  19. continue # lockfile disappeared? try again
  20. # better than kill -0 because we can verify that it's really
  21. # another run-crons process
  22. if [[ $(</proc/${cronpid}/cmdline) == $(</proc/$$/cmdline) ]] 2>/dev/null; then
  23. # whoa, another process is really running
  24. exit 0
  25. else
  26. rm -f ${LOCKFILE}
  27. fi
  28. done
  29. # Check to make sure locking was successful
  30. if [[ ! -L ${LOCKFILE} ]]; then
  31. echo "Can't create or read existing ${LOCKFILE}, giving up"
  32. exit 1
  33. fi
  34. # Set a trap to remove the lockfile when we're finished
  35. trap "rm -f ${LOCKFILE}" 0 1 2 3 15
  36. for BASE in hourly daily weekly monthly
  37. do
  38. CRONDIR=/etc/cron.${BASE}
  39. test -d $CRONDIR || continue
  40. if [ -e ${LOCKDIR}/cron.$BASE ]
  41. then
  42. case $BASE in
  43. hourly)
  44. #>= 1 hour, 5 min -=> +65 min
  45. TIME="-cmin +65" ;;
  46. daily)
  47. #>= 1 day, 5 min -=> +1445 min
  48. TIME="-cmin +1445" ;;
  49. weekly)
  50. #>= 1 week, 5 min -=> +10085 min
  51. TIME="-cmin +10085" ;;
  52. monthly)
  53. #>= 31 days, 5 min -=> +44645 min
  54. TIME="-cmin +44645" ;;
  55. esac
  56. find ${LOCKDIR} -name cron.$BASE $TIME -exec rm {} \;
  57. fi
  58. # if there is no touch file, make one then run the scripts
  59. if [ ! -e ${LOCKDIR}/cron.$BASE ]
  60. then
  61. touch ${LOCKDIR}/cron.$BASE
  62. set +e
  63. for SCRIPT in $CRONDIR/*
  64. do
  65. if [[ -x $SCRIPT && ! -d $SCRIPT ]]; then
  66. $SCRIPT
  67. fi
  68. done
  69. fi
  70. done
  71. # Clean out bogus cron.$BASE files with future times
  72. touch ${LOCKDIR}
  73. find ${LOCKDIR} -newer ${LOCKDIR} -exec /bin/rm -f {} \;