Operations 5 min read

Master Linux crontab: Schedule Scripts, Syntax, and Practical Examples

This guide explains how to use Linux's crontab command to create scheduled tasks, covering its features, common use cases, syntax details, example schedules, editing procedures, output redirection, and necessary script permissions for reliable automation.

Liangxu Linux
Liangxu Linux
Liangxu Linux
Master Linux crontab: Schedule Scripts, Syntax, and Practical Examples

Why use crontab?

When a server needs regular maintenance—such as cleaning old backup files—a shell script can be executed automatically using Linux's crontab command, which schedules tasks at specified times.

Key features of crontab

Runs tasks without user interaction, ideal for background automation.

Supports multiple independent jobs, each with its own schedule.

Allows execution on specific dates and times (e.g., the last day of each month).

Enables recurring intervals such as hourly or daily runs.

Persists across system reboots, automatically restoring scheduled jobs.

Typical use cases

System maintenance – periodic log cleanup, database backups.

Data analysis – scheduled analytics scripts and report generation.

Network tasks – regular connectivity checks, email notifications.

Basic crontab syntax

* * * * * command-to-execute

Fields represent minute hour day-of-month month day-of-week respectively.

Common scheduling examples

Run a script every day at 5 AM: 0 5 * * * /path/to/command/script.sh Run a script on the last day of each month (example shown uses day 1 for illustration): 0 0 1 * * /path/to/command/script.sh Run a script hourly:

0 * * * * /path/to/command/script.sh

Editing the crontab file

Open the editor with crontab -e. The system may prompt you to choose an editor via select-editor. Add, modify, or delete lines following the syntax above. For example, to schedule the 5 AM job: 0 5 * * * /path/to/command/script.sh It is common to redirect output to avoid clutter:

/path/to/command/script.sh > /dev/null 2>&1

Saving and locating crontab files

After saving and exiting the editor, the new job becomes active at the next scheduled time. Crontab files are stored under /var/spool/cron/.

Ensuring script executability

The script must have execute permission; set it with:

chmod +x /path/to/script.sh
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

task schedulingLinuxcronshell scriptcrontab
Liangxu Linux
Written by

Liangxu Linux

Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.