Crontab is not working in printf

hi, i have a script as:

printf '%s -> %s\n' "$(date '+%Y-%m-%d %H:%M')" "$(/opt/gcsw/status -ne | fgrep 'State:2' | wc)"

which gives output as: 2013-01-18 13:00 -> 80 480 6529 and it is working fine.

now I want to put this into cronjob and write the output to a file in every 5 minutes:

0,5,10,15,20,25,30,35,40,45,50,55 * * * * printf '%s -> %s\n' "$(date '+%Y-%m-%d %H:%M')" "$(/opt/gcsw/status -ne | fgrep 'State:2' | wc)" >> /var/tmp/gcsw/status.txt

i want to see the output as:

2013-01-18 13:00 -> 80 480 6529
2013-01-18 13:05 -> 85 480 6529
2013-01-18 13:10 -> 80 423 6329
2013-01-18 13:15 -> 71 460 6129
...

but file is not being updated :confused: i think there is a problem with printf with cronjob. any help?

Any error msgs? Pls keep in mind that processes launched by cron run in a reduced environment, and perhaps in another shell, so are you sure all commands are on the PATH var?

there is not any error message but file is not being updated :frowning: i didnot assign anything with PATH how can i solve it?

a) did you look into the syslog files for cron entries?
b) enter PATH=... into crontab (cf man crontab )

a) there is no entry related to cron in syslog
b) $PATH is defined as

/usr/sbin:/usr/bin:/usr/ccs/bin:/usr/openwin/bin:/usr/dt/bin:/usr/platform/SUNW,Sun-Fire-T200/sbin:/opt/SUNWexplo/bin:/opt/SUNWsneep/bin:/opt/CTEact/bin

can we modify the command without using printf ?

The % character has a special meaning in crontab entries.

man crontab
...

You need to escape each % as \%;
'text1%text2' becomes 'text1'\%'text2' and "text3%text4" becomes "text3"\%"text4" (i.e. two strings with a \% in between).

printf '%s -> %s\n' "$(date '+%Y-%m-%d %H:%M')" ...

becomes

printf \%'s -> '\%'s\n' "$(date '+'\%'Y-'\%'m-'\%'d '\%'H:'\%'M')" ...

This is so ugly that you better put the original code in an executable script, and run the script from the crontab.

2 Likes

i have created a file (/var/tmp/gcsw/script.sh) and write:

printf '%s -> %s\n' "$(date '+%Y-%m-%d %H:%M')" "$(/opt/gcsw/gwstatus -ne | fgrep 'State:2' | wc)" >> /var/tmp/gcsw/status.txt

and created a cronjob as:

0,5,10,15,20,25,30,35,40,45,50,55 * * * * /var/tmp/gcsw/script.sh

but output /var/tmp/gokhan/status.txt is not correct. so cronjob is not working :frowning:

PS: when I manually run:

printf '%s -> %s\n' "$(date '+%Y-%m-%d %H:%M')" "$(/opt/gcsw/gwstatus -ne | fgrep 'State:2' | wc)" >> /var/tmp/gcsw/status.txt

it is successfully OK.

And if you run the /var/tmp/gcsw/script.sh on the command line?

Ensure it is executable,
and put a first line #!/bin/bash
to ensure it is the correct shell (your commands require bash or ksh or a Posix-sh).
Additionally, as others posted, you can ensure that PATH in the script is equal to your current PATH. Do
echo $PATH
and insert a 2nd line in your script
export PATH=yourcurrentpath
where you replace yourcurrentpath with the output of the echo command.

Ensure, your crontab entry was done with
crontab -e
and is visible with
crontab -l

Finally, if the script works on your command line but still not in the crontab, check the cron log file. (The log file is dependent on your operating system.)

1 Like