Skip to content
shellhacks.dev
Cron and Scheduling

Run a script from a system cron file

A file in /etc/cron.d needs a user field after the five time fields, and on Debian a name with a dot is skipped. Both mistakes fail without any message.

1 min read
Summarize in AI

A job dropped into /etc/cron.d never runs, and nothing in the output says why.

Shell
mkdir /tmp/cd
cat > /etc/cron.d/with-user <<'EOF'
* * * * * root date > /tmp/cd/with-user.txt
EOF
cat > /etc/cron.d/no-user <<'EOF'
* * * * * date > /tmp/cd/no-user.txt
EOF
cat > /etc/cron.d/dotted.conf <<'EOF'
* * * * * root date > /tmp/cd/dotted.txt
EOF
sleep 65
ls /tmp/cd
Text
with-user.txt

System crontabs differ from user crontabs by one extra field, the user to run as, after the five time fields. Without it the command is read as the user name and the line is wrong, so cron drops it. Debian's cron also reads only file names made of letters, digits, hyphens and _, as the cron manual describes, so a name ending in .conf is ignored. Only one of the three jobs ran.

Cron logs the rejection to syslog, which a container without a syslog daemon does not keep, so look in journalctl -u cron or /var/log/syslog on a real host. Files in /etc/cron.d must be owned by root and not writable by others. Other cron implementations may treat file names differently.

Share

More snippets

Cron and Scheduling

Escape percent signs in a cron command

A percent sign in a crontab command ends the command and feeds the rest to stdin. Escape it with a backslash, or the date call below breaks at once.

1 min read
Cron and Scheduling

Set PATH in a crontab

Set PATH on a line at the top of a crontab, because cron runs jobs with a minimal PATH of /usr/bin and /bin and a tool in /usr/local/bin is not found.

1 min read
Cron and Scheduling

Set the shell for cron

Cron runs jobs with /bin/sh. Set SHELL=/bin/bash at the top of the crontab to use Bash features such as [[ ]], arrays and process substitution.

1 min read

Get new posts by email

One email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.