Mastering Linux Cron Jobs: How to Schedule Tasks on CentOS 7/9 via Terminal

Imagine this scenario: It is 3:00 AM. You are fast asleep. Suddenly, your server crashes because the log files filled up the entire disk space. Why? Because you forgot to schedule a cleanup task.

Automation is the heart of Linux server management. Whether you need to run a critical backup script, clear cache, or sync data to the cloud, Cron is the silent guardian that handles it all while you sleep.

In this guide, we will learn the "Hard Way" (via Terminal) on CentOS. While control panels like aaPanel are great, a true SysAdmin must know how to handle the raw terminal commands. Mastering this gives you total control over your server's schedule.

Confused by cron asterisks (* * * * *)? Learn the complete syntax of Crontab on CentOS and how to schedule automatic backup scripts manually via SSH

The Magic Syntax: * * * * *

To edit cron jobs, the command is simple: crontab -e. However, once inside, you are greeted by a blank screen or a confusing text editor. You need to speak the language of the "Stars".


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

Common Examples & Best Practices

1. Run every day at 3:00 AM:

0 3 * * * /root/backup.sh

2. Run every 5 minutes (Common for Laravel):

*/5 * * * * /usr/bin/php /var/www/html/artisan schedule:run >> /dev/null 2>&1

Notice the >> /dev/null 2>&1 part? This is crucial. It prevents Cron from spamming your email inbox every time the script runs successfully. It discards the output unless you want to log it to a specific file.

How to Save (Vi Editor Survival Guide)

CentOS usually uses vi as the default editor, which traps many beginners. Here is how to survive it:

  1. Press i on your keyboard to enter Insert Mode.
  2. Paste your cron line carefully.
  3. Press Esc to exit editing mode.
  4. Type :wq (Write and Quit) and hit Enter to save.

To verify if your cron is actually saved, you can run crontab -l to list all active tasks.


Author: Danang | Daily Innovate Tech

Post a Comment for "Mastering Linux Cron Jobs: How to Schedule Tasks on CentOS 7/9 via Terminal"