Solved

cron job to create a gzip access_log

Posted on 1997-09-28
12
322 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

0
Comment
Question by:mickael
  • 7
  • 5
12 Comments
 
LVL 4

Accepted Solution

by:
jlms earned 130 total points
ID: 2007112
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.

0
 

Author Comment

by:mickael
ID: 2007113
Hello,


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

Any suggestions?

Thank you
Mickael
0
 
LVL 4

Expert Comment

by:jlms
ID: 2007114
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!


0
 
LVL 4

Expert Comment

by:jlms
ID: 2007115
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!


0
 

Author Comment

by:mickael
ID: 2007116
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

0
 

Author Comment

by:mickael
ID: 2007117

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
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 4

Expert Comment

by:jlms
ID: 2007118
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.


0
 
LVL 4

Expert Comment

by:jlms
ID: 2007119
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.


0
 

Author Comment

by:mickael
ID: 2007120
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?
0
 
LVL 4

Expert Comment

by:jlms
ID: 2007121
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.


0
 

Author Comment

by:mickael
ID: 2007122

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).
_______________________________________________________________
0
 
LVL 4

Expert Comment

by:jlms
ID: 2007123
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.




0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When you do backups in the Solaris Operating System, the file system must be inactive. Otherwise, the output may be inconsistent. A file system is inactive when it's unmounted or it's write-locked by the operating system. Although the fssnap utility…
Every server (virtual or physical) needs a console: and the console can be provided through hardware directly connected, software for remote connections, local connections, through a KVM, etc. This document explains the different types of consol…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now