Simpler crontab entry to execute pgm on last day of the month


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Simpler crontab entry to execute pgm on last day of the month
# 1  
Old 03-17-2015
Simpler crontab entry to execute pgm on last day of the month

The following bash command line works for the last day of the month. Test by replacing the 1 with tomorrows day of month number
Code:
[ 1 == `date +%d -d '1 day'` ] && echo "Day before tomorrow"

Can it be used within crontab? As

Code:
*  * 28-31 *  *  [  '1' == `date +\%d -d '1 day'` ] && echo "Today ls last day of month" >>/tmp/crontabtest

I tried to test crontab with the above entry (forcing dates) and I was was not able to get results

Last edited by lsatenstein; 03-17-2015 at 07:48 AM.. Reason: Discovered that %d in crontab needs % to be escaped
# 2  
Old 03-17-2015
What operating system are you using?

When the command works from the command line, what is the output from the command?:
Code:
type date

Do you really want to run this command every minute of every hour 1 to 4 days every month?

The correct syntax for a numeric comparison in [ expression ] is:
Code:
[ 1 -eq `date +%d -d '1 day'` ]

not:
Code:
[ 1 == `date +%d -d '1 day'` ]

assuming that the date utility found on the system's default $PATH setting accepts a -d option on your system.

date +%d prints 01 on the 1st; which is not the same as 1 when performing a string comparison. You could also try a string comparison (which should use =; not ==), if you use date +%e instead of %d:
Code:
[ 1 = `date +%e -d '1 day'` ]

and this is probably safer than a numeric comparison. (Note that:
Code:
[ 1 -eq `date +%d -d '1 day'` ]

will give you a syntax error in some shells on the 8th and 9th days of the month because 08 and 09 are not valid octal values.)
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Dynamic crontab entry for day and night

I have query apply crontab entry that the script executes as below day hours (before 8:00 PM and after 7:00 AM) execute every 5 min in Night hours (after 8:00 PM to 7:00 AM) executes every 15 min How can we set such entry in crontab ? (4 Replies)
Discussion started by: kaushik02018
4 Replies

2. UNIX for Dummies Questions & Answers

Simpler next month year program

I have created this program to get the next month and year. Is there a simpler way. #!/bin/ksh string=`cat Date.txt` year=`echo $string | cut -c 1-4` month=`echo $string | cut -c 5-6` echo $year$month mon=`expr $month + 1` if ; then mon=0$mon echo $mon fi if ; then month=01 ... (2 Replies)
Discussion started by: w020637
2 Replies

3. UNIX for Dummies Questions & Answers

Running Script via Crontab on 2nd Working day each month

Hello Guys, I have a questions regarding running a shell script every second working day each month. I have no clue how solve this problem :wall:. Important is that it has to be the second working (Mo-Fr). Example: If 1st and 2nd Days of month are Sat and Sun the script must run on 4th day... (5 Replies)
Discussion started by: Hollo
5 Replies

4. Shell Programming and Scripting

Script to counting a specific word in a logfile on each day of this month, last month etc

Hello All, I am trying to come up with a shell script to count a specific word in a logfile on each day of this month, last month and the month before. I need to produce this report and email it to customer. Any ideas would be appreciated! (5 Replies)
Discussion started by: pnara2
5 Replies

5. Shell Programming and Scripting

Crontab to skip only one entry in a day???

Hello Friends, I have a cron tab like this: 10,40 1-23 * * * /script i want to skip only one execution at 00:10, so basically i want it to execute every hour at 10th and 40th minute, except 00:10. Could anyone help me doing this Thanks folks :b: (4 Replies)
Discussion started by: Prateek007
4 Replies

6. Shell Programming and Scripting

Code creates day 32 instead of 1st day of next month.

I am using the code below modified from a post I saw here regarding having the script write out future dates. The problem is that instead of making 8/1 it makes 7/32! Please help! yy=`date +%Y` mm=`date +%m` dd=`date +%d` echo "Today is : $yy $mm $dd" #!/usr/bin/ksh date '+%m... (5 Replies)
Discussion started by: libertyforall
5 Replies

7. Solaris

crontab entry to run a script on 1st of every month.

What should be the crontab entry in solaris to run a script on 1st of every month? Is this correct? 00 02 1 * * <script to be executed> (5 Replies)
Discussion started by: deepaksahni0109
5 Replies

8. Shell Programming and Scripting

Script to find previous month last day minus one day timestamp

Hi All, I need to find the previous month last day minus one day, using shell script. Can you guys help me to do this. My Requirment is as below: Input for me will be 2000909(YYYYMM) I need the previous months last day minus 1 day timestamp. That is i need 2000908 months last day minus ... (3 Replies)
Discussion started by: girish.raos
3 Replies

9. Shell Programming and Scripting

crontab entry to run every last day of the month

i've created a script which should run every last day of the month. what would be the exact crontab entry for this? thanks! (9 Replies)
Discussion started by: tads98
9 Replies

10. Shell Programming and Scripting

Write a shell script to find whether the first day of the month is a working day

Hi , I am relatively new to unix... Can u pls help me out to find out if the first day of the month is a working day ie from (Monday to Friday)...using Date and If clause in Korn shell.. This is very urgent. Thanks for ur help... (7 Replies)
Discussion started by: phani
7 Replies
Login or Register to Ask a Question