summary: shell commands in crontab

From: Edwin R Wolfe Jr. <ewolfe_at_umich.edu>
Date: Thu, 06 Aug 1998 17:08:28 -0400 (EDT)

Thanks to all who responded -- I've tried the suggestions, but still can't
get what I was after.

original post:

I was trying to test a crontab job and, to see if it was executed, I tried

/bin/touch /tmp/file.`date +%Y%m%d` and
/bin/csh ; /bin/touch /tmp/filea.`date +%Y%m%d`
 
to create an empty file with a datestamp as its name.

It doesn't entirely work: files named file. and filea. are created, with
the expected creation time??

Issuing the touch command from the command line (in sh, and csh) works
fine.

Could someone tell me what dumb mistake I'm making?

^^^^^^^^
Responses (A):

alan_at_nabeth.cxo.dec.com:
Is "date" in path that crontab uses to run commands? If
        not, specify the full path of "date".
        
Drew Kramer:
Offhand I would try a "/usr/bin/date" instead of "date". Also if you just
do a "printenv > /tmp/foo.out" in the crontab it will no doubt open your eyes
a bit wider.

Dale Inman:
remember that cron jobs do not pick up any environment variables from
profile or .profile. If you place these in a shell script with " .
/etc/profile" then your commands should be more sucessful.

Andrew Weston:
Try `/sbin/date +%%%m%d`.
I noticed that some background scripting requires the full path for
all commands as the PATH is not retained.

\\\\\\\\\\\\

I thought 'ah ha' this sure looks like a dumb mistake. So, I tried using
the full path. No change. I did the printenv > /tmp/foo and find that it
lists my HOME, LOGNAME, PATH=:/usr/bin, and SHELL=/usr/bin/sh. The man
page for crontab says that cron supplies these for every shell. So it
seems, at least for touch and date, the path is known and seems to explain
why no change.

Tried
. /etc/profile; /usr/bin/touch /tmp/file.`/usr/bin/date +%Y%m%d` and
. /etc/profile; /usr/bin/touch /udsk11/ewolfe/tmp/filea.`/usr/bin/date
'+%Y%m%d'`
and got file. and filea. with no mail err message

\\\\\\\\\\\\

^^^^^^^
Responses (B):
John Robens, Alex Nord, Mike Metelski:
have you tried putting forward ticks around the date format string:
/usr/bin/touch /tmp/file.`/sbin/date '+%Y%m%d'`

\\\\\\\\\\\\
this begets the same response: "file." is created, no error mail. But if
I say:
/bin/csh /usr/bin/touch /tmp/filea.`/sbin/date +%Y%m%d` or
/bin/csh /usr/bin/touch /tmp/filea.`/sbin/date '+%Y%m%d'`
no file is created and I get a mail message:
2P^G0^K^A^K^C: Command not found.
_at_: Syntax error.

If I say </bin/csh;> (just added a semicolon) then filea. is created and
the error message becomes
Y: Command not found.
m: Command not found.
Unmatched `.

\\\\\\\\\\\\


^^^^^^^^
Responses (C):
Dennis MacDonell, Lawrence Decker suggested something like this:
DATE=`date +%Y%m%d`
 /bin/touch /tmp/file.$DATE

\\\\\\\\\\\\
I tried
. /etc/profile sh; DATE=`/usr/bin/date +%Y%m%d`; /usr/bin/touch
"/tmp/fileb.$DATE"
. /etc/profile sh; DATE=`/usr/bin/date '+%Y%m%d'`; /usr/bin/touch
"/tmp/filec.$DATE"
with and without the "", with and without the . /etc/profile sh;
This time, no files were created, no error mail message?? Howcome not even
fileb. and filec. ?

\\\\\\\\\\\\

^^^^^^^^^
Responses (D):
Ronald D. Bowman, Nathan Grass, Picard, Marc:
suggested a single line in the crontab that ran a script with the commands
I wanted.

\\\\\\\\\\\\
I created the script
touch /tmp/filek.`date '+%Y%m%d'`
touch /tmp/fileka.`date +%Y%m%d`
DATE=`date +%Y%m%d`; touch /tmp/filekb.$DATE

and made an entry in the crontab file to invoke it. Worked fine (with or
without specifying the full paths to touch and date), all 3 files
were created with the timestamp and no error mail message.
\\\\\\\\\\\\


So, it appears that the solution is the simplest thing: make a script to
run from crontab. But I would like to know why this formatted date
command won't work directly from the crontab. How do the rules for crontab
differ from a regular sh script?
Received on Thu Aug 06 1998 - 21:10:01 NZST

This archive was generated by hypermail 2.4.0 : Wed Nov 08 2023 - 11:53:38 NZDT