Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


  Search this Thread
Old 04-18-2012, 10:15 AM   #1
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:


fnc_mail() {


my_mail_addr="ADMIN EMAIL ADDRESS"

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

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 `



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

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

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

sh -x /tmp/
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:
*/5 * * * * root /opt/
Any assistance would be greatly appreciated!
Old 04-18-2012, 10: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
*/5 * * * * root /opt/
You're using wrong cron entry. At what time you want to run the script?
Old 04-18-2012, 10:20 AM   #3
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, 06:19 PM   #4
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,396

Rep: Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395
Try these alterations:

1. use bash instead of sh

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

3. provide full path to sendmail cmd

*/5 * * * * root /opt/ >/tmp/arpmail.log 2>&1
Old 04-18-2012, 09:42 PM   #5
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/ 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/ ?


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

All times are GMT -5. The time now is 05:33 PM.

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