Ahmad Awais



Anatomy of crontab!

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) daemon 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.

Crontab* in the value field above means all legal values as in braces for that column.

UPDATE: Check out this awesome resource to visualize→ crontab.guru

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#

Save it CTRL (βŒƒ) + X then Y β€” finally press ENTER (return)

4. Save the crontab#

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

crontab -l

To Summarize#

1️⃣ Edit the cron by command crontab -e
2️⃣ Save it CTRL (βŒƒ) + X then Y β€” finally press ENTER (return)
3️⃣ Check cron jobs with crontab -l

And you’re done :) Let me know how it goes at your end!

Peace! ✌️

Founder & CEO at Langbase.com β€” Ship hyper-personalized Al apps in seconds Β· Ex VP DevRel Eng. Rapid Β· Google Developers Advisory Board (gDAB) founding member. πŸ§‘β€πŸ’» AI/ML/DevTools Angel Investor ❯ AI/ML Advisory Board member San Francisco, DevNetwork

🎩 Award-winning Open Source Engineer & Dev Advocate 🦊 Google Developers Expert Web DevRel πŸš€ NASA Mars Ingenuity Helicopter mission code contributor πŸ† 8th GitHub Stars Award recipient with 5x GitHub Stars Award (Listed as GitHub's #1 JavaScript trending developer).

🌳 Node.js foundation Community Committee Outreach Lead, Member Linux Foundation, OpenAPI Business Governing Board, and DigitalOcean Navigator. πŸ“Ÿ Teaching thousands of developers Node.js CLI Automation (100 videos Β· 22 Projects) & VSCode.pro course. Over 142 million views, 24 yrs Blogging, 108K developers learning, 200+ FOSS.

✌️ Author of various open-source dev-tools and software libraries utilized by millions of developers worldwide ⓦ WordPress Core Developer πŸ“£ TEDx Speaker with 100+ international talks.

✨ As quoted by: Satya Nadella Β· CEO of Microsoft β€” Awais is an awesome example for developers.
πŸ™Œ Leading developers and publishing technical content for over a decade πŸ’œ Loves his wife (Maedah) ❯ Read more about Ahmad Awais.

πŸ‘‹β€¦ Awais is mostly active on 𝕏 @MrAhmadAwais


Developers Takeaway

Stay ahead in the web dev community with Ahmad's expert insights on open-source, developer relations, dev-tools, and side-hustles. Insider-email-only-content. Don't miss out - subscirbe for a dose of professional advice and a dash of humor. No spam, pinky-promise!

✨ 172,438 Developers Already Subscribed
Comments 1
  • Leonardo Borsten
    Posted on

    Leonardo Borsten Leonardo Borsten

    Reply Author

    Great post! Can you please explain the */5 notation for the days? Looks like it says “any day divided by 5”.