Need help in running script on last day of month


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need help in running script on last day of month
# 1  
Old 10-31-2011
Need help in running script on last day of month

Hello Experts/Guru,

I need a help in running the script on every month last day....

PS: due to some constrain I can't schedule in crontab

Requirement:

On Jan 31st i want to run some script, similarly on Feb 28th, March 31st, April 30th......till Dec 31st.

Please help me by providing me a script...

Thanks a lot for your help in advance
# 2  
Old 10-31-2011
# 3  
Old 10-31-2011
Hello Amit,

Can you please help me clearing one doubt....

If i run the below script...
Code:
#!/bin/bash

TODAY=`/bin/date +%d`
TOMORROW=`/bin/date +%d -d "1 day"`

# See if tomorrow's day is less than today's
if [ $TOMORROW -lt $TODAY ]; then
        exit 0
fi

exit 1

it will run for every day which less than tomorrow, like it will on 29 coz 29 is less than 30, it will run on 30 coz 30 is less than 31st....what happen if date is 28th Feb...!!!

and my requirement is it should run on last day of every month....I can't do scheduling in crontab, due to some constrain......please help me in achieving it through script.....

Thanks in advance

Moderator's Comments:
Mod Comment Use code tag! See PM.

Last edited by zaxxon; 10-31-2011 at 06:11 AM.. Reason: code tags, see PM
# 4  
Old 10-31-2011
Try this...
Code:
#!/bin/bash
day=$(date +%d)
month=$(date +%m)
year=$(date +%Y)
run_or_not=$( awk -v day=$day -v month=$month -v year=$year 'BEGIN {
            if(month%2 && day == 30){ print "run" }else {
            if(month == 2){ leap=(0 == year % 4 && 0 != year % 100 || 0 == year % 400)
            if(leap && day == 29 ){ print "run" } else if(!leap && day == 28){ print "run"}
            } else if(day == 31){ print "run" } } }'  )
if [ "$run_or_not" == "run" ]
then
  echo "Execute your script!"
fi

--ahamed
This User Gave Thanks to ahamed101 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script to run on 2 4 and 7 day of the month

I am looking for a unix script which could run a job on 2, 4 and 7 working day of the month. if the days are falling on the saturday/sunday. it should run on the next day. Thank you.. (9 Replies)
Discussion started by: tradingspecial
9 Replies

2. Shell Programming and Scripting

Julian day to dates in YEAR-MONTH-DAY

hello, I have many files called day001, day002, day003 and I want to rename them by day20070101, day20070102, etc. I need to do it for several years and leap years as well. What is the best way to do it ? Thank you. (1 Reply)
Discussion started by: Ggg
1 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

run the script for last day of the month

Hello Experts, I have a script which i want to run the on last day of every month. let say I have backup.sh script which i want to run it every month last day. Can anyone please help :confused: thanks (4 Replies)
Discussion started by: aks_1902
4 Replies

5. 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

6. UNIX for Advanced & Expert Users

Autosys Job Running every 5th Day of Month

Hi, Is there a way to schedule a job in Autosys to run every 5th Day of Month without using custom calendar? Thanks. (1 Reply)
Discussion started by: vbhatnag
1 Replies

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

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. UNIX for Dummies Questions & Answers

cron script -run every 2nd day of month except Monday

I know I can't schedule this in cron and would have to write a wrapper around my script and schedule it in cron ....but not sure how do to this? How do I exclude Monday if the 2nd day of the month falls on a Monday? Thanks. I tried this: 0 0 2 * 0,2-6 command And I know this doesnt... (2 Replies)
Discussion started by: newtou
2 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