Australian cloud for business software — built and run in Sydney since 2002

NetLeverage
NetLeverage

Cron job works when run by hand but not from cron: environment, percent signs and missing output

Technical article
Troubleshooting

About this article: This article is reviewed against the cited sources. This is source research, not lab validation. Check the stated versions and your own requirements before making changes.

A command that works when you type it but does nothing from cron has four things to check first. Cron starts the job with a small environment, treats an unescaped % in the command as a line break, ignores crontab files that break its file rules, and sends the output to mail or the system log rather than your terminal. Each has a quick check; run them before you edit the schedule.

What the symptom means

Cron does not run your command in your login shell. The cron manual pages say that several environment variables are set up automatically: SHELL is set to /bin/sh, and LOGNAME and HOME are set from the /etc/passwd line of the crontab's owner. HOME and SHELL can be overridden in the crontab; LOGNAME cannot. Neither implementation's crontab(5) page lists anything else as set for you, apart from USER on BSD-style systems, and neither page gives a value for PATH. The cronie cron(8) page adds two details: the daemon "loads the PAM environment from the pam_env module", which "can be overridden by defining specific settings in the appropriate crontab file", and its -P option means "Don't set PATH. PATH is instead inherited from the environment." The cron manual pages do not say what the resulting PATH is on your system. A script that depends on a PATH entry, a variable from .bashrc, or bash-only syntax can therefore behave differently under cron.

Cron also reads the command line differently from a shell prompt. The command is everything up to a newline or a % character. Both implementations say that an unescaped % is changed into a newline, and that all data after the first % is sent to the command as standard input. Comments are not allowed on the same line as a command; they are taken to be part of the command.

Finally, the output of a job is not shown anywhere you are looking. Cron mails it to the crontab owner, or to the address in MAILTO. The cronie cron(8) page adds that if sendmail is not installed, the syslog output is used instead of mail. A job can therefore run and fail with no message in front of you.

Which implementation you have matters. The man7.org pages describe cronie, which is the daemon crond and is the one documented with crond.service. Debian's pages describe Vixie cron 3.0pl1.

Likely causes

Cause What you see How to confirm
Environment differs from your shell (PATH, variables, shell syntax) The job starts and fails with "command not found" or a missing variable, in the mail or log Print the environment from inside a cron job and compare it with your shell
Unescaped % in the command The command is cut at the first %, for example a date +%F file name Search the crontab line for %
Crontab file ignored (wrong owner, writable by others, no final newline, bad name in /etc/cron.d) Nothing runs and no start is logged for that job Check the file rules for your implementation and the cron log
Wrong format (missing user field in a system crontab, day-of-month and day-of-week both set) Job never runs, or runs on more days than expected Compare the line with the format in crontab(5)
Output goes nowhere You cannot tell whether the job ran or why it failed Redirect the output to a file for one run
Daylight saving change The cronie crontab(5) page says a job set for a time that does not exist that night is skipped; other pages differ (see Daylight saving time) Compare the job's time with the clock change

Prove the cause before changing anything

Steps 1 to 3 are read-only. Step 4 needs a temporary crontab edit, so it has its own save-first step and Disruptive step: mark.

1. Read the crontab as cron has it.

crontab -l

The cronie crontab(1) page says -l displays the current crontab on standard output. For the system file, read /etc/crontab and the files in /etc/cron.d/. Look for a % character, a trailing comment on a command line, and (in system files) the user name field between the schedule and the command.

2. Find out whether cron started the job. The cronie cron(8) page says the syslog output is used instead of mail when sendmail is not installed, and mentions a /var/log/cron log only in connection with inotify. The Debian page says cron logs to the syslog facility cron and by default logs the start of each job. On a systemd host with cronie, read the unit's journal:

journalctl -u crond.service --since "2026-11-20 08:00:00"

-u selects a unit and --since limits the time range, both as documented on the journalctl page, which says date specifications should be of the format "2012-10-30 18:17:16". Use a time shortly before the job was due, and the unit name your system uses. If you see the job starting but no output, go to step 4. If you never see the job start, go to step 3.

3. Check the file rules. For a Debian cron system file, the cron(8) page says /etc/crontab and the files in /etc/cron.d must be owned by root and must not be group- or other-writable, and that file names in /etc/cron.d must consist only of letters, digits, underscores and hyphens, so a name containing a dot is not used. For cronie, crontab(5) says crontab files must be regular files or symlinks to regular files, must not be executable or writable for anyone else but the owner, and that each entry must end in a newline. If the last entry has none, cron treats the crontab as at least partially broken and writes a warning to syslog.

Print the owner and mode of the file:

ls -l /etc/cron.d/

-l is the long listing format, as documented on the ls page.

4. See the real environment. A cron job that writes its own environment to a file shows exactly what the job sees. It needs a temporary crontab change, so follow the Disruptive step below. Compare the file with the output of env in your own shell.

Fix

Save the crontab first

Before any edit, save the current crontab under a dated name:

crontab -l > crontab-before-20261120.txt

The cronie crontab(1) page also says a backup of the last crontab is saved to $XDG_CACHE_HOME/crontab/crontab.bak when you edit or delete it, falling back to $HOME/.cache.

Capture the environment and the output

Disruptive step: adding a line to the crontab starts a new scheduled job as the crontab's owner. Add this line temporarily for a user's own crontab with crontab -e:

* * * * * env > $HOME/cron-env.txt 2>&1

