Solved

setting up a cron job

Posted on 2002-03-19
10
389 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
ID: 6880787
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
ID: 6882379
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
ID: 6882589
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
ID: 6882615
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
ID: 6882627
Try to throw in a "sleep 3" after the stop command.
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 1

Expert Comment

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

Expert Comment

by:smisk
ID: 6882651
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
ID: 6883403
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
ID: 6885283
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
ID: 6885512
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

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

Installing FreeBSD… FreeBSD is a darling of an operating system. The stability and usability make it a clear choice for servers and desktops (for the cunning). Savvy?  The Ports collection makes available every popular FOSS application and packag…
Why Shell Scripting? Shell scripting is a powerful method of accessing UNIX systems and it is very flexible. Shell scripts are required when we want to execute a sequence of commands in Unix flavored operating systems. “Shell” is the command line i…
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.:
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.

937 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

1 Experts available now in Live!

Get 1:1 Help Now