Go Premium for a chance to win a PS4. Enter to Win

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

setting up a cron job

I need to setup a cron job that does the following:

** copylogs.sh **
cd /usr/local/apache/bin
./apachectl stop
cd /usr/local/apache/logs
mv error_log error_log.mmddyyyy
mv access_log access_log.mmddyy
cd /usr/local/apache/bin
./apachectl start

** end copylogs.sh **

I don't know how to setup a cron job and have "su" access, so I would need to know the steps to setting it up.  Also, I would need to know how to be able to rename (mv) the log files and append the current date to the name of the log file.  It doesn't necessarly have to be in the "mmddyyyy" format - whatever is the easiest, just so we have the date appended to the file name.

Thanks,

Lisa

0
lphillips120898
Asked:
lphillips120898
  • 6
  • 4
1 Solution
 
smiskCommented:
For help on cron jobs, see http://www.superscripts.com/tutorial/crontab.html.

How is this for the script :

#! /bin/sh

# create date filenames for the error and access logs
error_file="error_log.`date +%m%d%y`"
access_file="access_log.`date +%m%d%y`"

# stop apache
/usr/local/apache/bin/apachectl stop

# roll the logs
mv /usr/local/apache/logs/error_log $error_file
mv /usr/local/apache/logs/access_log $access_file

# start apache
/usr/local/apache/bin/apachectl start
0
 
lphillips120898Author Commented:
The script looks good and I'm going to try it today... but I have a question about the cron job and crontab.  I read the info on the link you sent.  Everything seemed straight forward until I read at the end where it said:

I would suggest making a special hidden directory or using a directory inside your cgi-bin so nobody can see it.   So to finish and execute your crontab file just telnet into your server and type:

cd /path/to/crontab/directory
crontab nameofcrontabfile

Now... If I'm going to execute this as root and there is already a crontab in the /etc directory am I going to use that crontab file to add the job to?  If so, when would you want to use another file (crontab nameofcrontabfile) for your crontab file?  Just want to make sure I don't mess up any of the jobs that are currently in the etc/crontab file.

Thanks,

Lisa

0
 
smiskCommented:
Lisa,

  Cron allows for individual users to create their own scheduled tasks as well (thus the crontab command).  Since this is a system level command, I see no harm in putting it in /etc/crontab.

1) Edit /etc/crontab ("vi /etc/crontab" or whatever editor you use)
2) Insert the actual crontab line as follows :

# this will roll the apache logs every night at 12:05
5 0 * * * /root/jobs/roll_apache.sh

Where /root/jobs/roll_apache.sh is the script.

You should be all set.  I just suggest you test running /root/jobs/roll_apache.sh to make sure the command works.

Steve
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
lphillips120898Author Commented:
I just tested the "http_log_archive.sh" script from the command line and all worked well EXCEPT starting the http services back up (/usr/local/apache/bin/apachectl start).

I discovered this by running ps -ef | grep httpd

I then just ran the same command from the command line and it started up fine.  Is it possible that it is trying to start the httpd services up too quickly after they were stopped?  Is it possible it needs some kind of "pause" to wait a few seconds before trying to start the http services?  If so, what is the syntax?  If not, any suggestions?



#! /bin/sh

# create date filenames for the error and access logs
error_file="error_log.`date +%m%d%y`"
access_file="access_log.`date +%m%d%y`"

# stop apache
/usr/local/apache/bin/apachectl stop

# roll the logs
mv /usr/local/apache/logs/error_log /usr/local/apache/logs/archive/$error_file
mv /usr/local/apache/logs/access_log /usr/local/apache/logs/archive/$access_file

# start apache
/usr/local/apache/bin/apachectl start
0
 
smiskCommented:
Try to throw in a "sleep 3" after the stop command.
0
 
smiskCommented:
Try to throw in a "sleep 3" after the stop command.
0
 
smiskCommented:
Lisa,

  Cron allows for individual users to create their own scheduled tasks as well (thus the crontab command).  Since this is a system level command, I see no harm in putting it in /etc/crontab.

1) Edit /etc/crontab ("vi /etc/crontab" or whatever editor you use)
2) Insert the actual crontab line as follows :

# this will roll the apache logs every night at 12:05
5 0 * * * /root/jobs/roll_apache.sh

Where /root/jobs/roll_apache.sh is the script.

You should be all set.  I just suggest you test running /root/jobs/roll_apache.sh to make sure the command works.

Steve
0
 
lphillips120898Author Commented:
Just one more thing...

The other jobs in the crontab look like:

0 * * * 0,6 root /usr/lib/sa/sa1 600 6 &
5 19 * * * root /usr/lib/sa/sa2 -A &

Why do they have root in front of the script and the prameters?  Is this something I need to do?

This is what I have, based on your example:

0 * * * /root/scripts/http_log_archive.sh

Let me know.  Thanks - Lisa
0
 
lphillips120898Author Commented:
I'm not really "rejecting" this answer... but I just realized this question is locked and I'm assuming no one is allowed to "post" additional comments.  At this point everything is working except the execution of the cron job and Steve deserves the points... Steve, can you just give me some input on the previous comments I had about the difference in syntax in the existing crontab file I had and the suggested entry?  Should I try this again using "root" in front of the script name?  What are the entries at the end of the script used for?  One more thing.. since my cron job didn't run is there anyway for me to see why it didn't run?  I know the script works because I tested it.

Thanks for you help,

Lisa
0
 
smiskCommented:
Lisa,

Try adding "root" like the other jobs in the /etc/crontab file.  I think that this is the default ("root" signifies that the job runs as the user "root" whereas "joe" would run it as "joe").

Being as I am not familiar with Apache, do you normally start and stop it as a superuser?

So bottom line, try this as the cron entry :

0 * * * * root /root/scripts/http_log_archive.sh

You were missing one * also (perhaps the reason it didn't run?)
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 6
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now