LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 06-03-2009, 01:38 PM   #1
Zero187
Member
 
Registered: May 2009
Posts: 32

Rep: Reputation: 15
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!
 
Old 06-03-2009, 02:20 PM   #2
ramram29
Member
 
Registered: Jul 2003
Location: Miami, Florida, USA
Distribution: Debian
Posts: 848
Blog Entries: 1

Rep: Reputation: 47
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.
 
Old 06-03-2009, 02:23 PM   #3
Zero187
Member
 
Registered: May 2009
Posts: 32

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by ramram29 View Post
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
 
Old 06-03-2009, 03:12 PM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Quote:
Originally Posted by Zero187 View Post
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.
 
Old 06-04-2009, 11:06 AM   #5
Zero187
Member
 
Registered: May 2009
Posts: 32

Original Poster
Rep: Reputation: 15
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.

Last edited by Zero187; 06-04-2009 at 11:07 AM.
 
Old 06-04-2009, 12:20 PM   #6
ramram29
Member
 
Registered: Jul 2003
Location: Miami, Florida, USA
Distribution: Debian
Posts: 848
Blog Entries: 1

Rep: Reputation: 47
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
creating a script to email daily log file sittnduck Linux - Server 4 07-15-2008 08:59 PM
Shell script in cron.daily ;Where does the log go ? jeepescu Linux - General 2 12-03-2007 06:07 PM
Advice? Best way to move files daily to a daily "date" named directory ziphem Linux - Newbie 2 04-15-2007 08:03 AM
script to clear log files kushalkoolwal Programming 8 01-24-2006 07:11 PM
Sending system log files to an e-mail account darkbluex Linux - Security 1 10-27-2004 07:37 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 07:11 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration