# 16.2 Adding Cron Jobs The software utility cron is a time-based job scheduler in Unix-like computer operating systems. Users that set up and maintain software environments use cron to schedule jobs (commands or shell scripts) to run periodically at fixed times, dates, or intervals. It typically automates system maintenance or administration, though its general purpose nature makes it useful for things like downloading files from the Internet and downloading email at regular intervals. The origin of the name "cron" is from the Greek word for time, χρόνος (chronos). Cron is most suitable for scheduling repetitive tasks. Scheduling one-time tasks can be accomplished using the associated at utility. Here is how the crontab works: ```bash # For details see man 4 crontabs # 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 ``` At [crontab guru](https://crontab.guru/) you can play around how this works. We are going to make a daily job to count the number of active users, and an updated cron job to gather system performance data. #### Creating a cron job for a specific user Create a new daily cron job that logs a message to the system log with the number of users. You can edit ```cron``` for the user that you are logged in as with ```crontab -e``` option (```-e``` means edit). If we add ```-u``` we can select the user we want to make the crontab for; this only works for super users or root. ```bash crontab -e -u nietzche ``` Write in the crontab the following ```bash * * * * * echo "`date` - Hello `who`" >> /tmp/cron.log ``` You can check if the logging is working with crontab is working every minute here ```bash tail –f /tmp/cron.log ``` Some information from the crontab file: ```bash # For details see man 4 crontabs # 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 ```