LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 02-17-2010, 09:43 AM   #1
frenchn00b
Senior Member
 
Registered: Jun 2007
Location: E.U., Mountains :-)
Distribution: Debian, Etch, the greatest
Posts: 2,546

Rep: Reputation: 57
mutt and crontab , not working :(


Hello,

Into my crontab -e, I run sh email.sh

which has:
Code:
#!/bin/sh
echo "Email pages"
cd
mkdir email
cd email
cat .email| while  read i  ; do 
wget -k "$i" -O page.htm 
EMAILTOBESENT="XXXXXXXXXX@gmail.com"
mutt -e 'set content_type="text/html"' "$EMAILTOBESENT" -s "$(date) email $i" < page.htm
rm page.htm
done
No way, it does not send anything. crontab does not let it work.

When I test it from xterm, it works though.
 
Old 02-17-2010, 11:10 AM   #2
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,398
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Things like environment variables ($PATH, for instance) do not get set the same in a cron job as they do when you log in interactively. Perhaps try adding something like
Code:
source ~/.bashrc
to the top of your script, or explicitly add environment variables as necessary. Almost certainly, the commands wget... and mutt... will fail because they are not found in $PATH.
--- rod.
 
Old 02-17-2010, 11:13 AM   #3
Berhanie
Senior Member
 
Registered: Dec 2003
Location: phnom penh
Distribution: Fedora
Posts: 1,625

Rep: Reputation: 165Reputation: 165
If there was an error with the cron job, cron should have you an email with the stderr. If the script runs from the command line but not from cron, then a common explanation is that PATH wasn't defined in the script.
 
Old 02-17-2010, 12:24 PM   #4
frenchn00b
Senior Member
 
Registered: Jun 2007
Location: E.U., Mountains :-)
Distribution: Debian, Etch, the greatest
Posts: 2,546

Original Poster
Rep: Reputation: 57
Quote:
Originally Posted by theNbomr View Post
Things like environment variables ($PATH, for instance) do not get set the same in a cron job as they do when you log in interactively. Perhaps try adding something like
Code:
source ~/.bashrc
to the top of your script, or explicitly add environment variables as necessary. Almost certainly, the commands wget... and mutt... will fail because they are not found in $PATH.
--- rod.
Nope. Not working, is it again a bug with mutt that doesnt like crontab.
Pfff so bad. I mean this is completely simple thing, that should work. I am running debian stable.
 
Old 02-17-2010, 06:32 PM   #5
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,398
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Have you at least identified where it goes off the rails? Is the cron job actually running? How do you know? What evidence have you observed so far?
I doubt that there is any bug with mutt related to cron. I am certain that it all has to do with setting up the environment properly within you script, while running as a cron job.
--- rod.
 
Old 02-19-2010, 03:11 PM   #6
frenchn00b
Senior Member
 
Registered: Jun 2007
Location: E.U., Mountains :-)
Distribution: Debian, Etch, the greatest
Posts: 2,546

Original Poster
Rep: Reputation: 57
I found the error, syslog says:

Code:
sm-mta[19280]: o1JL75pZ019280: from=<XXXXXXXXXXXXXXX>, size=2947, class=0, nrcpts=1, msgid=<201002192107.o1JL71xg019258@XXXXXXXXXXXXXXX>, proto=ESMTP, daemon=MTA-v4, relay=localhost [127.0.0.1]
where XXXXXXXXXXXXXXX is nto the same as what I defined into .msmtprc !!

damn bugs "!!! that's really annoying
 
Old 02-19-2010, 03:20 PM   #7
frenchn00b
Senior Member
 
Registered: Jun 2007
Location: E.U., Mountains :-)
Distribution: Debian, Etch, the greatest
Posts: 2,546

Original Poster
Rep: Reputation: 57
Quote:
Originally Posted by theNbomr View Post
Have you at least identified where it goes off the rails? Is the cron job actually running? How do you know? What evidence have you observed so far?
I doubt that there is any bug with mutt related to cron. I am certain that it all has to do with setting up the environment properly within you script, while running as a cron job.
--- rod.
I mean this is unbelieveable to accept such packages under Debian. It simply does not work, and I wonder why nobody cares much. Mutt and msmtp are very important for linux, mroe than any fancy desktop


Code:
   #!/bin/sh
############variables
  

cd
mkdir emaildaily
cd emaildaily

cat $HOME/.emaildaily | while read COL ; do 


wget  --user-agent Mozilla/4.0   -k "$COL" -O daily-page.html
  
        ls -lah daily-page.html
        

