LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   script for sending log files daily (https://www.linuxquestions.org/questions/linux-software-2/script-for-sending-log-files-daily-730467/)

Zero187 06-03-2009 01:38 PM

script for sending log files daily
 
I don't know any BASH although I will be learning it this fall at college. I am trying to find a script that will send a log file daily using sendmail to a specified email address. I have sendmail installed and working I'm just not exactly sure how to write a script that will send a file as an attachment and do it once a day.

If anyone knows how to do this, I would be very grateful. Also, once I get this script do I just put it in /etc/init.d/ folder?

Thanks a lot!

ramram29 06-03-2009 02:20 PM

Here's what you need to do: 1. Run the command that will actually email the attachment and make sure it works. 2. Create a bash file that will run that command and make sure the bash script works. 3. Add the bash script to a cron.

Zero187 06-03-2009 02:23 PM

Quote:

Originally Posted by ramram29 (Post 3561997)
Here's what you need to do: 1. Run the command that will actually email the attachment and make sure it works. 2. Create a bash file that will run that command and make sure the bash script works. 3. Add the bash script to a cron.


I hate having to ask to be spoonfed, but I really am new to the linux environment (been using windows all my life unfortunately).

I can figure out the command to email attachments, I won't ask for that, but I really have no idea what a "cron" is. How would I add the .sh file to the cron?

Thanks again

colucix 06-03-2009 03:12 PM

Quote:

Originally Posted by Zero187 (Post 3562002)
I can figure out the command to email attachments, I won't ask for that, but I really have no idea what a "cron" is. How would I add the .sh file to the cron?

A good reason to read man pages:
Code:

man cron            # explains what cron is
man 1 crontab      # explains how crontab works
man 5 crontab      # explains the format of the crontab entries

Regarding the sendmail issue, you can embed the log file in the mail body using command substitution and a "here document". For example:
Code:

/usr/sbin/sendmail "user@domain" << EOF
From: Me <user@domain>
To: Mario Rossi <user@domain>
Cc: Luigi Bianchi <user@domain>
Subject: Test subject. Today is $(date)
The content of today log file is:
$(cat /path/to/log/file)
Goodbye
EOF

If you want to attach it, use uuencode or mutt. They have an option to attach files. Once you've finished and tested the script, be sure to use the full path of each command inside the script (crontab has a very limited environment and the PATH is just /bin:/usr/bin). Also check for standard output and standard error in the mail box of the crontab's owner, since every stdout and stderr not redirected to a file is sent through the system mail by default.

Zero187 06-04-2009 11:06 AM

Thanks for your help guys, I got it working perfectly.

Just for reference for anyone else looking for answers here is my script:

Code:

#!/bin/bash
TODAY=$(date '+%m-%d-%y');
wflogs -f '$start_time >= [this 1 day ago]' --sort=time -i netfilter -o html /var/log/shorewall/shorewall.log > /var/log/firewall/$TODAY.html;
uuencode /var/log/firewall/$TODAY.html $TODAY.html | mail -s "Firewall Log" myemail@server.com;


And the cron solution for me was to just drop this script in the /etc/cron.daily/ folder.

ramram29 06-04-2009 12:20 PM

I'm glad you got it working. Looks like your on your way to becoming a Linux admin. You can also create a cronjob, as root run:

crontab -e

This way you can run the script every minute, every week or whenever you want. Do some brainstorming on cron and you'll be able to schedule any script. It is a very powerfull admin tool.


All times are GMT -5. The time now is 10:38 AM.