Advertisement
linuxserver Cron jobs let your Linux server run tasks automatically at specific times—every minute, every hour, every day, or any custom schedule you want. This beginner-friendly tutorial explains how cron works, how to create and edit cron jobs, how the time syntax works, how to test and debug tasks, and common mistakes to avoid.

Cron Jobs for Beginners: How to Automate Tasks on a Linux Server

5 Min Read Verified Content

Welcome back!
Today we’re learning something that every Linux user eventually needs:


👉 Cron Jobs — the automation engine inside your Linux server.

If you’ve ever wished your server could “take care of things on its own,” cron is the tool that makes that dream come true.

Think of cron as a reliable old friend who never forgets what you ask them to do.

  • Want to back up a folder every night?

  • Want to restart a service every morning?

  • Want to run a script every 5 minutes?

Cron can do it — consistently, silently, and perfectly.

Let’s start from the very basics.




🧩 Step 1: What Exactly Is Cron?

Cron is a built-in Linux service that executes commands automatically based on a schedule you define.

A task scheduled by cron is called a cron job.

Cron jobs live inside something called a crontab (“cron table”).

Each user has their own crontab.
Root has a special one, too.




🛠️ Step 2: Opening Your Crontab

To open your personal cron job list:

crontab -e

The first time you run it, Linux may ask you which editor you want.
If you're a beginner, choose nano — it’s the easiest.

You will see something like:

# Edit this file to introduce tasks to be run by cron.

Below that is where you will write your scheduled tasks.




🔢 Step 3: Understanding Cron Syntax (The Magic 5 Fields)


Cron jobs follow this pattern:

* * * * * command-to-run | | | | | | | | | ----- day of week (0-7) | | | ------- month (1-12) | | --------- day of month (1-31) | ----------- hour (0-23) ------------- minute (0-59)

Let’s break it down in simple language:

FieldMeaningExample
minutewhen in the hour0–59
hourwhich hour0–23
day of monthwhich day1–31
month1–12Jan = 1
day of week0–7Sunday = 0 or 7

* means “all.”

Example:

* * * * *

means every minute of every hour of every day.




🎯 Step 4: Simple Beginner Cron Examples


✔ Run a script every day at 2 AM:

0 2 * * * /usr/bin/bash /home/user/backup.sh

✔ Run a command every 5 minutes:

*/5 * * * * /usr/bin/php /var/www/app/cron.php

✔ Run a task every Monday:

0 9 * * 1 /usr/bin/bash /home/user/report.sh

✔ Reboot server every Sunday at midnight:

0 0 * * 0 /sbin/reboot

✔ Clear temp folder every hour:

0 * * * * rm -rf /tmp/*


⏱️ Step 5: Helpful Time Patterns You Will Use a Lot


Every minute:

* * * * *

Every 30 minutes:

*/30 * * * *

Every hour:

0 * * * *

Every day at midnight:

0 0 * * *

First day of each month:

0 0 1 * *

Every 10 minutes:

*/10 * * * *


🧪 Step 6: Testing Your Cron Jobs (Very Important for Beginners)


Cron can be tricky to test because it runs in the background and doesn't show errors.

Here’s how to test safely:

Test with a simple log:

* * * * * echo "Cron is working!" >> /home/user/cronlog.txt

Wait 1 minute, then check:

cat /home/user/cronlog.txt

If text appears — cron is alive.




📌 Step 7: Environment Differences (Cron Is NOT Like Your Terminal)


Cron does not run with your normal environment.


Common problems:


❌ PATH is different
❌ Scripts fail because commands can’t be found
❌ Variables don't exist
❌ Directories aren’t what you think

To fix this, always use full paths, like:

/usr/bin/python3 /usr/bin/php /usr/bin/bash /usr/bin/node

Example:

0 * * * * /usr/bin/python3 /home/user/script.py

Not:

0 * * * * python3 script.py # ❌ might fail


🧹 Step 8: Logging Your Cron Jobs (Optional But Super Useful)


Add logging to see if things run correctly:

0 * * * * /usr/bin/bash /home/user/backup.sh >> /var/log/backup.log 2>&1

This captures:

  • normal output

  • errors

All in one place.




⚠️ Step 9: Common Mistakes Beginners Make & How to Fix Them


❌ Script works manually but not in cron

Cause: Missing PATH
Fix: Use full paths


❌ Permission denied

Fix:

chmod +x script.sh


❌ Cron didn’t run

Check cron service:

sudo systemctl status cron

or on some distros:

sudo systemctl status crond


❌ Wrong file edited

Many beginners edit system-wide cron instead of user cron.

Correct:

crontab -e

Wrong:

sudo nano /etc/crontab # not for beginners


📦 Step 10: Bonus — Creating a Cron Job Folder Script

Let’s say you want to run all scripts inside /etc/cron.hourly:

Just copy your script into:

/etc/cron.hourly/

Example:

sudo cp backup.sh /etc/cron.hourly/ sudo chmod +x /etc/cron.hourly/backup.sh

Linux will run it automatically.




🎉 Conclusion


You now know:

✔ What cron jobs are
✔ How to open a crontab
✔ How the 5-field schedule works
✔ Common real examples
✔ How to test and debug cron
✔ How to log cron output
✔ How to avoid typical beginner mistakes


Cron is one of the simplest, most powerful automation tools Linux has. Once you understand it, you can automate backups, scripts, server maintenance, monitoring, and much more.


Advertisement
Back to Linuxserver