My latest journey into the "underbelly" of the linux beast!
The Platform: I administer a system that creates a continous log file of events. This is a Red Hat RHEL 4 Update 5 system (circa 2007). It exists on a stand alone network that does not have internet access.
The Task: My boss has tasked me with the first business day of every month, to make a copy of the log file onto my thumb drive, rename it, and upload it to the company server.
The Complications: The order of the day requires the log file to be collected exactly on the first business day of the month. The file is root ownership and buried in a pile of subdirectories. The only way to get the file onto the internet and to my server is via a thumb drive. Thumb drives are not mounted when inserted nor is there any dialog to do so. The command line is used for virtually every task, with the exception of several Windows machines also on the network.
The Goal: By using a Windows machine as the conduit, I can easily insert my thumb drive and drag and drop the file to it. I decided I could script a way to automagically copy and rename the log file and place it neatly on the Windows computer and do it on the first business day. That way, if I'm absent or otherwise occupied, I still have the file available of the desired time/date. All I have left to do in insert thumb drive, copy the newly renamed file to it, upload from my desktop to the server, done.
So I needed to be able to detect the correct moment the file should be copied. I decided a cron job wouldn't work for two reasons: 1- the system may or may not be on at a particular hour of the day, 2- How do you tell crontab what the first business day of each month is?
The system is turned on every business day, but maybe at 5am or maybe not until 10am. If it comes on a 5am, it might go into use immedately thus putting undesired data in the log, thus the copy must be made immediately following bootup before the system can be used and only on the first business day.
Here's my idea: At boot up the log file is touched so the access date is reset. So at boot time, compare the current MONTH with the date stamp of the previously copied and renamed log on the Windows machine. If the month is the same - don't make a new copy of the log. If the month is different, make a new copy and overwrite the old one. Then the file on the Windows machine is always last months log uncorrupted by this months activities. Then I can copy and upload it in a more leisurely fashion.
Here's my script entries I put in rc.local:
The functions seem to work as I want, but the darn thing writes a new logfile every bootup. It seems as though the IF isn't working for some reason.
date +%m returns the current month numerically. Thus Sept. returns 09
ls -l --time-style=+%m <LOGFILE> | awk '$6 {print $6}' extracts the numerical month from the file listing. So a file last accessed in Sept. returns 09
So if 09 is not equal to 09, make a copy. Obviously, this is not true, so if IF should just exit, right? What am I missing? Anyone have a better idea on how the make this work?
The Platform: I administer a system that creates a continous log file of events. This is a Red Hat RHEL 4 Update 5 system (circa 2007). It exists on a stand alone network that does not have internet access.
The Task: My boss has tasked me with the first business day of every month, to make a copy of the log file onto my thumb drive, rename it, and upload it to the company server.
The Complications: The order of the day requires the log file to be collected exactly on the first business day of the month. The file is root ownership and buried in a pile of subdirectories. The only way to get the file onto the internet and to my server is via a thumb drive. Thumb drives are not mounted when inserted nor is there any dialog to do so. The command line is used for virtually every task, with the exception of several Windows machines also on the network.
The Goal: By using a Windows machine as the conduit, I can easily insert my thumb drive and drag and drop the file to it. I decided I could script a way to automagically copy and rename the log file and place it neatly on the Windows computer and do it on the first business day. That way, if I'm absent or otherwise occupied, I still have the file available of the desired time/date. All I have left to do in insert thumb drive, copy the newly renamed file to it, upload from my desktop to the server, done.
So I needed to be able to detect the correct moment the file should be copied. I decided a cron job wouldn't work for two reasons: 1- the system may or may not be on at a particular hour of the day, 2- How do you tell crontab what the first business day of each month is?
The system is turned on every business day, but maybe at 5am or maybe not until 10am. If it comes on a 5am, it might go into use immedately thus putting undesired data in the log, thus the copy must be made immediately following bootup before the system can be used and only on the first business day.
Here's my idea: At boot up the log file is touched so the access date is reset. So at boot time, compare the current MONTH with the date stamp of the previously copied and renamed log on the Windows machine. If the month is the same - don't make a new copy of the log. If the month is different, make a new copy and overwrite the old one. Then the file on the Windows machine is always last months log uncorrupted by this months activities. Then I can copy and upload it in a more leisurely fashion.
Here's my script entries I put in rc.local:
Code:
THISMONTH = date +%m LOGMONTH = ls -l --time-style=+%m <LOGFILE> | awk '$6 {print $6}' if [ $THISMONTH -ne $LOGMONTH ] then cp <LOGFILE> <NEWLOGFILE> fi
date +%m returns the current month numerically. Thus Sept. returns 09
ls -l --time-style=+%m <LOGFILE> | awk '$6 {print $6}' extracts the numerical month from the file listing. So a file last accessed in Sept. returns 09
So if 09 is not equal to 09, make a copy. Obviously, this is not true, so if IF should just exit, right? What am I missing? Anyone have a better idea on how the make this work?
Comment