We help IT Professionals succeed at work.

cron job to create a gzip access_log

mickael
mickael asked
on
Medium Priority
362 Views
Last Modified: 2010-04-21
Hello every one,

I would like to know how I could create a cron job to do the following once a month:

make a copy of access_log and rename it to access_log-b
gzip access_log-b
rename access_log-b to access_log-tmp
remove access_log-b

I will then have an access_log-tmp that I can analyse with webtrends every month !!

The OS is Unix BSDI ant the server is NCSA 1.5

Comment
Watch Question

Commented:
Create a shell script that does what you describe.

Then edit your cron file (usually using the command "crontab -e) including a line like this:

# MIN HOUR DAY MONTH DAYOFWEEK   COMMAND
0 8 1 * * your_script_here.sh

This will run your script the first day of each month at 8:00 am.
Remeber to clarify the PATH environment variable or to put full paths for the commands that require it.

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
Hello,


My biggest problem is creating this simple shell script ...

Any suggestions?

Thank you
Mickael

Commented:
No problem, try this example:
--------- File begin after this line -----
#!/bin/sh
PATH=/bin
export PATH
cd /directory_where_your_files_are
cp access_log access_log-b
gzip access_log-b
mv access_log-b.gz access_log-tmp.gz
---------- The previus line is the las of the file -----

Create a file with the previous commands, then change permisions to allow  execution of the shell:
chmod 755 your_script_here.sh
and do what I mention in my first posting (crontab -e).
Run once your script to check is working properly.

A simpler script is this one:
-------Script begins in next line ------
#!/bin/sh
/bin/gzip -c access_log > access_log-tmp.gz
-------Script ends in previous line -----

And the easiest is to include the prevous line directly in your crontab file: run crontab -e and add a line like this:

0 8 1 * * /bin/gzip -c /your_directory/access_log >   /your_directory/access_log-tmp.gz

The previousshould be written in the same line!


Commented:
No problem, try this example:
--------- File begin after this line -----
#!/bin/sh
PATH=/bin
export PATH
cd /directory_where_your_files_are
cp access_log access_log-b
gzip access_log-b
mv access_log-b.gz access_log-tmp.gz
---------- The previus line is the las of the file -----

Create a file with the previous commands, then change permisions to allow  execution of the shell:
chmod 755 your_script_here.sh
and do what I mention in my first posting (crontab -e).
Run once your script to check is working properly.

A simpler script is this one:
-------Script begins in next line ------
#!/bin/sh
/bin/gzip -c access_log > access_log-tmp.gz
-------Script ends in previous line -----

And the easiest is to include the prevous line directly in your crontab file: run crontab -e and add a line like this:

0 8 1 * * /bin/gzip -c /your_directory/access_log >   /your_directory/access_log-tmp.gz

The previousshould be written in the same line!


Author

Commented:
Hello,

here is my cron tab file is this correct?

0 8 1 * * /usr/bin/gzip -c /usr/local/etc/httpd/logs/access_log > /usr/local/etc/httpd/logs/access_log-tmp.gz

Author

Commented:

If I decide to go with the scrip, should I put this file unto my cgi-bin or it doesn't matter where I install it?

Thank you, I will soon be all set !!

#!/bin/sh
/bin/gzip -c access_log > access_log-tmp.gz

Commented:
It does not matter, the cron jobs are completely independent from your web services(unless you program your cron job to modify something in you web site!), your script can by anywhere, just be sure that it has permission to execute.


Commented:
About your crontab file:
It is OK, nevertheless the numbers there are just sugestions, like it is it will run every 1st day of each month at 8:00 am; just adjust the time (and maybe day) to your needs.


Author

Commented:
Is your name Jim ?? (wild guess)

I susspect I will have a problem because I get prompted to replace existing access_log.gz by new one, can I make the script answer yes too?

Commented:
You could, but is a better idea or to rename the previous month's compressed file or to give to the new compressed file a unique name.

Renaming the old file can be done by hand (after all you have a month to do so :) ) or can be done in one script. Giving the new compressed file a unique name has to be done in the script.

Let me know what you prefer and we can work out something.


Author

Commented:

Don't you sleep ??? or are you on the "UTC or next to it" time zone !! This is really fast expertise/service... THIS IS GREAT

Giving the new compressed file a unique name would be best so I can give mnthly files for our records. I am joining a example of a cront tab job that came with our server doc so that it may actually  reveal particular info/procedure about our particular sever (if it does makes somewhat of a difference?):

________________________________________________________________

You should set up a cron tab to compute the daily statistics and send you a daily report, a weekly report, and a monthly report automatically. Additionally, you should "Nuke" the getstats log file at the start of every month to free up more disk space for you. Here is a sample cronfile:

     ----------------------
     58 23 * * * /usr/local/bin/getstats -d -f | /usr/bin/mail -s "HTTP Daily Stats"
     stats@yourdomain.com
     59 23 * * 7 /usr/local/bin/getstats -w -f | /usr/bin/mail -s "HTTP Weekly Stats"
     stats@yourdomain.com
     01 00 1 * * /usr/local/bin/getstats -w -f -n | /usr/bin/mail -s "HTTP Monthly Stats"
     stats@yourdomain.com
     ----------------------

Store this three line file in your home directory in a file called "cronfile" for example. Make sure it
is only three lines. If the lines are long, let them wrap, but do not add a hard return.

then type "crontab cronfile" in your home directory.

The first line will send a full daily report to stats@yourdomain.com each day at 23:58 (11:58
pm).
_______________________________________________________________

Commented:
Well, actually I am in Kuala Lumpur, Malaysia and have some free time to be around here.

Now, to make a unique file you can use the "date" command, butyou have to find out the exact sintax and options for your UNIX version, which I don't know, because the op[tions I will put could differ from the ones you need.

In my UNIX (Linux) one can write the crontab line like this:

0 8 1 * * /usr/bin/gzip -c /usr/local/etc/httpd/logs/access_log > /usr/local/etc/httpd/logs/access_log-tmp`date +%b%y`.gz

In Linux date +%b%y will write the Month (abbreviated) and the year (last two digits), so if your crontab runs the first of November you should get a file called:

access_log-tmpNov97.gz

So check your date command and add the options that are more convinient for you. Please  nothe that the
date +%b%y
is enclosed in back quotes.




Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.