LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-13-2009, 10:52 PM   #1
dhar
LQ Newbie
 
Registered: Nov 2009
Posts: 4

Rep: Reputation: 0
cron for last day of month


Hi,

I searched the forum to find the cron for last day of month and i found the following by colucix:

58 23 * * * [ $(date +%d) -eq $(echo $(cal) | awk '{print $NF}') ] && myJob.sh

When i try this i get the following error message:

Subject: Cron <dhar@shell> [ $(date +

Syntax error: end of file unexpected (expecting ")")

Your help would be greatly appreciated.

Thanks
Dhar
 
Old 11-14-2009, 12:15 AM   #2
ksorge
LQ Newbie
 
Registered: Jun 2008
Posts: 3

Rep: Reputation: 1
Hi Dhar,

You are close with your date calculation, but why don't you try this:

Code:
58 23 * * * [ `date +%d` -eq `cal | sed '/^$/d' | tail -1 | awk '{print $NF}'` ] && myJob.sh
cal - prints the calendar with a blank line at the end (maybe)
sed '/^$/d' - will get rid of the blank line
tail -1 - print only the last non-blank line
awk '{print %NF}' - get the (N)umber of (F)ields column, or the last column in the line

Thanks,
ksorge
 
Old 11-14-2009, 09:16 AM   #3
dhar
LQ Newbie
 
Registered: Nov 2009
Posts: 4

Original Poster
Rep: Reputation: 0
hi ksorge,

I tried your code too and i get the following error:

Subject: Cron <dhar@shell> [ `date +

Syntax error: EOF in backquote substitution

by the way shell is -> FreeBSD 7.1-STABLE


Thanks

Dhar
 
Old 11-14-2009, 09:36 AM   #4
fpmurphy
Member
 
Registered: Jan 2009
Location: /dev/ph
Distribution: Fedora, Ubuntu, Redhat, Centos
Posts: 299

Rep: Reputation: 62
I suggest that you move the last day of the month logic out of your cron job and into your shell script. Them run your shell script every day or alternatively on days 28-31. The script then tests the date and does nothing if the date is not actual the last day of the month.

Last edited by fpmurphy; 11-14-2009 at 09:39 AM.
 
Old 11-14-2009, 10:18 AM   #5
dhar
LQ Newbie
 
Registered: Nov 2009
Posts: 4

Original Poster
Rep: Reputation: 0
Thanks

So i can write the script something like this?

#!/bin/sh
[ $(date +%d) -eq $(echo $(cal) | awk '{print $NF}') ]


Thanks
 
Old 11-14-2009, 11:25 AM   #6
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984
Hi Dhar,

the problem in your crontab entry is that the % sign has a special meaning in crontabs. Look carefully at the crontab manual and you will find the explanation:
Quote:
Percent-signs (%) in the command, unless escaped with backslash (\), will be changed into newline characters, and all data after the first % will be sent to the command as standard input.
hence a correct entry would be:
Code:
58 23 * * * [ $(date +\%d) -eq $(echo $(cal) | awk '{print $NF}') ] && /path/to/myJob.sh
You could take also the suggestion by fpmurphy by simply putting this check at the beginning of your script and execute the rest of the code only if the condition is matched, e.g.
Code:
#!/bin/sh
if [ $(date +%d) -eq $(echo $(cal) | awk '{print $NF}') ]
then
  <-- your code here -->
fi
Also don't forget to put full path of your scripts/commands in crontab entries and into the script itself. Indeed, cron has a very limited environment and the PATH is usually limited to /bin:/usr/bin.

Cheers!

PS - I checked the post you refer to... and I forgot to put the backslash before the % sign, there. After more than one year, I can edit it. Thank you for making me notice this "bug".

Last edited by colucix; 11-14-2009 at 11:28 AM.
 
Old 11-14-2009, 12:08 PM   #7
dhar
LQ Newbie
 
Registered: Nov 2009
Posts: 4

Original Poster
Rep: Reputation: 0
Resolved

Thank you all very much. I appreciate your time and help. This forum is the best and very very helpful. Keep it up the great work.

Special thanks to colucix!!

Thanks

Dhar
 
Old 11-14-2009, 02:45 PM   #8
SaintDanBert
Senior Member
 
Registered: Jan 2009
Location: "North Shore" Louisiana USA
Distribution: Mint v21.3 & v22.x with Cinnamon
Posts: 1,797
Blog Entries: 3

Rep: Reputation: 108Reputation: 108
... might be another way

Quote:
Originally Posted by fpmurphy View Post
I suggest that you move the last day of the month logic out of your cron job and into your shell script. Them run your shell script every day or alternatively on days 28-31. The script then tests the date and does nothing if the date is not actual the last day of the month.
I like this idea very much. I suggest that you write a shell function file to hold all of tests like these ... yes, there will be more.
The function calls with parameters will be much more readable in your cron scripts and you won't need to copy and paste the date-time magic strings.

When I used to do lights-out, unattended system admin work, we had a command line utility that took a date-time expression as a parameter.
I don't remember the command program name, sorry. The program used success exit if the expression was true and failure exit if not-true.
It would handle all sorts of date keywords like "today" or "midnight". In addition, it handled things like "last day of next month" or "fifth sunday in June next year".
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Get first day of last month and last day of last month in bash xowl Linux - Software 18 02-09-2017 10:49 AM
cron job: day of month / day of week conflict? geekpie Linux - General 2 10-21-2009 04:56 AM
cron - finding last day of month FormalLogic Linux - General 7 03-10-2009 02:16 PM
cron last day of every month lobo78 Linux - General 2 03-03-2004 09:41 PM
Starting day of month, month length chrisk5527 Programming 2 03-03-2004 05:03 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:59 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration