A photo of Evan Pratten
Evan Pratten

I used cron for the first time

And I didn't die

Cron has always been one of those “scary sysadmin things” in my head. But today, I finally used it!

My need

I have access to a private API that happens to clear it’s users if they are inactive for too long. To solve this, I decided to add a small cron job to make an API call once per month. Basically a keepalive.

How I set it up

Adding a cron job to my laptop was very easy. First, I made a bash script for my api call (not needed, but I felt like doing it).

#! /bin/bash
curl --include --header "Accept: application/xml" '<API Endpoint Here>' --user $1:$2

Then, by running crontab -e in my terminal, I just added a new line at the bottom of the file, discribing the task, and when it should be run.

# Edit this file to introduce tasks to be run by cron.
# 
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
# 
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').# 
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
# 
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
# 
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
# 
# For more information see the manual pages of crontab(5) and cron(8)
# 
# m h  dom mon dow   command
00 11 1 * * /usr/local/bin/api-keepalive.sh <username> <password>

This will run once per month, on the 1st, at 11:00.

That’s it! Stupidly simple, and I am no longer scared of cron