skip to content

Job Scheduling in Linux with at and crontab

/ 7 min read

Why do today, what you can automate for every day?! 😅🤖

Job Scheduling in Linux with at and crontab

Hey, hey, tech enthusiasts! Daniel Bahl here, and I’m super stoked to dive into the world of Linux job scheduling today. Let’s be real—who wants to sit around and manually run commands like a cave man when you could be leveling up in a game? 🎮 😜

Automating tasks in Linux is like setting up a team of virtual minions that do your bidding while you sip on a cup of coffee ☕ or watch some epic YouTube videos 📹. You can think of it as the ‘set it and forget it’ philosophy, but for your Linux system.

Whether you’re a sysadmin in a data center, a developer with a VPS, or a hobbyist messing around on a Raspberry Pi, knowing how to schedule tasks in Linux is an invaluable skill. So in today’s post, we’re going to dissect and master two Linux classics—at for one-time tasks and crontab for recurring chores.

So, buckle up and put on your Linux hard hats! 🎩 It’s time to make your computer work smarter, not harder.

🚀 One-Time Scheduling with at 🕐

Picture this: You’ve got a hot date with your latest game obsession, but you also need to, let’s say, create a user account for Karen, the intern starting next week. What do you do? Burn the midnight oil? Nah! You use at!

🧭 Is at on Your System?

Before you jump in, you might want to see if you already have the at utility installed. Fire up your terminal and run:

which at

If your terminal shoots back a path, then congrats! You’re already in business. If not, keep reading.

🛠 Installing at

For those rocking a Debian-based distro like Ubuntu, Pop!_OS, Deepin or alike you can summon at into your machine with:

sudo apt install at

Or, if you’re on a Red Hat-oriented distro (AlmaLinux, Rocky, Fedora), it’s:

sudo dnf install at

🏃‍♂️ Get the at Daemon Up and Running

Check if the at daemon is chilling out and ready for commands:

systemctl status atd

If not, you’ll need to enable and start the daemon like a boss:

systemctl enable atd
systemctl start atd

🕵️‍♂️ Working with at

Alright, now we’re cooking! at is super flexible when it comes to date and time formats. You can check all its majesty in the man pages (man at), but for now, let’s stick to a few quick examples.

Say you want to schedule a backup to run tonight at 8:15 PM. No worries! Run:

# "24-hour clock example"
at -f my-awesome-backup.sh 20:15 

# "12-hour clock example"
at -f my-awesome-backup.sh 8:15pm

You can also use now plus an extra time unit for “in the moment” jobs:

at -f ultra-urgent-backup.sh now + 15 minutes

Feeling nocturnal? How about a database backup at 2 in the morning?

at -f night-owl-db-backup.sh 2:00 AM

Now, let’s talk about how chill at is with time—like, well.. really chill 😜 Check out these totally valid time formats you can use with at:

at 0815am Jan 24
at 8 :15amjan24
at now "+ 1day"
at 5 pm FRIday

Whether you’re super specific or prefer a more relaxed approach, at just gets you. 🧘‍♂️

📜 What’s in the Queue?

Want to know what jobs are pending? Easy peasy:

atq

This returns:

1	Mon Sep  7 12:34:56 2023 a piraffeRemote
# This information is, from left to right: job number, date, hour, year, queue, and username.

Each job has its own special ID number. You can use that number to delete a job if you’ve changed your mind:

# atrm <Job-ID>
atrm 1

So there you have it, folks! at is like your virtual techie sidekick, always on standby to tackle tasks like adding new users, initializing servers, and yeah, even pranking your teammates (just don’t blame me!). Imagine a digital butler who never takes a day off. 🎩

Now, you might be thinking, “Can I use at for something as critical as server reboots?” Sure, you could. But let me let you in on a little secret: the shutdown command itself has some neat scheduling tricks up its sleeve. Want to give your server a quick beauty sleep at 3:15 AM? It’s a cinch:

shutdown -r 03:15 &

Oh, and see that ampersand (&) at the end? Remember to end with &; That will allow you to log out of the system without killing the shutdown process. Neat, right?

“But wait,” you might ask, “what if I change my mind?” No worries. Kill that scheduled reboot with:

shutdown -c

So whether it’s one-time tasks or periodic reboots, you’ve got all the tools you need for automating your Linux life. Go ahead, make your Linux machine work for you while you kick back and enjoy your gaming session. 🎮

