Solved

setting up a cron job

Posted on 2002-03-19
10
388 Views
Last Modified: 2010-04-21
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
Comment
Question by:lphillips120898
  • 6
  • 4
10 Comments
 
LVL 1

Accepted Solution

by:
smisk earned 200 total points
Comment Utility
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
 

Author Comment

by:lphillips120898
Comment Utility
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
 
LVL 1

Expert Comment

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

Author Comment

by:lphillips120898
Comment Utility
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
 
LVL 1

Expert Comment

by:smisk
Comment Utility
Try to throw in a "sleep 3" after the stop command.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 1

Expert Comment

by:smisk
Comment Utility
Try to throw in a "sleep 3" after the stop command.
0
 
LVL 1

Expert Comment

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

Author Comment

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

Author Comment

by:lphillips120898
Comment Utility
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
 
LVL 1

Expert Comment

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Let's say you need to move the data of a file system from one partition to another. This generally involves dismounting the file system, backing it up to tapes, and restoring it to a new partition. You may also copy the file system from one place to…
I have been running these systems for a few years now and I am just very happy with them.   I just wanted to share the manual that I have created for upgrades and other things.  Oooh yes! FreeBSD makes me happy (as a server), no maintenance and I al…
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…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

771 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

10 Experts available now in Live!

Get 1:1 Help Now