A cron job runs a command automatically at specified times on a Linux system. This guide explains how to add a schedule for your own user account, for example to run a maintenance script every 15 minutes.
Before you start
You need terminal access and permission to use cron. Test the script manually first, use full paths and check the server’s timezone. Jobs can overlap if a previous run has not finished.
Add a user cron job
-
Open your user crontab:
crontab -e -
Add a line containing five schedule fields followed by the command. For example:
*/15 * * * * /usr/bin/bash /home/example/scripts/task.sh >> /home/example/task.log 2>&1Replace both paths with locations accessible to your account. Check the location of Bash on your server. The example runs at minutes 0, 15, 30 and 45 of each hour and appends output and errors to the log.
-
Save and close the editor. The save keys depend on the editor selected by your system.
-
Check the saved schedule:
crontab -l -
After the next scheduled run, inspect the log and confirm that the task completed.
Understand the schedule
The five fields are minute, hour, day of month, month and day of week, in that order. An asterisk matches every permitted value for that field. Cron’s environment is smaller than an interactive shell’s, so do not rely on your usual working directory or shell settings.
User crontabs do not include a username field. System files such as /etc/crontab do include a username after the schedule fields and should only be edited by a server administrator. Timezone changes and daylight saving changes can affect scheduled runs.