Link to home
Start Free TrialLog in
Avatar of mickael
mickael

asked on

cron job to create a gzip access_log

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

ASKER CERTIFIED SOLUTION
Avatar of jlms
jlms

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mickael
mickael

ASKER

Hello,


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

Any suggestions?

Thank you
Mickael
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!


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!


Avatar of mickael

ASKER

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

Avatar of mickael

ASKER


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
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.


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.


Avatar of mickael

ASKER

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?
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.


Avatar of mickael

ASKER


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).
_______________________________________________________________
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.