LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
LinkBack Search this Thread
Old 08-14-2008, 04:19 AM   #1
FormalLogic
LQ Newbie
 
Registered: Mar 2008
Posts: 10

Rep: Reputation: 0
cron - finding last day of month


Hi guys,
I want to know how to run a task at the last day of every month, whethwer it has 31, 30 days or whatever. it's to update a MySql db (The Worst Database In The World, no contest

Thanks in advance.
I'll include a cron header to make it easy to explain.

#minute (0-59),
#| hour (0-23),
#| | day of the month (1-31),
#| | | month of the year (1-12),
#| | | | day of the week (0-6 with 0=Sunday).
#| | | | | commands
#------------------------------------------------------------------------------
 
Old 08-14-2008, 05:05 AM   #2
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 62
Run a script on the 28-31 day of the month, and have a test inside your script.
 
Old 08-14-2008, 05:18 AM   #3
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 62
For example, make a script called "is_last_day_of_month" containing this:
Code:
#!/bin/bash

if [ $# -eq 0 ]; then
        test_date=$(date)
else
        test_date="$1"
fi

today_month=$(date -d "$test_date" +%m)
tomorrow_month=$(date -d "$test_date + 1 day" +%m)

[ "$today_month" != "$tomorrow_month" ] && exit 0 || exit 1
Save it somewhere, e.g. /usr/local/bin/is_last_day_of_month and make it executable. Then in your crontab (to run at 1:13 am):

Code:
13 1 28-31 * * /usr/local/bin/is_last_day_of_month && yourcommand
 
1 members found this post helpful.
Old 08-14-2008, 10:01 AM   #4
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,361

Rep: Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910
Another idea is a test based on the cal command, as shown in this thread.
 
Old 08-15-2008, 01:42 AM   #5
FormalLogic
LQ Newbie
 
Registered: Mar 2008
Posts: 10

Original Poster
Rep: Reputation: 0
Thanks Guys!!!

Thanks guys, answer is perfect,
This thread just shows how much I still need to learn. i'm a sysadmin/programmer so I'm learning different date syntax all the time (i've got to remember MySql/Php/Linux/C/God Knows What else) It's a little daunting sometimes. But I'll just do what i always do: wing it! As you've probably guessed formal logic is a euphamisim for "Don't know what the hell i'm doing"
 
Old 08-15-2008, 03:15 AM   #6
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,361

Rep: Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910
I've just noticed a little bug in the code suggested by matthewg42. In line 10:
Code:
tomorrow_month=$(date -d "$test_date + 1 day" +%m)
you have to strip out the plus sign between $test_date and "1 day". The syntax to add days, hours, and so on, is simply
Code:
date -d "$test_date 1 day 3 hours"
that is you can put one or more time specifications with different units, but without the + sign. To subtract an amount of time, use the word "ago" in the time specification:
Code:
date -d "$test_date 4 days ago"
 
Old 12-08-2008, 07:05 AM   #7
archtoad6
Senior Member
 
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
Blog Entries: 15

Rep: Reputation: 230Reputation: 230Reputation: 230
Messy.

I've just re-read man:crontab(5) & man:cron(8) & I'm surprised there is no "Last of Month" feature. IMO, this is a bug.

If you run your script at 1:13 am, as suggested, won't you be doing your update before the beginning of business? I would expect that anything that needs to be done on the last day of the month needs to be done after the close of business. If that is the case, why not run your script at 1:13 am on the 1st of the month.

BTW, what is the purpose of the db update you're tasked w/ doing?


Edit: Oops, I got here from http://www.linuxquestions.org/questi...ly-job-689037/ & forgot that this thread is a little old, hope it doesn't bother anyone.

Last edited by archtoad6; 12-08-2008 at 07:23 AM. Reason: see edit
 
Old 03-10-2009, 01:16 PM   #8
malaprop
LQ Newbie
 
Registered: Dec 2008
Location: TX
Distribution: Ubuntu 8.10
Posts: 26

Rep: Reputation: 15
This also works, notice here I'm using d/m/y instead of m/d/y.

Code:
# GET FIRST DAY OF LAST MONTH
FIRST=$(date --date "last month" +01/%m/%Y)

# USE FIRST DAY OF THIS MONTH TO GET LAST DAY OF LAST MONTH.
LAST=$(date --date "`date +%m/01/%Y` yesterday" +%d/%m/%Y)

echo "FIRST $FIRST"
echo "LAST $LAST"
 
  


Reply

Tags
automation, cron, scripts


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 On
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are 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 14 08-09-2012 05:52 AM
LXer: A month on the command line -- Day 1 LXer Syndicated Linux News 3 05-06-2007 11:35 AM
Finding six month old files arockia Linux - Software 11 09-01-2004 09:37 AM
cron last day of every month lobo78 Linux - General 2 03-03-2004 08:41 PM
Starting day of month, month length chrisk5527 Programming 2 03-03-2004 04:03 PM


All times are GMT -5. The time now is 11:57 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration