rss logo

Setting up /etc/crontab on OpenBSD

OpenBSD Logo

I recently wanted to schedule automatic tasks on OpenBSD system. A big surprise for me was that, unlike Linux distributions, there was no default /etc/crontab file.

It's not that hard to implement, but there are some aspects to take into account in order to create it, which we will see here.

Things to know

Before going further, there are some things to know about setting up the /etc/crontab file.

  • Things to know:
    • Log file for cron: /var/cron/log, useful for tracing commands that are being executed correctly.
    • Time to take file changes: cron checks the modification time on the system crontab file once a minute.
    • File mode: The crontab file will be ignored if it does not have the proper file mode. The mode must be 0600. It must not be writable by any user other than root and must not have the execute, set-user-ID, set-group-ID, or sticky bits set.
    • Restart cron: /etc/rc.d/cron restart
    • Alternatively, we can use the /var/cron/tabs/root file.

Create /etc/crontab

  • Create /etc/crontab file:
root@host:~# touch /etc/crontab
  • Set owner:
root@host:~# chown root:wheel /etc/crontab
  • Set rights file:
root@host:~# chmod 0600 /etc/crontab
  • Edit /etc/crontab file:

Here's an example where we want to download the br.zone file (containing all IP addresses from Brazil) on the 12th day of every month at 5:00am.

# Example of job definition: # .---------------- minute (0 - 59) # | .------------- hour (0 - 23) # | | .---------- day of month (1 - 31) # | | | .------- month (1 - 12) OR jan,feb,mar,apr ... # | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat # | | | | | # * * * * * user-name command to be executed 0 5 12 * * root wget --no-check-certificate https://www.ipdeny.com/ipblocks/data/countries/br.zone -O /etc/tables/br.zone
  • Check /var/cron/log file to see if the command has been well executed:
root@host:~# tail -f /var/cron/log Apr 12 00:05:01 BSDhost cron[3577]: (root) CMD (wget --no-check-certificate https://www.ipdeny.com/ipblocks/data/countries/br.zone -O /etc/tables/br.zone)
Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

Contact :

contact mail address