su in crontab

Robert Elz kre at munnari.OZ
Tue Jan 14 04:21:55 AEST 1986


In article <706 at astrovax.UUCP>, wls at astrovax.UUCP (William L. Sebok) writes:
> I just tripped over a rather puzzling anomaly.  If I insert 
> 
> 08 * * * * su wls % exec /usr/ucb/whoami >>/mnt/wls/TST/Errors 2>&1
> 
> I get "root" in the file /mnt/wls/TST/Errors...

% in a crontab line is replaced by a newline.  Cron executes
the resulting line with "sh -c <line>"

In 6th edition, the shell would run commands, and hand them its
standard input as their standard input, so the \n in cron lines
could (sometimes) be useful for passsing data to commands.

Since then, the standard input of the commands run by sh has been
the standard input of the shell.  For commands run by cron, standard
input is "/" (as is standard output, and standard error), so your
"su" would have been handed the contents of "/" as its input.  I
doubt if it executed many useful commands...  (But try making a file
in / with a name like "\nrm /etc/passwd\n" ...:-).  Then the "exec..."
command would have been executed by the shell after the su finally
gave up (as root).

What this all means is that the only way to pass data to commands run
by cron, is to put the data in a file, and redirect stdin, or use your
"echo" trick for simple input.  I doubt that there's any remaining use
for the '%' construct really, it could probably vanish without anyone
realizing it had gone.  4.3 has added a user name field to crontab,
so you can run commands from there as any user you like, and won't need
"su".  Not as general purpose as the SysV user crontabs, but better
than it was.

Robert Elz		seismo!munnari!kre  kre%munnari.oz at seismo.css.gov



More information about the Comp.unix.wizards mailing list