LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-18-2012, 11:15 AM   #1
nitrocs
LQ Newbie
 
Registered: Apr 2012
Posts: 2

Rep: Reputation: Disabled
BASH scripting help


I am running a DD-WRT enabled Linksys WRT350N router
To say I'm a novice bash/C/UNIX programmer is an overstatement, but I learn as I go

I've cobbled together a script that emails me when clients sign on/off my network:

Quote:
#!/bin/sh

fnc_mail() {

subj="$1"
msg="$2"

my_mail_addr="ADMIN EMAIL ADDRESS"
my_mail_to="YOUR EMAIL ADDRESS"
my_smtp="mail.optonline.net"
my_username="xxx"
my_passwd="xxx"
comcast="-d optonline.net"

if [ -z "$3" -o "$(dirname $3)" = "." ]; then logfile="/opt/lastsentmail.log"; else logfile="$3"; fi

echo "From: $(nvram get router_name)<$my_mail_addr>" > /opt/arpmsg.txt
echo "Subject: $subj" >> /opt/arpmsg.txt
echo "To: $my_mail_to" >> /opt/arpmsg.txt

#echo -e $msg >> /opt/arpmsg.txt
cat /opt/arpnew.txt >> /opt/arpmsg.txt

x=0
while [ $x -le 3 ] ;do

## rnr ##
cat /opt/arpmsg.txt | sendmail -S $my_smtp -f $my_mail_addr $comcast > $logfile 2>&1

if [ "$(cat $logfile | grep 221 | awk '{print $1}')" = "221" ]; then break;fi

echo -e "\nSend Count = $x" >> /opt/arpmsg.txt

cat $logfile | grep -v 250 | grep -v 334 | grep -v 235 | grep -v 354 | \
grep -v 220 | grep -v 221 >> /opt/arpmsg.txt


sleep 120s

x=`expr $x + 1 `

done

}

arp > /opt/arpnew.txt
cmp -s /opt/arpnew.txt /opt/arpold.txt > /dev/null
if [ $? -eq 1 ];

then
msg=$(cat /opt/arpnew.txt)
fnc_mail "Router Active Clients" "$msg" "/opt/arpmail.log"
cp /opt/arpnew.txt /opt/arpold.txt
rm /opt/arpnew.txt

else
rm /opt/arpnew.txt
fi
In typical fashion for discovering new horizons, I've met a new issue
When I run the script through the command line:

Quote:
sh -x /tmp/custom.sh
It works just fine, but when the cron job runs I get emails w/ blank body
I know cron requires "absolute" paths (???) and I though I specified that everywhere, but I cant figure out whats going on now

cron command i'm using is:
Quote:
*/5 * * * * root /opt/arpmail.sh
Any assistance would be greatly appreciated!
 
Old 04-18-2012, 11:18 AM   #2
Satyaveer Arya
Senior Member
 
Registered: May 2010
Location: Palm Island
Distribution: RHEL, CentOS, Debian, Oracle Solaris 10
Posts: 1,415

Rep: Reputation: 305Reputation: 305Reputation: 305Reputation: 305
Quote:
*/5 * * * * root /opt/arpmail.sh
You're using wrong cron entry. At what time you want to run the script?
 
Old 04-18-2012, 11:20 AM   #3
nitrocs
LQ Newbie
 
Registered: Apr 2012
Posts: 2

Original Poster
Rep: Reputation: Disabled
right now I have it run every five minutes & it does...but the email body is blank
 
Old 04-18-2012, 07:19 PM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
Try these alterations:

1. use bash instead of sh

2. use
Code:
set -xv
as the 2nd line for debugging

3. provide full path to sendmail cmd

4.
Code:
*/5 * * * * root /opt/arpmail.sh >/tmp/arpmail.log 2>&1
 
Old 04-18-2012, 10:42 PM   #5
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
The -x in sh -x /tmp/custom.sh produces command tracing output, normally sent to the terminal. When run from cron, there is no terminal to send it to. cron normally captures any such output and mails it.

Unexpected that the mail does not send the trace output in the body of the mail.

Have you tried sh /tmp/custom.sh ?
 
  


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
Bash Scripting Help amit.thaker Linux - Newbie 5 05-31-2011 10:44 AM
Reading a bash variable in bash scripting problem freeindy Programming 3 11-27-2008 03:29 AM
need help on bash scripting stympman Linux - Newbie 3 10-20-2008 07:20 PM
Bash Scripting Darklight451 Programming 1 10-07-2004 02:48 AM
BASH scripting help Chucklez Linux - Newbie 4 12-12-2002 01:07 PM


All times are GMT -5. The time now is 10:28 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration