Solved

setting up a cron job

Posted on 2002-03-19
10
394 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
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.

 

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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

Hello fellow BSD lovers, I've created a patch process for patching openjdk6 for BSD (FreeBSD specifically), although I tried to keep all BSD versions in mind when creating my patch. Welcome to OpenJDK6 on BSD First let me start with a little …
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…
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…
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…

617 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