Solved

cron job to create a gzip access_log

Posted on 1997-09-28
12
321 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
Comment Utility
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
Comment Utility
Hello,


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

Any suggestions?

Thank you
Mickael
0
 
LVL 4

Expert Comment

by:jlms
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility

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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 4

Expert Comment

by:jlms
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility

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
Comment Utility
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Introduction Regular patching is part of a system administrator's tasks. However, many patches require that the system be in single-user mode before they can be installed. A cluster patch in particular can take quite a while to apply if the machine…
FreeBSD on EC2 FreeBSD (https://www.freebsd.org) is a robust Unix-like operating system that has been around for many years. FreeBSD is available on Amazon EC2 through Amazon Machine Images (AMIs) provided by FreeBSD developer and security office…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

763 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

7 Experts available now in Live!

Get 1:1 Help Now