It writes the environment to a file every minute (cronie's page says cron examines entries every minute). Wait for the file to appear, read it, then remove the line. Delete cron-env.txt after reading it, since it may hold secrets. To back out, delete the line in crontab -e, or reinstall the saved file with crontab crontab-before-20261120.txt. The crontab -r option removes the whole current crontab, so do not use it for this. If the user had no crontab before, delete the added line rather than reinstalling a saved file.

To keep what the job prints, redirect it in the job's own line, as the crontab(5) example does: >> $HOME/tmp/out 2>&1. That is also an edit, so it follows the same save-first and back-out steps.

Environment differences

The crontab(5) page says environment settings in a crontab take the form name = value, and that SHELL may be overridden. Disruptive step: editing the crontab replaces the user's live schedule; save it first. Set what the job needs at the top of the crontab, for example a PATH line that includes the directory of the program, or call the program by its full path. If the script uses bash syntax, name bash in the script's first line or set SHELL. Risk: running the script under env -i runs the real command; only for a job safe to run now. To test a script under a stripped environment without waiting for cron, the env page documents -i, --ignore-environment, which starts with an empty environment. That is harsher than cron, which still sets SHELL, LOGNAME and HOME.

Percent signs

Disruptive step: this edit also replaces the live crontab. Copy the saved file to a new name such as crontab-new-20261120.txt and make the change in that copy. The documented fix is to escape the % with a backslash, for example date +\%F. Both crontab(5) pages say an unescaped % is turned into a newline. Alternatively, move the command into a script and call the script. Check the syntax of the copy with crontab -T crontab-new-20261120.txt (the cronie crontab(1) synopsis is crontab [-T] <file | ->, and it describes -T as testing the file syntax without installing it; the Debian crontab(5) page does not mention it, so on Debian read the copy by eye and watch the cron log after installing). Then install the copy with crontab crontab-new-20261120.txt. To back out, install the saved file with crontab crontab-before-20261120.txt.

Ignored crontab files

Disruptive step: changing /etc/cron.d files affects every job in them. Write down the current owner and mode: ls -l /etc/cron.d/backup. Copy the file to a directory outside /etc/cron.d, for example cp /etc/cron.d/backup /root/backup-cron-saved-20261120, so the saved copy is never read as a second job. Then correct the owner or mode. To back out, copy the saved file back over /etc/cron.d/backup, set the owner and mode you wrote down, check them with ls -l /etc/cron.d/backup, and wait for cron to notice. The cron(8) pages say cron checks for changes every minute and needs no restart. Cronie adds that if inotify is in use and a crontab is a symlink, the daemon must receive a SIGHUP to reload.

Daylight saving time

The cronie crontab(5) page says non-existent times during a daylight saving change never match, so jobs scheduled in the missing hour do not run, and repeated times run matching jobs twice. The cronie cron(8) page describes it differently: if time was adjusted one hour forward, jobs that would have run in the skipped interval "will be run immediately", and if time was adjusted backward, running the same job twice is avoided. The two cronie pages disagree, so test your own system. The Debian cron(8) page says that for clock changes under 3 hours, jobs skipped by a forward change are run soon after, and jobs in a repeated time are not run again, for jobs set at a fixed time. Move such jobs outside the change window.

Example (illustrative): On web01, the user deploy has this line:

0 2 * * * tar czf /srv/backups/site-$(date +%F).tgz /var/www

It works when typed at a prompt, but no archive appears. The % in +%F ends the command there, and cron sends the rest to it as standard input. Changing the line to date +\%F fixes it. The same user's script also calls a tool in /usr/local/bin by bare name. The environment file from the temporary job shows the tool's directory is not in the job's PATH, so the script now uses the full path.

Confirm the fix

  1. The job's start appears in journalctl -u crond.service (or the syslog cron facility on Debian).
  2. The file or action the job should produce appears, at the next scheduled time.
  3. The output file, if you used one, shows no error.
  4. The temporary environment job is gone from crontab -l, and the saved crontab file is kept until the fix has run once.

What to watch afterwards

  • Have the job write its own success or failure line, and check for that line, instead of relying on mail.
  • After a clock change, check that scheduled jobs ran.
  • Keep crontab files in version control or a dated copy, since crontab -r removes the whole current crontab, and only the cronie manual documents a backup of the last crontab.
  • Check the cron log after package updates that touch /etc/cron.d.

Risks and limits

  • These commands have not been run or lab-tested.
  • No default PATH value is stated, because neither crontab(5) page gives one.
  • Distributions may name the cron unit differently. Only crond.service is documented on the cronie page.
  • Anacron, at and systemd timers follow different rules and are not covered.

Sources

  • crontab(5) — cronie manual page on man7.org, cronie 2012-11-22, accessed 20 November 2026
  • crontab(1) — cronie manual page on man7.org, cronie 2019-10-29, accessed 20 November 2026
  • cron(8) — cronie manual page on man7.org, cronie 2013-09-26, accessed 20 November 2026
  • crontab(5), Debian bookworm — Debian Manpages, cron 3.0pl1-162, page dated 24 January 1994 in its footer, source file last updated 2023-03-02, accessed 20 November 2026
  • cron(8), Debian bookworm — Debian Manpages, cron 3.0pl1-162, 19 April 2010 page date, accessed 20 November 2026
  • journalctl(1) — systemd 262~devel (development manual), colophon: obtained 2026-08-04, most recent upstream commit 2026-08-03, accessed 20 November 2026
  • ls(1) — GNU coreutils 9.11, April 2026, accessed 20 November 2026
  • env(1) — GNU coreutils 9.11, April 2026, accessed 20 November 2026
  • cp(1) — GNU coreutils 9.11, April 2026, accessed 20 November 2026