Crontabs

From MineOS Wiki
Jump to navigation Jump to search

Note, crontabs are distribution-specific. This page as meant as a guide on how to correctly implement cron jobs in the abstract, but you should reference material specific to your installation when actually locating cron files and syntax. That said, much of what is here is applicable to all Linux-based crond usage.

crontab overview

Often, many maintenance tasks need to be executed at a certain time (such as during expected low-use hours) without requiring interaction from the admin. Crontabs are the answer to this. Crontabs are time-triggered events in Linux, which can be used to execute shell commands at any interval.

Tasks can be done hourly, daily, on certain days of the week, certain minutes of the hour/day etc. There is tremendous flexibility!

Checking crontabs

To see the crontabs (scheduled tasks) for any given user, use the syntax: crontab -u user -l

<syntaxhighlight lang="bash">

  1. /etc/crontab: crond(8) configuration

@hourly ID=sys.hourly /usr/sbin/runjobs /etc/cron/hourly @daily ID=sys.daily /usr/sbin/runjobs /etc/cron/daily @weekly ID=sys.weekly /usr/sbin/runjobs /etc/cron/weekly @monthly ID=sys.monthly /usr/sbin/runjobs /etc/cron/monthly

  1. End of file

</syntaxhighlight>

Pre-configured crontabs

There are three pre-configured 'helper'-crontabs in MineOS CRUX, these are: hourly, daily, and weekly. This setup means that any executable script located in the /etc/cron/{hourly/daily/weekly} directories will be executed at their respective interval. 'monthly' also exists, but is too imprecise a time-frame for responsible administration, in my opinion, and therefore not utilized.

In other words, if you create a script called 'restart_hiawatha.sh' with executable permissions in /etc/cron/weekly, the script will be automatically executed once a week, midnight on Sunday.

crontab intervals

Entry Description Equivalent To
@monthly Run once a month, midnight, first of month 0 0 1 * *
@weekly Run once a week, midnight on Sunday 0 0 * * 0
@daily Run once a day, midnight 0 0 * * *
@hourly Run once an hour, beginning of hour 0 * * * *

Manually Adding/Editing crontabs

  1. Open the crontab file: crontab -u root -e
  2. Edit/add crontabs as necessary
  3. Save and quit with :wq

If you need help understanding the syntax of crontabs, step over to this Crontab Generator.

Application

crontab run as root at a regular interval

For example, if you want to to run an set of commands every week, you can create a single script:

(backupscript contents) <syntaxhighlight lang="bash"> cd /usr/games/minecraft su mc -c './mineos_console.py -d /var/games/minecraft -s servername backup' </syntaxhighlight>

Remember, this format for crontabs are usable for any purpose, not just Minecraft maintenance!

crontab run as mc at a user-specified interval

For Minecraft scripts, it's advised to use mc to run crontabs.

This means, create a crontab file for the user mc which will execute scripts as mc. As a result, you can simplify your crontab entries:

(crontab contents) <syntaxhighlight lang="bash">

  • 4 * * * cd /usr/games/minecraft; ./mineos_console.py -d /var/games/minecraft -s servername backup

</syntaxhighlight>

crontab run as root at a user-specified interval