How to setup a Cron Job

You can use cron jobs to automatically run a script or command on a set schedule.

How to setup a Cron Job

Video Format

Prefer a video? You can watch that here:

Additionally, you can subscribe to my Youtube channel here:

What are cron jobs?

Cron jobs are designed to run a command or script on a set schedule. Need something to run every 10 minutes? Weekly? Monthly? Cron jobs are a great option and you can use crontab to do this.

Crontab entries

When creating an entry in crontab, you must fill in the minutes, hours, day of the month, month and day of the week.

* ----- minute (0-59)
| *------ hour (0-23)
| | *-------- day of the month (1-31)
| | | *------- month (1-12)
| | | | *-------- day of the week (0-6) (Sunday = 0)
| | | | |
* * * * * script_to_run.sh

When updating these entries, * is a wildcard. You can also use

# Wildcard, no filter
*
# specific number
1
2
# muliple numbers
1,2,3
# ranges
1-3
4-6
# multiple numbers and ranges
1,2,4-6
# Every x time period
*/5
*/15

This ties into the system time. Meaning 1 12 * * * would run at 12:01 PM every day. If this is a server, the general recommendation seems to be to use UTC time. You can check the time your system is set to by running date.

Options

You can add various options to the top of the crontab entry. You can also import variables. Here are two examples, the first will have the system send an email to the supplied email address for failures. The second entry will use bash rather than shell (by default cron will just use shell scripts).

I prefer to specify bash as that's what I know best.

MAILTO=email@example.com
SHELL=/bin/bash
Important to note, just because you run the bash shell does not mean that you import your bashrc file. You are not running in interactive mode.

This means specific entires added like aliases will not work when entering a crontab.

If you need to have access to bash aliases you need to create a bash script to call when running the crontab and import the bashrc script in that. There are several ways to do this, but that's beyond the scope of this article.

Examples

Here are the examples discussed in the video. The comment above each describes how the period it's setup to run on.

#every 5 minutes between Midnight and Noon
*/5 0-12 * * *  echo "hello world" 
#9 AM, 11 AM, 1 PM, 2PM through 6 PM at the top of hour
0 9,11,13,14-18  * * * echo "good morning"
#6 AM, the first day of the month
0 6 1 * * "begin month"
# 6 AM every Monday
0 6 * * 1 "Happy Monday"
# Midnight on Jan 1st.
0 0 1 1 * "Happy New Year"
# Run every 6 hours Mon-Fri
* */6 * * 1-5 ~/bash/script.sh

The site below can help you check the vaility of your crontab entries.

Crontab.guru - The cron schedule expression editor
An easy to use editor for crontab schedules.

Setting up crontab

To begin enter the command below:

crontab -e

If this doesn't work, you may need to install crontab. In arch linux you could do this via:

sudo pacman -S cronie

After this crontab -e should work.

The first time you run this command, it may ask for the default editor and you can select from whatever editor options you have installed. If you need to change this or crontab didn't give you an option, see below to select the default editor.

Setting the editor

By default, at least in Arch Linux, crontab -e opens using Vi, not Vim,  Vi!

I know enough Vim to be dangerous. Vi, on the other hand, is just different enough that it started to drive me a little crazy. Things were similar but different enough to make things difficult.

You can change the editor one time using the command below:

EDITOR=editor_name crontab -e
#example, notice no space between the = and name
EDITOR=vim crontab -e
Changing the editor one time

You can also set a default editor so that using crontab -e opens the crontab file in the specified editor.

How to do so will depend on your shell. You can confirm your shell by running the $SHELL in the terminal.

Set default editor, Ubuntu-based distros

In Ubuntu-based distros, you will likely be able to run the command select-editor to select which editor apps like crontab use.

If you don't have select-editor or are not on ubuntu-based....

Set default editor in Fish shell

To set the editor in fish you can run the following command:

#set editor in Fish:
set -Ux EDITOR `editor_name`
#Example:
set -Ux EDITOR vim

The U and x flags do the following:

  • U = Universal, meaning it's kept across all fish sessions and through restarts
  • x = export, meaning any child processes from this (like a bash shell) will have the environment variable too

If you ever want to remove this as a variable, run:

set -e EDITOR

Set default editor in Bash shell

To set the default editor in bash, you need to update your .bashrc file.

Alternately, if desired, you could update .profile or .bash_profile or .bash_login.

All of these should be located in your home ~ directory. If you don't have a  .bashrc file, it will be created with the commands below. If you do have one you can update it with the same command.

Using .bashrc

Run the commands below to open your .bashrc file in your editor of choice.

cd
#<editor> .bashrc
#Examples
vim .bashrc
#or
nano .bashrc

What to add to .bashrc

Once in the editor, you need to include this information below. You should be able to paste the EDITOR command with CTRL+ SHIFT + V. If using Vim, just make sure you're in insert mode first.

If the .bashrc file already exists, you will want to add this line to the end.

#export EDITOR='editor'
#examples
export EDITOR=vim
#or (only pick one of these)
export EDITOR=nano

If you prefer to use the profile

If you're wanting to  update .profile or .bash_profile or .bash_login instead, you can run the following to open it:

cd
ls -l
#confirm which option you have, profile, bash_profile, or bash_login
#editor .profile or .bash_profile or .bash_login
#example
vim .profile

What do add in the .bash_profile or .profile

#EDITOR=path_to_editor
#Example:
EDITOR=/usr/bin/vim

#Find path to editor:
#which editor
#example
which vim

Delete a Crontab file

You can run the command below to delete your crontab.

⚠️
Running this command will delete your crontab file!
crontab -r

Enjoy this article? Consider subscribing to my newsletter!

Vashinator
Focused on Linux and Open Source

Running other scripts

Due to cron jobs just running a shell command, you can also run other scripts like python scripts. You can get more info about how to run a python script from a bash script below.

Running Python from a Bash Script
Sometimes, it isn’t best to do everything via Python. In my case, due to multi-factor authentication, I couldn’t send emails via Python and instead was able to use bash to run the python code and send an email if it failed. Video version: You can get the full code in