mutt -F  ~/.muttrc  -e 'set content_type="text/html"' XXXXXXXXX@gmail.com  -s "$(date) Dailyemail $CONFIGFILE" < daily-page.html
        echo "$(date), Email sent to $EMAILTOBESENT"
        echo "$(date), Email sent to $EMAILTOBESENT" >> .log

done 


rm daily-page.html


exit 0
 
Old 02-19-2010, 05:47 PM   #8
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,398
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
So, your script is littered with shell variables. How are these variables given values? The script as shown does nothing to give them any values, and when cron runs the script, it doesn't know anything about any variables you may have set last week in some shell session.
--- rod.
 
Old 02-20-2010, 01:25 AM   #9
frenchn00b
Senior Member
 
Registered: Jun 2007
Location: E.U., Mountains :-)
Distribution: Debian, Etch, the greatest
Posts: 2,546

Original Poster
Rep: Reputation: 57
Quote:
Originally Posted by theNbomr View Post
So, your script is littered with shell variables. How are these variables given values? The script as shown does nothing to give them any values, and when cron runs the script, it doesn't know anything about any variables you may have set last week in some shell session.
--- rod.
ok so what the trick to get the user full environment like xterm?

let's give a try

Quote:
#!/bin/sh
############variables

SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=xavier
HOME=/home/xavier
set >> $HOME/.set_resultcron


cd
mkdir emaildaily
cd emaildaily

cat $HOME/.emaildaily | while read COL ; do


wget --user-agent Mozilla/4.0 -k "$COL" -O daily-page.html

ls -lah daily-page.html


mutt -F ~/.muttrc -e 'set content_type="text/html"' XXXXXXXXXX@gmail.com -s "$(date) Dailyemail $CONFIGFILE" < daily-page.html
echo "$(date), Email sent to $EMAILTOBESENT"
echo "$(date), Email sent to $EMAILTOBESENT" >> .log

done


rm daily-page.html


exit 0


Last edited by frenchn00b; 02-20-2010 at 01:38 AM.
 
Old 02-20-2010, 02:06 AM   #10
frenchn00b
Senior Member
 
Registered: Jun 2007
Location: E.U., Mountains :-)
Distribution: Debian, Etch, the greatest
Posts: 2,546

Original Poster
Rep: Reputation: 57
Tada !! It works !! I had to upgrade the version of mutt to the last one.

and use now :
below

but I dont like it much, I wish it could be automatic, and directly from crontab to loads before those, and not within the script :
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=xavier
HOME=/home/xavier

Code:
#!/bin/sh
############variables

SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=xavier
HOME=/home/xavier
set > $HOME/.set_resultcron


cd
mkdir emaildaily
cd emaildaily

cat $HOME/.emaildaily | while read COL ; do 


wget  --user-agent Mozilla/4.0   -k "$COL" -O daily-page.html
  
        ls -lah daily-page.html
        

mutt -F  ~/.muttrc  -e 'set content_type="text/html"'  -s "$(date) Dailyemail $CONFIGFILE" -- XXXXXXX@gmail.com  < daily-page.html
        echo "$(date), Email sent to $EMAILTOBESENT"
        echo "$(date), Email sent to $EMAILTOBESENT" >> .log

done 


rm daily-page.html


exit 0
 
Old 02-20-2010, 04:12 AM   #11
frenchn00b
Senior Member
 
Registered: Jun 2007
Location: E.U., Mountains :-)
Distribution: Debian, Etch, the greatest
Posts: 2,546

Original Poster
Rep: Reputation: 57
OK, I made the script:

It works fine:

Code:
#!/bin/sh
############variables

SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=`whoami`
HOME=/home/`whoami`
set > $HOME/.set_resultcron
cd
[ ! -d emaildaily ] && mkdir emaildaily
cd emaildaily
rm daily-pagetmp.html 
rm daily-page.html 
EMAILTOBESENT="" 
USERNAMEZ=""
USERARGPASSED="$1"
USERARGPASSED2="$2"


ProcedureCheck () { 
  if [ "$USERNAMEZ" = "$USERARGPASSED" ] ; then 
          echo "Normal user mode of $USERNAMEZ"
          printf "\033[33m%-10s\033[0m"  ">> URL: $COL => EMAIL: $EMAILTOBESENT" ; echo ""
          if [ "$COL" != "" ] && [ "$EMAILTOBESENT" != "" ] ; then 
	      ProcedureTest
              [ "$USERARGPASSED2" = "--testing"  ]  && echo "Testing mode, no sending with mutt"
	      [ "$USERARGPASSED2" != "--testing"  ]  && ProcedureSendmeemail	
	  fi
    else 
       echo 
	  printf "\033[31m%-10s\033[0m"  "User not corresponding : $USERNAMEZ  to argument: $USERARGPASSED " ; echo ""
          printf "\033[31m%-10s\033[0m"  ">> URL: $COL => EMAIL: $EMAILTOBESENT" ; echo ""
  fi 
}
 

