How to schedule a script with crontab
Add a cron job to run a Bash script at a specific time or interval using crontab on Linux and macOS.
Run any Bash script automatically at a scheduled time or recurring interval by adding a cron job entry with crontab on Linux and macOS.
Prerequisites
- Linux or macOS with cron installed (preinstalled on most distributions)
- A Bash script to schedule (the script must be executable)
- Terminal access with the user account that should own the cron job
Step-by-Step: Schedule a Script with crontab
1. Make the Script Executable
The cron daemon runs scripts as the owning user but does not invoke a login shell. Mark the script as executable with
chmod so cron can run it directly:
chmod +x /home/user/scripts/backup.shThe script must also include a shebang line (
#!/bin/bash) as its first line. Cron does not assume Bash — without the shebang, cron attempts to interpret the script with
/bin/sh, which may lack Bash-specific features.
2. Open the crontab Editor
The crontab command opens the current user's cron table for editing with the
-e flag:
crontab -eThe crontab command launches the default text editor (usually
nano or
vi). Each line in this file represents one scheduled cron job.
3. Add the Cron Job Entry
A crontab entry consists of five time fields followed by the command to execute. Add a line that schedules the script to run at 2:00 AM every day using the crontab time expression
0 2 * * *:
0 2 * * * /home/user/scripts/backup.sh >> /home/user/logs/backup.log 2>&1The five fields in the crontab expression represent minute (0), hour (2), day of month (
), month (), and day of week (*). The
>> /home/user/logs/backup.log 2>&1 portion redirects both standard output and standard error to a log file for debugging.
Always use
absolute pathsin crontab entries. The cron daemon does not load the user's
PATH environment variable, so commands like
backup.sh without a full path cause "command not found" errors. See
Crontab: job not runningfor more on this common problem.
4. Save and Exit the crontab Editor
Save the file and exit the editor. The crontab command installs the updated cron table automatically. No service restart is needed — the cron daemon reads the updated table on the next scheduling cycle.
For the full list of crontab time expressions and field syntax, see the Crontab expression reference.
How to Verify the Script Is Scheduled in crontab
The crontab command displays all active cron jobs for the current user with the
-l flag:
crontab -lThe crontab listing shows the newly added entry:
0 2 * * * /home/user/scripts/backup.sh >> /home/user/logs/backup.log 2>&1Confirm the cron daemon is running by checking the service status:
systemctl status cronThe cron service should show
active (running). If cron is not running, start it with
sudo systemctl start cron.
Common Issues When Scheduling Scripts with crontab
The cron job does not execute at the scheduled time— The most common cause is a missing absolute path to the script or to commands inside the script. The cron daemon uses a minimal
PATH (
/usr/bin:/bin) that does not include directories like
/usr/local/bin. See
Crontab: job not runningfor a full diagnosis.
The cron job runs but produces no output— Without output redirection, cron sends job output as a local email to the user. If no mail transfer agent (MTA) is configured, the output is silently discarded. Always append
>> /path/to/logfile 2>&1 to crontab entries to capture output.
The cron job runs at the wrong time— The cron daemon uses the system's local timezone, not UTC, unless the
CRON_TZ variable is set in the crontab file. Verify the system timezone with
timedatectl and adjust the crontab schedule accordingly.