Cron for end of month


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Cron for end of month
# 1  
Old 05-20-2012
Cron for end of month

Hello,

I'm on FreeBSD 8.2-STABLE shell and I was trying the following cron entry for end of month:
Code:
59 23 * * * [ `date +%d` -eq `echo \`cal\` | awk '{print $NF}'` ] && /home/user/eomt.sh >/dev/null 2>&1


I get the following error
Code:
Subject: Cron <user@shell> [ `date +

Syntax error: EOF in backquote substitution
Syntax error: Error in command substitution

eomt.sh contains the following codes

Code:
#!/bin/sh
  rm /home/user/tst/logs/* && rm /home/user/tst/stats/*
exit 0


your help will be greatly appreciated.

regards
user

Last edited by Franklin52; 05-21-2012 at 03:50 AM.. Reason: Please use code tags
# 2  
Old 05-20-2012
Probably best to run job every night:
Code:
59 23 * * * /home/user/eomt.sh >/dev/null 2>&1

and check for end-of-month in eomt.sh:
Code:
#!/bin/sh
if [ `date +%d` -eq `awk '$NF{E=$NF} END{printf E}'` ]
then
         rm /home/user/tst/logs/* && rm /home/user/tst/stats/*
fi
exit 0

This User Gave Thanks to Chubler_XL For This Post:
# 3  
Old 05-20-2012
The percent sign in a command must be escaped (\%). Alone it is treated as a newline which is likely the cause of the error indicated.
This User Gave Thanks to agama For This Post:
# 4  
Old 05-21-2012
Chubler_XL and agama, thanks so much, appreciate it. I'll try your suggestions and will give back my input.
# 5  
Old 05-21-2012
Code:
eom=$(cal | tr -s '\n' ' ' | awk '{print $(NF)}')
if [ "$(date +%e)" = "$eom" ] ; then
    # do something
fi

Generally as Chubler points out, you want this script to run every day of the month, but just do some work on the last day. If you have GNU date or can install it, life will be easier for this kind of thing. And do not run it too close to midnight in case some other major system hog is running and pre-empts your process right into next month. We had that happen on an oracle server that gets the heck beat out of it.
This User Gave Thanks to jim mcnamara For This Post:
# 6  
Old 05-22-2012
Jim, thank you so much for the nice suggestion
# 7  
Old 06-01-2012
Hi, just wanted to give updates regarding the end of month script. I tried Chubler_XL codes and unfortunately it didn't work. I was waiting for 05/31.

This is what i have tried:

Code:
59 23 * * * /home/user/eomt.sh >/dev/null 2>&1

eomt.sh:

Code:
#!/bin/sh

if [ `date +%d` -eq `awk '$NF{E=$NF} END{printf E}'` ]
then
    rm /home/user/tst/logs/* && rm /home/user/tst/stats/*
fi
exit 0

I also tried to run the script manually by using ./eomt.sh and it doesn't even come out.

best regards
user
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Get the next month end date

how to get the next month end date. dt=01312014 output=02282014 (2 Replies)
Discussion started by: w020637
2 Replies

2. Shell Programming and Scripting

cron job to run on second to last friday each month

I needed a cron job to run on the second to last friday of every month. Our servers are running HP-UX, and the HP-UX date command is pretty basic and does not have all of the fancy options that Linux date command does, and it does not have the ability at all to return future dates. So I had to... (0 Replies)
Discussion started by: lupin..the..3rd
0 Replies

3. Shell Programming and Scripting

Displaying time left to end of day, week, month?

Hello, basically I'm looking after a way of showing how many time is left to the end the day, week, month... I can't seem to figure this out, so could one of you skilled guys tell me how should this be approached? Examples: Time left: Day: 12h 12m 11s Week: 4d 12h 12m 11s Month:... (4 Replies)
Discussion started by: TehOne
4 Replies

4. Shell Programming and Scripting

fetching Month end using cal

Hi , Can someone please assist to get monthend using cal utility and monthend shouldn't be saturday or sunday. Cheers, gehlnar (2 Replies)
Discussion started by: gehlnar
2 Replies

5. Shell Programming and Scripting

End of Month - Cron Jobs

I am running AIX 5.3 with the default shell as ksh. I would like to know how to run a cron job on this system the last calendar day of the month. I found an example, but, I'm not sure if it will work with AIX and ksh... 02 16 * * * && /home/user/job-eom.kshI also need to be able to run a... (2 Replies)
Discussion started by: jthompson333
2 Replies

6. UNIX for Advanced & Expert Users

last month end date

Hi, How to get the lastmonth's end date? (have current date) through s shell script. I need to get it and pass it to a procedure. Please advice. Thanks in advance. (7 Replies)
Discussion started by: vanathi
7 Replies

7. UNIX for Dummies Questions & Answers

Cron job -- to execute at every first Sunday of every month

Dear all How can I schedule the cronjob to be run sometime at every first Sunday at every month? I have the edit the cronjob every month now, thanks (2 Replies)
Discussion started by: shanemcmahon
2 Replies

8. UNIX for Dummies Questions & Answers

setting up cron job for month end week report

Hi all, Needs your help in scheduling a cron job for the below mentioned requirement. Just let me know if anybody has a similar job running as mentioned below: Month end reports - Report of all items created in a parent table that were created during the week. Presently this report runs... (3 Replies)
Discussion started by: Bhups
3 Replies

9. UNIX for Advanced & Expert Users

Unix Cron Month End Processing

Is it possible to use cron to schedule a job that will run the first 3 Sunday's during a month in addition to the last day of the month? (3 Replies)
Discussion started by: cpete
3 Replies

10. Shell Programming and Scripting

Cron to schedule 1st Wednesday of every Month??

Is there a way to use cron to run a job on the 1st Wednesday of every month?? The more generic question is, "Are all of the fields in crontab AND'd together or are some OR'd?". i.e. I had an entry in my crontab this morning as follows and I didn't expect it to run since the day of month is 20... (5 Replies)
Discussion started by: bradtri
5 Replies
Login or Register to Ask a Question