Linux - Server This forum is for the discussion of Linux Software used in a server related context. |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
04-07-2011, 07:04 PM
|
#1
|
LQ Newbie
Registered: Apr 2011
Posts: 3
Rep:
|
crontab execute correctly, but date in script fails
At the beginning of each month, /etc/crontab executes a job
at midnight to start a new Apache log file with the correct month
in the logfile name.
Job last ran April 1 at 00:00:01 according the /var/log/cron.
Here's my /etc/crontab entry.
# Apache log switch and restarting Apache
0 0 1 * * root /root/bin/apchlogchg.sh
Here's the apchlogchg.sh file...
#!/bin/bash
#
# Getting the "CURR"ent & "PREV"ious "Mon"th.
#
CURR=`date +%b%Y`
PREV=`date --date '1 month ago' +%b%Y`
If you were to echo $PREV, the results were "Feb2011".
Normally this script worked correct, but for April 1, it failed.
I have notice years past that having a job execute right a midnight,
sometimes fails when a "date" is involved. Could this be because of
the time change to DST in March?
We're running CentOS 5.5 x86_64.
Most of my servers came up with Feb2011, this April 1.
Any reason why the crontab begins on April 1, 00:00:01, but yet
the date command gets the wrong date? M
Thanks in advance.
|
|
|
04-08-2011, 12:08 PM
|
#2
|
LQ Guru
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509
|
Since the problem started from April most likely the DST is involved, but I cannot explain the reason. What if you try the -u option? What if you try the following from the command line?
Code:
date -d '20110401 1 month ago' +%b%Y
Anyway, since you're intersted in month and year, the following should be equivalent and might be a valid workaround:
Code:
date -d yesterday +%b%Y
Edit: according to the coreutils FAQ relative items in date strings may cause the result to end up at an undesired time. Such as at an invalid time due to a time change or in the same month instead of a different month and if you are working with months or years then it is more robust to operate at the middle of the month. They suggest to use a date at the middle of the month, e.g.
Code:
date --date="$(date +%Y-%m-15) -1 month" +%b%Y
Reference: http://www.gnu.org/software/coreutil...ing-right_002e
Last edited by colucix; 04-08-2011 at 12:22 PM.
|
|
|
04-09-2011, 06:18 AM
|
#3
|
Senior Member
Registered: Dec 2004
Location: Marburg, Germany
Distribution: openSUSE 15.2
Posts: 1,339
|
For me it looks working, also on April 1st. What is your version of date --version? I get 6.12.
|
|
|
04-12-2011, 04:03 PM
|
#4
|
LQ Newbie
Registered: Apr 2011
Posts: 3
Original Poster
Rep:
|
Colucix - Thanks for digging the FAQ up. The date command you provided is a bit over my head right now, but it's
beginning to sink in as to what you are doing.
I recall that 15 or so years ago on a SCO system, the crontab system would set the job off, but the date inside
the script would reveal a different time. I also recall the BIOS date was a few minutes apart from the OS date.
So, not fully understanding how Linux gets it's date, I'll use your suggestion for now.
This script has been working well for years. Just that this "time change" seems to have caused all of my scripts,
"be late" when getting the time. The crontab started the job on time, but the date command seems to have thought,
it was still in the previous month.
Thanks for your help.
|
|
|
04-12-2011, 04:05 PM
|
#5
|
LQ Newbie
Registered: Apr 2011
Posts: 3
Original Poster
Rep:
|
Reuti - All servers have the same revision of 5.97.
Thanks for your reply.
|
|
|
04-15-2011, 02:20 PM
|
#6
|
LQ Newbie
Registered: Jul 2009
Posts: 25
Rep:
|
Quote:
@skylord
So, not fully understanding how Linux gets it's date.
|
Here is how it does it.
Quote:
The x86 and x86-64 computers that most often run Linux, as well as most other computers
of this general class, have two built-in clocks. The fi rst of these clocks, sometimes called
the hardware clock, maintains the time while the computer is turned off. When you boot
Linux, it reads the hardware clock and sets the software clock to the value it retrieves.
The software clock is what Linux uses for most purposes while it’s running.
|
The hardware clock works on Coordinated Universal Time (UTC).
Last edited by Bodi; 04-15-2011 at 02:22 PM.
|
|
|
04-17-2011, 02:56 AM
|
#7
|
Senior Member
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
|
Quote:
Originally Posted by skylord
This script has been working well for years. Just that this "time change" seems to have caused all of my scripts,
"be late" when getting the time.
|
Cron and your scripts may be using a different time zone. For example, when the scripts are run, they might have TZ set, in which case
Code:
date
env -u TZ date
give different "local times". Since service scripts like /etc/init.d/cron (which handles starting and stopping the cron service) are written to use the sh shell, the environment may differ if your scripts use e.g. bash or tcsh shell.
If you want to make sure your script is run at the first day of each month (which I think your script expects), you can do a simple loop to make sure the date has changed. This one is for bash, sh, dash or compatible shells:
Code:
while [ `date '+%Y%m'` -ne `date -d 'now + 18 hours' '+%Y%m'` ]; do sleep 60 ; done
The loop makes sure that if the month changes in the local time zone in the next 18 hours, it waits one minute at a time until the date has actually changed.
Hope this helps.
|
|
|
All times are GMT -5. The time now is 06:04 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|