Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting


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

Reply    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 01-21-2013
gc_sw's Avatar
Registered User
 
Join Date: Aug 2010
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 offline Forum Advisor  
Registered User
 
Join Date: Jul 2012
Location: Aachen, Germany
Posts: 2,030
Thanks: 25
Thanked 460 Times in 445 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
Registered User
 
Join Date: Aug 2010
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 offline Forum Advisor  
Registered User
 
Join Date: Jul 2012
Location: Aachen, Germany
Posts: 2,030
Thanks: 25
Thanked 460 Times in 445 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
Registered User
 
Join Date: Aug 2010
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
Registered User
 
Join Date: May 2012
Posts: 664
Thanks: 38
Thanked 175 Times in 162 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
Registered User
 
Join Date: Aug 2010
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
Reply

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 01:17 PM.