ProcedureTest () { 
echo "TESTING ... : URL: $COL => EMAIL: $EMAILTOBESENT   USER: $USERARGPASSED"
}

ProcedureSendmeemail () { 
echo "Sending with mutt advanced... URL: $COL => EMAIL: $EMAILTOBESENT   USER: $USERARGPASSED  " 
[ -f daily-page.html ]  && echo "Found a kept page!"
wget  --user-agent Mozilla/4.0   -k "$COL" -O daily-pagetmp.html
cat  daily-pagetmp.html >> daily-page.html 
ls -lah daily-page.html
mutt -F  ~/.muttrc  -e 'set content_type="text/html"'  -s "$(date) Dailyemail to $USERARGPASSED (tell me which webpages you want daily) " -- $EMAILTOBESENT  < daily-page.html
echo "$(date), Email sent to $EMAILTOBESENT"
echo "$(date), Email sent to $EMAILTOBESENT" >> .log
rm daily-pagetmp.html 
}



if [ "$USERNAMEZ" = "$USERARGPASSED" ] ; then 
    echo "Running dailyemail script for the user: $USERARGPASSED" 
fi

cat $HOME/.emaildailyrc | while read COL ; do 
  VUE="0"
  if [ "`echo $COL | cut -c 1-7`" = "#email=" ] ; then 
        #EMAILTOBESENT=`echo "$each" | awk -F" : " '{print $2}' | awk '{ gsub(" : ","");print}'  | awk '{ gsub(" :","");print}'`
	EMAILTOBESENT=`echo "$COL" | cut -d"=" -f2 `
	printf "\033[32m%-10s\033[0m" "Active email: $EMAILTOBESENT"  ; echo ""     
	VUE="1"
  fi

  if [ "`echo $COL | cut -c 1-6`" = "#user=" ] ; then 
        #EMAILTOBESENT=`echo "$each" | awk -F" : " '{print $2}' | awk '{ gsub(" : ","");print}'  | awk '{ gsub(" :","");print}'`
        USERNAMEZ=`echo "$COL" | cut  -d"=" -f2 `
        printf "\033[32m%-10s\033[0m" "Active user: $USERNAMEZ" ; echo ""
        VUE="1"
  fi

  if [ "`echo $COL | cut -c 1-1`" != "#" ] && [ "$VUE" = "0" ] ; then 
      if [ "`echo $COL | cut -c 1-5`" = "keep=" ] ; then 
	COL=`echo $COL | sed s/keep=//g`
	if [ "$COL" != "" ] ; then 
	  echo "Procedure checking URL... URL= $COL" 
	  echo "Keeping mode of $COL *skip to next page for sending*"
	  wget  --user-agent Mozilla/4.0   -k "$COL" -O daily-page.html
	  #ProcedureCheck
	fi
      else
	if [ "$COL" != "" ] ; then 
	  echo "Procedure checking URL... URL= $COL" 
	  ProcedureCheck
          [ -f daily-page.html ] && rm daily-page.html
	fi
      fi 
  fi

# rem page
  VUE="0"
done 
[ -f daily-page.html ] && rm daily-page.html
exit 0
 
Old 02-21-2010, 11:48 PM   #12
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.10, Centos 7.5
Posts: 17,609

Rep: Reputation: 2445Reputation: 2445Reputation: 2445Reputation: 2445Reputation: 2445Reputation: 2445Reputation: 2445Reputation: 2445Reputation: 2445Reputation: 2445Reputation: 2445
Actually, you can set env vars in cron and some have default values, the only issue is you have to be careful that different programs don't expect different values.
Its also more secure to use absolute paths to cmds/files.
http://linux.die.net/man/5/crontab
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Crontab is not working, the script is working arfal SUSE / openSUSE 6 02-08-2010 08:48 PM
mutt is not working for a particular user Hi_This_is_Dev Linux - General 3 09-15-2009 01:27 PM
Mutt experts - Mutt won't use the Maildir I specify in .muttrc moistTowelette Debian 2 08-24-2009 08:04 AM
sendmail masquerading not working with mail/mutt Hcman Linux - Software 0 11-10-2004 07:57 AM
crontab not working subaruwrx Linux - Newbie 8 08-17-2004 09:12 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 05:36 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration