Conditional Cron | 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.

Conditional Cron

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 03-30-2007
baanprog baanprog is offline
Registered User
 
Join Date: Mar 2005
Last Activity: 5 March 2014, 10:08 PM EST
Posts: 40
Thanks: 7
Thanked 0 Times in 0 Posts
Conditional Cron

Hi,

The following test in cron fails. Any clue?

I want it to run last day before end of month


Code:
34 13 * * * [ `date +'\%d'` -eq `cal | grep -v "^$" | tail -1 | awk '{print $NF-1}'` ] && /export/myshell.sh

Regards
Sponsored Links
    #2  
Old 03-30-2007
dennis.jacob's Avatar
dennis.jacob dennis.jacob is offline Forum Advisor  
Registered User
 
Join Date: Feb 2007
Last Activity: 30 March 2014, 9:33 AM EDT
Location: Singapore/Cochin
Posts: 871
Thanks: 0
Thanked 12 Times in 11 Posts
Quote:
Originally Posted by baanprog
Hi,

The following test in cron fails. Any clue?

I want it to run last day before end of month


Code:
34 13 * * * [ `date +'\%d'` -eq `cal | grep -v "^$" | tail -1 | awk '{print $NF-1}'` ] && /export/myshell.sh

Regards
The backslash not required...

[ `date '+%d'` -eq `cal | grep -v "^$" | tail -1 | awk '{print $NF-1}'` ] && do....what required..
Sponsored Links
    #3  
Old 03-30-2007
baanprog baanprog is offline
Registered User
 
Join Date: Mar 2005
Last Activity: 5 March 2014, 10:08 PM EST
Posts: 40
Thanks: 7
Thanked 0 Times in 0 Posts
still

The cron does not give any error, but i dont get the output file either...


regards
    #4  
Old 03-30-2007
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon (Administrator Emeritus)
 
Join Date: Aug 2001
Last Activity: 22 September 2014, 3:12 PM EDT
Location: Ashburn, Virginia
Posts: 9,925
Thanks: 60
Thanked 435 Times in 260 Posts
First, the backslash is required. From the crontab man page:
Quote:
The sixth field of a line in a crontab file is a string that is executed by the shell at the specified times. A percent character in this field (unless escaped by \) is translated to a NEWLINE character.
Some versions of cron are smart enough to remove that blackslash prior to passing it to the shell. Others just leave it there. So the shell will see:
[ \30 -eq 30 ]
Most shells will get that right but some versions of the bourne shell will not. You are probably using Solaris which has both behaviors. My advice is to put shell scripting into a shell script which is executed by a modern shell. If you must get this to work, use eval with the date command
Code:
34 13 * * * [ `eval date +'\%d'` -eq `cal | grep -v "^$" | tail -1 | awk '{print $NF-1}'` ] && /export/myshell.sh

Sponsored Links
    #5  
Old 03-30-2007
awk awk is offline
Registered User
 
Join Date: Feb 2007
Last Activity: 29 May 2012, 11:56 AM EDT
Posts: 135
Thanks: 0
Thanked 0 Times in 0 Posts
running this in ksh seems to work -

[ $(TZ=GMT-48 date +%d) -eq "01" ] && echo true

Of course, you will need to make adjustments if you are far from England.

Basically, sets the date 48 hours ahead and does the date. Today, the 30th, comes up with 01, since march has 30 days.

The cal routine would not work on months where the last day of the month was on Sunday, since that would be the first field, and $NF-1 would be the entire line (the last day), and not the day before.
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
If conditional abhinavsinha Shell Programming and Scripting 12 11-03-2008 05:30 AM
If conditional abhinavsinha UNIX for Dummies Questions & Answers 1 10-31-2008 01:48 AM
AWK - conditional cause Rafael.Buria Shell Programming and Scripting 2 01-28-2008 12:24 PM
AIX and cron logs filtering ?: /etc/cronlog.conf, /var/adm/cron/log Keith Johnson AIX 0 01-09-2008 07:32 PM
Conditional File Movement script scheduled using CRON job imu Shell Programming and Scripting 2 09-20-2005 06:30 AM



All times are GMT -4. The time now is 08:18 AM.