Solved

setting up a cron job

Posted on 2002-03-19
10
392 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: 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!

 

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Virtualizing TAPE on dual VIOS 3 116
Parsing a file using ksh 10 77
auto mounter on solaris 1 72
Linux hostname change 2 87
This tech tip describes how to install the Solaris Operating System from a tape backup that was created using the Solaris flash archive utility. I have used this procedure on the Solaris 8 and 9 OS, and it shoudl also work well on the Solaris 10 rel…
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…
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…
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.

735 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