21

If I create a script and then place it in this folder (/etc/cron.hourly), will my system run this script hourly? Or does my script need to begin with a command as well?

Switchkick
  • 3,883

3 Answers3

31

Every script placed in folder /etc/cron.hourly would run on hourly basis.

However your files needs to be:

  • executable,
  • match the Debian cron script namespace (^[a-zA-Z0-9_-]+$).

So for example if you've script with extension, it won't work.

To print the names of the scripts which would be invoked, run:

sudo run-parts --report --test /etc/cron.hourly
kenorb
  • 10,347
  • Is there any reasonable explanation why file name extensions are considered bad in that context...? – Hinz Aug 04 '23 at 09:07
17

Yep, you got it.

Just start it with a #!/bin/bash like you normally would. And make sure you sudo chmod +x /etc/cron.hourly/yourscript because it won't run without execute permissions.

maco
  • 15,892
8

Anything in /etc/cron.hourly will be executed hourly, just like anything in /etc/cron.daily will be run once a day.

Make sure the file is executable, and start it with #!/bin/bash or #!/usr/bin/python (or #!/usr/bin/env python) or whatever is appropriate for the type of script you'll be running.

A.B.
  • 90,397
csgeek
  • 321