From Pine View Farm

Adventures in Linux: #!/bin/bash Dept. 5

I just knew there was a way to completely automate this process. Now I’ve learned about BASH variables.

#!/bin/bash

# This script stops Apache,
# gzips the current log files,
# removes the old log files (access_log and error_log),
# creates a new folder named with today’s date and time,
# moves the gzipped log files to that location, then
# restarts Apache.
#
# The name of the folder is “month-day-year-hour-minute” so
# that the script can be run multiple times in the same day
# without stepping on itself (unless you run it twice in
# the same second).
#
# If someone wishes to tailor the script to his or her own
# system, it should work fine as long as the path
# statements are edited to reflect his or her configuration.

NOW=$1

: ${NOW:=$(date +”%m-%d-%Y-%H-%m”)}

cd /usr/local/apache2/bin
./apachectl stop

echo “Stopping Apache”

cd /usr/local/apache2/logs
mv access_log access_log.old
mv error_log error_log.old

echo “Renaming log files”

gzip access_log.old
gzip error_log.old

echo “Zipping log files”

#Gzip removes the original files after zipping them.

echo “Removing *.old files.”

mkdir /root/files/oldlogs/$NOW

echo “Making folder /root/files/oldlogs/$NOW”

mv /usr/local/apache2/logs/*.gz /root/files/oldlogs/$NOW

echo “Archiving old log files.”

cd /usr/local/apache2/bin
./apachectl restart

echo “Restarting Apache”

Share

5 comments

  1. chris the plumber

    June 10, 2007 at 6:12 pm

    I know enough to call for karen. . . or Opie he knows a lot about this stuff

     
  2. Frank

    June 10, 2007 at 7:18 pm

    Yeah, well, this is really geek stuff.

    I’ll translate it into English (yeah, scripting looks like English, but it’s not).

    Stop the webserver program.

    Zip up and delete the log files (if they get too big, they slow things down–that’s why I’m working on a new server).

    Make a new directory somewhere else and copy the zipped files over there —–>.

    Start everything back up.

    Most of it is just commands you can type in at the command line. The tricky part was setting it up to make automatically the new directory with a name that did not duplicate any other existing names, or the whole thing would have crashed and burned.

    Chris, I’ve been poking at this for three months. It would have been five minutes work for someone who knew what he was doing.

     
  3. Opie

    June 10, 2007 at 11:17 pm

    From the XAMPP online docs:

    start Starts XAMPP.
    stop Stops XAMPP.
    restart Stops and starts XAMPP.
    startapache Starts only the Apache.
    startssl Starts the Apache SSL support. This command activates the SSL support permanently, e.g. if you restarts XAMPP in the future SSL will stay activated.
    startmysql Starts only the MySQL database.
    startftp Starts the ProFTPD server. Via FTP you can upload files for your web server (user “nobody”, password “lampp”). This command activates the ProFTPD permanently, e.g. if you restarts XAMPP in the future FTP will stay activated.
    stopapache Stops the Apache.
    stopssl Stops the Apache SSL support. This command deactivates the SSL support permanently, e.g. if you restarts XAMPP in the future SSL will stay deactivated.
    stopmysql Stops the MySQL database.
    stopftp Stops the ProFTPD server. This command deactivates the ProFTPD permanently, e.g. if you restarts XAMPP in the future FTP will stay deactivated.
    security Starts a small security check programm.

    But Frank likes to build this stuff himself. Probably keeps him out of Linda’s hair.

     
  4. Frank

    June 14, 2007 at 6:48 pm

    Opie, you’ve almost got it.

    It’s not that I like to do it manually.

    It’s that I like to know what’s happening before I automate something. If it crashes later, that makes it a lot easier to debug.

     
  5. rising average httpd server

    August 20, 2010 at 7:47 pm

    […] I ended up writing a script to recyle the log files every day. I had to hunt a little while, but I found it. I sure that someone who knows more about BASH could have done it better, but it may be a good […]