$_ CLI In Depth
Learning progress 0%
lesson packages services cron

crontab Syntax & Pitfalls

5 min read Module 11: Packages, Services & Scheduled Jobs Updated Dec 5, 2026
Cheat Sheet
0:00 / 3:14

The percent sign

In a crontab, an unescaped percent sign in the command is special. Cron turns the first percent sign into a newline that is fed to the command as standard input, and any later percent signs become more newlines. The effect is that a date format such as %Y-%m-%d is cut apart and the command receives the wrong text. The fix is to escape every percent sign in the command with a backslash.

BASH1 lines
0 0 * * * date +\%Y-\%m-\%d >> /workspace/dates.log

The backslashes tell cron to pass literal percent signs to date. Without them, the line would appear to run without error and would produce a broken log file. Test any command that contains a percent sign in a terminal first, and then escape it for the table.

Day of month and day of week

When both the day-of-month field and the day-of-week field are restricted, cron runs the job when either one matches. This is the surprising part. A line that says the thirteenth of the month, on a Friday, runs on every thirteenth and on every Friday. If you want a Friday that is the thirteenth, you must write the check in the command itself, for example by testing the date inside a script.

When one of the two fields is an asterisk, only the other one counts, which is the behavior most people expect. The OR rule only matters when both are restricted.

BASH1 lines
0 6 1 * 1 /workspace/report.sh

This line runs at 06:00 on the first of each month and also at 06:00 on every Monday, because the two restricted fields combine with OR. Many people read the line as the first Monday of the month, which is not what it says.

The environment is minimal

Cron gives each job a short list of environment variables. The PATH is usually only the system directories, the HOME variable is set to the owner home, and the rest of your shell environment is absent. A command that works in your terminal can therefore fail in cron with a message such as command not found. The fix is to use absolute paths for every program, and to set PATH in the table if the job needs more.

Many crontabs begin with a PATH line that lists the directories the jobs need. The line is read as an assignment, not as a schedule, so it has no time fields.

BASH1 lines
PATH=/usr/local/bin:/usr/bin:/bin

Missing newline and extra spaces

Cron reads a table line by line, and some implementations ignore the last line of a file that has no trailing newline. A crontab that looks correct can silently drop its final job. Always end the file with a newline, which is what the echo command does by default and what most editors add. Extra spaces between the fields are fine, but a tab or a space inside a field is not.

Ranges, lists, and steps together

You can combine the three forms, but a combination that looks clever is often wrong. A range with a step, such as 1-30/5, means the values 1, 6, 11, 16, 21, 26 within the range. A list can hold ranges, such as 1-5,10. The safest approach is to write one simple form per field and read the result aloud before you save the file.

Weekdays are a common place for a mistake. The field 1-5 means Monday through Friday, because Monday is 1 and Friday is 5. The field 5-1 is not a valid range, and most implementations reject it or run it wrongly.

Warning

A typo in a time field may not cause an error. The line may be accepted and then never run, or run at a time you did not intend. Check every new line by reading it back in plain English.

Testing a line before you install it

The simplest test is to run the command by hand, with the same environment you expect cron to provide. The env command with a minimal set of variables gives you a close approximation. The second test is to check the schedule with a tool that prints the next run times, which many systems provide for crontab files.

BASH1 lines
env -i PATH=/usr/bin:/bin HOME="$HOME" date +%H:%M

The command runs date with an almost empty environment. If it works here, it is much more likely to work from cron. Use the same pattern to test the command part of a crontab line.

Consider how a schedule behaves across a daylight saving change. Cron evaluates the table in the local time of the machine, so a job set for 02:30 may run twice or not at all on the night the clocks change. A job that must run exactly once per day should record the date it last ran and skip a second run on the same date.

Finally, keep crontab lines readable. A line with a long pipeline and several redirections is hard to read in the table, so move complex work into a script and put only the schedule and the script path in the table. The table then shows the timing at a glance, and the script can be reviewed, tested, and versioned separately.

Hands-on: write lines that avoid the traps

Write two crontab lines to files. The first is a dated log line that escapes its percent signs. The second runs a weekday job at 09:00 and uses a range for the weekdays.

BASH3 lines
echo "0 0 * * * date +\%Y-\%m-\%d >> /workspace/dates.log" > dated.cron
echo "0 9 * * 1-5 /workspace/report.sh" > weekday.cron
cat dated.cron weekday.cron

The first file must contain escaped percent signs, and the second must contain the range 1-5 in the weekday field. Read both files back to be sure the backslashes and the spaces are where you expect them.

Decoding a crontab line

Visualizer

A crontab line is five time fields and then a command. Decode the fields, the day-of-month and day-of-week rule, and the two pitfalls: % and the short PATH.

1 / 5
crontab line
*/15minute
9-17hour
*day of month
*month
1-5day of week
jobcommand

A line has five time fields, then the command. Read them left to right: minute, hour, day of month, month, day of week.

for t in 09:00 09:15 17:45; do date -d "today $t" '+%a %F %H:%M'; done
Terminal lab · 2 steps · real Linux in your browser

Escape percent signs and write a weekday range

Write a dated log line with escaped percent signs, then write a weekday line.

01
Escape the percent signs
Write a crontab line that logs the date with the format %Y-%m-%d. Every percent sign must be escaped with a backslash, so cron passes it through. ``bash echo "0 0 * * * date +\%Y-\%m-\%d >> dates.log" > dated.cron `` Read the file back. The backslashes must be in front of each percent sign.
Show hint for step 1
Put a backslash before every percent sign in the date format.
02
Write a weekday range
Write a line that runs /workspace/report.sh at 09:00 on weekdays, into weekday.cron, using the range 1-5 in the weekday field. ``bash echo "0 9 * * 1-5 /workspace/report.sh" > weekday.cron `` The file should hold one line.
Show hint for step 2
Put the range 1-5 in the fifth field, after two asterisks.
Check yourself Question 1 of 4
Concept Check Unescaped percent sign recall

What does an unescaped percent sign inside a crontab command do?

Recap & Key Takeaways

An unescaped percent sign in a crontab command becomes a newline, so escape each one with a backslash.
When both day of month and day of week are restricted, cron runs the job when either one matches.
Cron runs with a minimal environment, so use absolute paths and test a line with env -i before installing it.
Always end a crontab file with a newline, and read every new time field back in plain English.
Was this lesson helpful? Spot an issue or have a suggestion? Let us know on GitHub.
Report an issue