🕰️ Periodic Scheduling with Cron

Let’s shift gears and talk about cron, the grandmaster of task scheduling. Trust me, one-off tasks are cute and all, but the real deal is with tasks that run on a recurring basis. Perfect for sysadmins and infrastructure engineers who live by the mantra: “Why do today what you can automate for every day?” 😜😅

🛠️ Installing Your Cron Scheduler

Most Linux distros comes with cron built-in out-of-the-box, yay 🎉 So there’s no reason to install anything for the standard cron needs. But there are tons of different alternative, read more at Gentoos Wiki post about different cron scheldulers like cronie, anacron and fcron if you feel brave or have special needs.

📜 Working with Your Crontab File

Every user gets their own crontab, a magical text file that cron reads to know what jobs to run and when. To see your current users cron-file (crontab file) type:

crontab -e

This opens your crontab in your favorite editor (you know you have one), and here’s where the magic happens:

15 22 * * 6 /home/piraffe/late-night-text-to-wife_homesoonfromwork.sh

Let me decode that for you:

Field	AllowedValues	Represents
Minute	00-59	        The minute of the hour
Hour	0-23	        The hour of the day
Day	    0-31	        The day of the month
Month	1-12	        The month of the year
Weekday	0-7             The day of the week 
# Weekday examples 
## (0 and 7 is both sunday, 1 = monday, 5 = friday etc.) 

15 22 * * 6 /home/piraffe/late-night-text-to-wife_homesoonfromwork.sh
|  |  | | |------------------------- Weekday [0-7]
|  |  | |--------------------------- Month   [1-12]
|  |  |----------------------------- Day     [0-31]
|  |-------------------------------- Hour    [0-23]
|----------------------------------- Minute  [00-59]

In this example, we’re running late-night-text-to-wife_homesoonfromwork.sh every Saturday at 22:15 (or 10.15pm) 🎩✨

To help you understand how to write the time format in the cron, you can use a generator to generate the time format, I recommend the Crontab.guru

📌 Important Points to Remember:

  • Make your scripts executable (chmod +x script.sh)
  • Use absolute paths (always /path/to/your/script.sh)
  • Know who’s executing your script; it’s usually root or a system user. (sudo crontab -e)

Want to see what jobs you’ve got lined up?

crontab -l

🎯 Cron Job Examples and How to Decipher Them

So, you’re getting the hang of cron and can’t wait to start automating all the things. But the syntax is driving you nuts, right? Fear not! Let’s demystify some common cron schedules you might want to use.

🔁 Every Minute

If you have something that needs to happen every minute, use the following:

* * * * * your-command.sh

⏲️ Every 5 Minutes

Need to run something every 5 minutes? Here’s how:

*/5 * * * * your-command.sh

☀️ Every Morning at 9 AM, but Not on Mondays

To have a good morning every day at 9 AM, except Mondays (because who likes Mondays?):

0 9 * * 2-7 your-command.sh

🕔 Every Weekday at 5 PM

Ah, the classic “end-of-workday” script can be scheduled like so:

0 17 * * 1-5 your-command.sh

🏖️ Every Weekend at Noon

For those who like to work even on weekends, run a script every Saturday and Sunday at noon:

0 12 * * 6,7 your-command.sh

🌑 Midnight Shenanigans

To run a job every day at midnight:

0 0 * * * your-command.sh

🌙 Custom Schedule: First and Fifteenth of the Month at 6:30 PM

For those who love customization:

30 18 1,15 * * your-command.sh

🕰️ At 2:30 PM on the 1st of January

Kick off the New Year with a script:

30 14 1 1 * your-command.sh

Run every second?

You can’t. But you can, I’ve written a blogpost about how you can achieve this

🐳 Other Tips And Tricks

But wait, there’s more! If you’re the admin and want to meddle with someone else’s crontab, you can totally do that (with proper permissions, of course).

sudo crontab -u daniel -e

Same goes for peeping at Daniel’s scheduled tasks:

sudo crontab -u daniel -l

So, whether you’re a periodic scheduler or a one-off kind of person, Linux has got you covered. It’s like having your own army of robotic assistants, but way cooler. 🤖

📬 If you have questions or comments, feel free to hit me up on Twitter 𝕏.com/danielbahl