LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   BASH scripting help (https://www.linuxquestions.org/questions/linux-newbie-8/bash-scripting-help-940470/)

nitrocs 04-18-2012 10:15 AM

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!

Satyaveer Arya 04-18-2012 10:18 AM

Quote:

*/5 * * * * root /opt/arpmail.sh
You're using wrong cron entry. At what time you want to run the script?

nitrocs 04-18-2012 10:20 AM

right now I have it run every five minutes & it does...but the email body is blank

chrism01 04-18-2012 06:19 PM

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

catkin 04-18-2012 09:42 PM

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 ?


All times are GMT -5. The time now is 05:17 AM.