Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 341
  • Last Modified:

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

0
mickael
Asked:
mickael
  • 7
  • 5
1 Solution
 
jlmsCommented:
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
 
mickaelAuthor Commented:
Hello,


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

Any suggestions?

Thank you
Mickael
0
 
jlmsCommented:
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
jlmsCommented:
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
 
mickaelAuthor 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

0
 
mickaelAuthor 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
0
 
jlmsCommented:
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
 
jlmsCommented:
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
 
mickaelAuthor 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?
0
 
jlmsCommented:
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
 
mickaelAuthor 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).
_______________________________________________________________
0
 
jlmsCommented:
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 does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 7
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now