Ahmad Awais

Senior Full Stack WordPress developer — WP Core Contributor — Front-end Fanatic — Accidental Writer. I love to write, talk, build, and share everything about WordPress. You can find me making small automation scripts for DevOps stuff or toying around with WP Business ideas. Let's catch up at Twitter @MrAhmadAwais.

SHARE


Setup Cron in Unix — Basic Understanding!

Ahmad AwaisAhmad Awais

More often than not, we find ourselves in a situation where something needs to be automated. For example, since I use Let’s Encrypt SSL certificates, I have to renew them every 90 days.

To automate it, I use a simple cron. Being a DevOps fanboy, I like doing things from scratch. This article will help you write a simple cron yourself if you have SSH/root access to your server.

Crontab

You can use the crontab command to setup your cron on a Unix based OS.

NAME: crontab — maintain crontab files for individual users (V3)

SYNOPSIS: crontab [ −u user] file crontab[−uuser]{−l |−r |−e}

DESCRIPTION: The crontab utility is the program used to install, deinstall or list the tables used to drive the cron(8) dae- mon in Vixie Cron. Each user can have their own crontab.

Add a Cron Job

Let’s add a simple cron job that repeats itself every fifth day.

1. Edit the Current crontab

Add a cron job by the command (this will open up the crontab file where you can add your crons).

crontab -e

A crontab file has five fields for specifying mins, hours, the day of the month, month, and the day of the week followed by the command to be run at that interval.

* * * * *  command to be executed
– – – – –
| | | | |
| | | | +—– day of week (0 – 6) (Sunday=0)
| | | +——- month (1 – 12)
| | +——— day of month (1 – 31)
| +———– hour (0 – 23)
+————- min (0 – 59)
* in the value field above means all legal values as in braces for that column.

2. Add a Cron Job

Since we only want our Cron to run every fifth day. I’ll add something like this at the end of the file. Each cron job should be at a new line.

0 0 */5 * * shell command here

or to be exact a custom shell command for a bash function that I wrote.

0 0 */5 * * wpsites update --le --all

3. Save the crontab

Then ⌃ (CTRL) + X then type Y and finally press ENTER (return) on Mac to save the file.

4. Save the crontab

You can check the new crons have been installed of not by

crontab -l

And you’re done :)

Let me know how it goes at your end!

🎩

WPTakeaway: Premium WordPress Newsletter!

Takeaway my professional opinion about Full Stack WordPress Development, building a business, growing a family, and everything web! Delivered to your inbox twice a month. 👻 I hate spam! Pinky Promise!

Senior Full Stack WordPress developer — WP Core Contributor — Front-end Fanatic — Accidental Writer. I love to write, talk, build, and share everything about WordPress. You can find me making small automation scripts for DevOps stuff or toying around with WP Business ideas. Let's catch up at Twitter @MrAhmadAwais.