Operating System - HP-UX
1834479 Members
3525 Online
110067 Solutions
New Discussion

Re: Cron and posix programming

 
Todd Bowden
Valued Contributor

Cron and posix programming

This should be an easy one for someone out there.

Im trying to run a command and redirect it to a file.

The command does not matter but here is the problem Im having after redirection.

On the command line I can run "some_command > /tmp/temp_`date +%m%d`.log" and it works the way we want it, but it does not work running from cron. I know it has to be some environment variable but Im at a loss. The file when run from cron ends up cutting off after the underscore. Any help would be most appreciated.
6 REPLIES 6
John Palmer
Honored Contributor

Re: Cron and posix programming

I don't think that it's an environment variable, I think that it's the way that cron issues the command.

One workaround is to get cron to call a script that itself calls
some_command > /tmp/temp_`date +%m%d`.log

If some_command is a script that you have control over then get it to redirect its standard output/error with:-

exec 1> 2>&1
Todd Bowden
Valued Contributor

Re: Cron and posix programming

That would be a good work around, but can I insert that `date +%m%d` to append it to the log file in the crontab entry? If I can how?
John Palmer
Honored Contributor

Re: Cron and posix programming

Yes there is...

After trying the same thing myself and checking cron's log (/var/adm/cron/log) it appears that cron doesn't like the % characters.

If you escape them with backslash then it works eg

bdf > /tmp/test.`date +\%m\%d`.log

Devbinder Singh Marway
Valued Contributor

Re: Cron and posix programming

why don't you script i.e. specify $1 as your command i.e. :-
#!/bin/ksh
COMMAND=$1

DDD=$(date +%m%d)

$1 >/tmp/temp_$DDD.log

in cron

time * * * script bdf

just a thought
Seek and you shall find
Todd Bowden
Valued Contributor

Re: Cron and posix programming

Ok, how do I give myself points? HAHA

I found the answer I was looking for.

in cron it interprets % characters as new lines. Here is a excerpt from crontab(1m)

A percent character (%) in this field (unless
escaped by a backslash ()) is translated to a newline character,
dividing the field into "lines". Only the first "line" (up to a % or
end-of-line) of the command field is executed by the shell. Any other
"lines" are made available to the command as standard input.


so when I do the following in cron it works:

some_command > /tmp/temp_`date +%m%d`.log

you have to escape the % signs.
unix team_4
Occasional Advisor

Re: Cron and posix programming

I put in front of % in my cron jobs.

33,03 * * * * xx=`date +%d`; date +"%D.%X" >>/var/adm/sa/vmstat.$xx