How to use Crontab on Ubuntu server

Crontab is a command used to execute tasks regularly in Linux/Unix operating systems. It is very suitable for repetitive tasks. I am using Ubuntu 20.04 LTS, but the crontab commands in most linux distributions are similar, so even if you are not using Ubuntu, this article can be used as a reference.

Command

crontab [ -u user ] { -l | -r | -e }

-u: View or edit crontab tasks for specific users. This is optional.
-e: Edit crontab file
-r: Delete crontab file
-l: Show crontab file

Schedule

We can edit the crontab file after executing the crontab -e command, and the system will open the crontab file with the default editor. The tasks we configured should look like this(wikipedia).

# ┌───────────── minute (0 - 59)
# │ ┌───────────── hour (0 - 23)
# │ │ ┌───────────── day of the month (1 - 31)
# │ │ │ ┌───────────── month (1 - 12)
# │ │ │ │ ┌───────────── day of the week (0 - 6) (Sunday to Saturday;
# │ │ │ │ │                                   7 is also Sunday on some systems)
# │ │ │ │ │
# │ │ │ │ │
# * * * * * <command to execute>

* All
/ Every
- Range
, Or

For example, in November and December, Monday to Friday, hello.py will be executed every 2 hours(:15) from 8 am to 6 pm.

15 8-18/2 * 11,12 1-5 python3 /home/test/hello.py

We may see many logs like this in the cron log file.

(CRON) info (No MTA installed, discarding output)

This is usually because the tasks has output, such as executing the echo command, but we have not configured the MTA(Message Transfer Agent). We can solve the problem by installing postfix, but a simpler solution is > /dev/null 2>&1. Regarding the log output, I will discuss it in the next chapter.

45 15 * * 1-5 echo "Hello World!" > /dev/null 2>&1

Logs

Crontab is an automated task, and it is important to track the tasks execution status. On the Ubuntu server, the logs is stored in /var/log/syslog, we can confirm the logs through grep cron /var/log/syslog.

We can also separate the cron logs from syslog. This requires editing /etc/rsyslog.d/50-default.conf and uncomment cron.*. After restarting the service through sudo systemctl restart rsyslog, we can check the logs by cat /var/log/cron.log.

Another requirement is to save logs for each crontab tasks.
Output All logs: * * * * * python3 /home/test/hello.py > /tmp/hello.log 2>&1
Output Common logs: * * * * python3 /home/test/hello.py > 1> /tmp/hello.log
Output Error logs: * * * python3 /home/test/hello.py > 2> /tmp/hello.log

Explanation:

> Redirect
1 STDOUT
2 STDERR

Editor

On Ubuntu server, the default crontab editor is nano, but I prefer to use vi. After rm ~/.selected_editor then execute the crontab command, we will be able to select the default editor.

root@test:/var/log# crontab -e

Select an editor.  To change later, run 'select-editor'.
  1. /bin/nano        <---- easiest
  2. /usr/bin/vim.basic
  3. /usr/bin/vim.tiny
  4. /bin/ed

Choose 1-4 [1]: 2

Timezone

Another important thing for crontab is that Timezone and NTP. We can select the Timezone through tzselect, and copy it to localtime cp /usr/share/zoneinfo/Asia/Tokyo /etc/localtime.

root@test:/var/log# date -R
Sun, 22 Nov 2020 20:55:32 +0900

References

Wikipedia Cron
Archlinux Cron
Ubuntu CronHowto
Linux crontab 命令

Leave a Reply

Your email address will not be published.