Technical article 104821

I am getting unexpected results from cron(C); the crontab file contains "%"

OpenServer 5.0.0

Written · Last revised

Problem

I am getting some unusual results from cron(C), when my
crontab(C) file contains '%' percent characters.

Cause

Field 6 of a crontab file entry holds the command to be
executed.  In this field, a '%' character is used to indicate
the end of line, and text after the '%' is treated as stdin
information to be passed to the command.
If a '%' occurs in a command argument, for example, a format
string for a date(C) command, it is translated into newline,
even if it is in quotes.  The crontab(C) field 6 entry
date '+%T' >FOO

actually results in no FOO file being created, but instead in the full date being written to stdout, and subsequently mailed to the user by cron.

Solution

The '\' backslash character can be used to escape the '%'
percent character in a crontab(C) file.  Note however that
the escaping backslash is itself passed to the command in
whose argument it appears, so it may be necessary to exercise
some care in any quoting used.

Following is an example of the use of '%' percent characters, both escaped and otherwise, in field 6 of a crontab(C) entry:

date +\%T >FOO; cat >>FOO%cfile line 1%cfile line 2
This will create a file containing
10:27:01
cfile line 1
cfile line 2
Note that the date parameter +\%T is not quoted.  If single
quotes are placed around this parameter, the first line of
the file becomes
\10:27:01

since the '\' is passed to date(C).

From the Xinuos technical article database, record 104821. Reproduced with permission. The original is at www.sco.com/ta/104821, which is reachable but blocked from search engines. Xinuos marks this article against a retired product line. That refers to the product’s lifecycle, not to whether the advice still works — plenty of it still applies to running systems.

Next step

Need this fixed rather than explained?

Tell us the release, what the system runs and what it is doing. We will reply by email with what we would do next.

  • You deal with the people who do the work
  • No obligation, and no upgrade required before we will help
  • We tell you plainly if something is not worth doing

Send an enquiry

We reply by email. If a server is down, say so in the first line. You can also write to help@scoexpert.com.