Crontab is not working in printf | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Crontab is not working in printf

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 01-21-2013
gc_sw's Avatar
gc_sw gc_sw is offline
Registered User
 
Join Date: Aug 2010
Last Activity: 7 June 2013, 4:29 AM EDT
Location: Under my feet
Posts: 146
Thanks: 106
Thanked 1 Time in 1 Post
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:
Code:
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:

Code:
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 i think there is a problem with printf with cronjob. any help?

Last edited by gc_sw; 01-21-2013 at 04:18 AM..
Sponsored Links
    #2  
Old 01-21-2013
RudiC RudiC is online now Forum Advisor  
Registered User
 
Join Date: Jul 2012
Last Activity: 27 August 2014, 4:50 AM EDT
Location: Aachen, Germany
Posts: 4,116
Thanks: 67
Thanked 979 Times in 928 Posts
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?
Sponsored Links
    #3  
Old 01-21-2013
gc_sw's Avatar
gc_sw gc_sw is offline
Registered User
 
Join Date: Aug 2010
Last Activity: 7 June 2013, 4:29 AM EDT
Location: Under my feet
Posts: 146
Thanks: 106
Thanked 1 Time in 1 Post
there is not any error message but file is not being updated i didnot assign anything with PATH how can i solve it?
    #4  
Old 01-21-2013
RudiC RudiC is online now Forum Advisor  
Registered User
 
Join Date: Jul 2012
Last Activity: 27 August 2014, 4:50 AM EDT
Location: Aachen, Germany
Posts: 4,116
Thanks: 67
Thanked 979 Times in 928 Posts
a) did you look into the syslog files for cron entries?
b) enter PATH=... into crontab (cf man crontab )
Sponsored Links
    #5  
Old 01-21-2013
gc_sw's Avatar
gc_sw gc_sw is offline
Registered User
 
Join Date: Aug 2010
Last Activity: 7 June 2013, 4:29 AM EDT
Location: Under my feet
Posts: 146
Thanks: 106
Thanked 1 Time in 1 Post
a) there is no entry related to cron in syslog
b) $PATH is defined as
Code:
/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 ?
Sponsored Links
    #6  
Old 01-21-2013
MadeInGermany MadeInGermany is offline Forum Advisor  
Registered User
 
Join Date: May 2012
Last Activity: 26 August 2014, 11:46 AM EDT
Location: Simplicity
Posts: 1,844
Thanks: 117
Thanked 535 Times in 484 Posts
The % character has a special meaning in crontab entries.

Code:
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).

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

becomes

Code:
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.

Last edited by MadeInGermany; 01-21-2013 at 06:48 AM..
The Following 2 Users Say Thank You to MadeInGermany For This Useful Post:
Corona688 (01-21-2013), elixir_sinari (01-21-2013)
Sponsored Links
    #7  
Old 01-21-2013
gc_sw's Avatar
gc_sw gc_sw is offline
Registered User
 
Join Date: Aug 2010
Last Activity: 7 June 2013, 4:29 AM EDT
Location: Under my feet
Posts: 146
Thanks: 106
Thanked 1 Time in 1 Post
i have created a file (/var/tmp/gcsw/script.sh) and write:
Code:
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:
Code:
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

PS: when I manually run:
Code:
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.
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
crontab is not working!! getrue Red Hat 14 09-08-2011 11:06 AM
crontab not working shifahim UNIX for Dummies Questions & Answers 8 08-16-2011 08:22 AM
working of printf() zius_oram Programming 7 08-28-2010 12:37 PM
crontab NOT working baanprog UNIX for Advanced & Expert Users 2 09-26-2006 12:11 PM
crontab not working right kymberm UNIX for Dummies Questions & Answers 3 07-09-2003 04:21 PM



All times are GMT -4. The time now is 04:50 AM.