runit_cron 916 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. # vim: ft=sh
  2. # env config:
  3. # * SLEEP: time in seconds to sleep after main action
  4. # default: 1080
  5. #
  6. # * SERVICE: service name to be displayed as sleep argv0
  7. # default: "${PWD##*/}"
  8. #
  9. # * STATE_FILE: path to file that indicate that main action completed
  10. # default: "${TMPDIR:-/tmp}/.${SERVICE}_cron"
  11. #
  12. # example:
  13. # file: /etc/sv/mlocate/run
  14. # ```sh
  15. # #!/bin/sh --
  16. #
  17. # updatedb_service() {
  18. # exec updatedb
  19. # }
  20. # . /etc/runit/functions/runit_cron
  21. # SERVICE=locate-db STATE_FILE=/run/mlocate/.state SLEEP="$(( 60 * 60 ))" \
  22. # runit_cron updatedb_service
  23. # ```
  24. #
  25. runit_cron() {
  26. set -ue
  27. : "${1:?}"
  28. : "${SERVICE:=${PWD##*/}}"
  29. : "${STATE_FILE:=${TMPDIR:-/tmp}/.${SERVICE}_cron}"
  30. if [ -f "${STATE_FILE}" ]; then
  31. rm -f -- "${STATE_FILE}"
  32. exec chpst -b "${SERVICE}: sleep" \
  33. sleep "${SLEEP:-$(( 30 * 60 ))}"
  34. else
  35. ( umask 777 && :> "${STATE_FILE}" )
  36. "${@}"
  37. fi
  38. }