#!/usr/bin/perl # You may need to change this path to /usr/local/bin/perl $mailprog = "/usr/sbin/sendmail"; #************************************************************** # # Script to automatically empty log files via cron job: PWSautoemptylogfiles V2.5free # # Written by: # Premier Website Solutions - http://www.premierwebsitesolutions.com # Created - January 9, 2005 (V1.0) # Modified - January 9, 2005 (V1.1) # - added /var/log/chkservd.log file to options to empty # Modified - January 15, 2005 (V2.0) # - added cpanel logs to options to empty(*) # Last Modified - January 16, 2005 (V2.5) # - final adjustments before going public # # (*) features not in free version # Join my scripts club for $10 US and get access to full features of all scripts # www.premierwebsitesolutions.com/scripts/scriptsclub.pws # # # Provide the path to the various log files below, and the sender and sendto email # addresses for the email that will be sent when log files are emptied. Specify # which files you want automatically deleted. Then upload this script to wherever # you want on your server and set a cron job to run it. # Remove the .txt extension from the script name. # # Ownership and permissions should be root:root, and 0700, unless you want # to enable wheel users to use it, then it would be root:wheel 0770. # # # Registered users of this script will be notified of any future updates. # If you registered this copy with me, put your email here for future reference. # This copy is registered to: # #************************************************************** # If you want to be emailed when this script is run, put your email address here. # If you use spamassassin, you should include a name, like this: # $sendto_email = 'me '; $sendto_email = ''; # This is the sender for the email if you do include a sendto_email. # Change it if you wish. $sender_email = 'Mike '; # Subject of the email sent to you # With multiple servers, you may want to say which server like this example # It will be specified in the email body with the next variable, but it's # nice to see in the subject line also. $subject = "Auto deleted log files on server1"; # The name of the server this copy is installed on. This will tell you which # server's logs were deleted in the message you receive. $server_name = "server1"; # For most server configurations, the following variables should not have to # be changed. Just look them over to see if they do match your server setup. # This is the path to your system log files directory. It may need to be changed if # your server is set up differently. It must be the full path beginning with /. $var_logs = "/var/log"; # For the different file choices here, set the variable to Y to delete them, # or N to not delete them. $oldvarlogs = "Y"; # older files renamed with 1-4 on the end $zippedvarlogs = "Y"; # zipped (.gz) log files $chkservd = "Y"; # chkservd.log file # More log file options are in the members version. # For the next variable, you may need to change it. Select one that # works for your server. Just comment the currently selected one by # putting a # in front and uncomment a different one. To know if it's # working, check your log files after the script has run. If the log # files do not exist, the related service was not restarted and you # will need to change the variable. # If the related service does not need to be restarted on your server, # just uncomment the last option. # Command to restart chkservd on your server # This recreates the /var/log/chkservd.log file $chkservdrestart = "/etc/rc.d/init.d/chkservd restart >/dev/null 2>&1"; #$chkservdrestart = "/etc/init.d/chkservd restart >/dev/null 2>&1"; #$chkservdrestart = "service chkservd restart >/dev/null 2>&1"; #$chkservdrestart = ""; #------------------------------------------------------------------- #------------------------------------------------------------------- # UNLESS YOU KNOW CGI, DO NOT EDIT ANYTHING BELOW HERE # Feel free to study the code, but alterations are at your own risk #------------------------------------------------------------------- #------------------------------------------------------------------- $message = "The following log files have been deleted on server $server_name:\n\n"; $var_logs =~ s/\/$//; if ($oldvarlogs eq "Y") { system ("rm -f $var_logs/*.1"); system ("rm -f $var_logs/*.2"); system ("rm -f $var_logs/*.3"); system ("rm -f $var_logs/*.4"); $message = $message . "numbered log files in $var_logs\n"; } if ($zippedvarlogs eq "Y") { system ("rm -f $var_logs/*.gz"); $message = $message . "zipped log files in $var_logs\n"; } if ($chkservd eq "Y") { system ("rm -f $var_logs/chkservd.log"); system ("$chkservdrestart"); $message = $message . "chkservd.log file in $var_logs\n"; } if ($sendto_email eq "" || $sender_email eq "") {exit;} # Open The Mail Program open(SENDMAIL,"|$mailprog -t"); print SENDMAIL "To: $sendto_email\n"; print SENDMAIL "From: $sender_email\n"; print SENDMAIL "Subject: $subject\n\n"; print SENDMAIL "$message\n"; close (SENDMAIL);