LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 08-14-2009, 12:02 PM   #1
cmfarley19
Member
 
Registered: Nov 2002
Location: Central VA
Distribution: Ubuntu/Debian
Posts: 228

Rep: Reputation: 32
Bash script works from command line, fails from cron


I have a script that backs up my wordpress database. Upon successful complete it e-mails me the results using ssmpt.
Here is the script:
Code:
#! /bin/bash
CMD0="`basename $0`"
TEMPLOGX="/var/log/scripts/TEMP${CMD0}.log"
LOGX="/var/log/scripts/${CMD0}.log"
BUDIR="/var/local/backups" ## Local back up dir
BLOGDIR="/data/web-site/wp"
DATESTAMP=`date '+%Y%m%d'`
#
echo "*************************" | tee -a $LOGX
echo "FROM: Sender <sender@sender.net>" > $TEMPLOGX
echo "TO: Recipient <recipient@recipient.net>" >> $TEMPLOGX
echo "SUBJECT: Wordpress Backup - `date '+%Y%m%d %H:%M:%S - %A'`" >> $TEMPLOGX
echo "" >> $TEMPLOGX
echo "now=`date +\"%Y%m%d %H:%M:%S - %A\"`" | tee -a $TEMPLOGX
echo "CMD0=$CMD0 $*" | tee -a $TEMPLOGX
echo "LOGX=$LOGX" | tee -a $TEMPLOGX
echo "PATH=$PATH" | tee -a $TEMPLOGX

mysqldump --opt --user wp_user  --password=wp_pass wp_ff_data > $BUDIR/mysql/wp_ff_data_$DATESTAMP.sql


tar -czf $BUDIR/wp/foofarley_$DATESTAMP.tar.gz $BLOGDIR

mount -t smbfs //Master2/Linux /media/Linux/ -o username=smb_user%smb_pass && \
        var="SUCCESS"


if [ $var ]
        then
#       echo $var
        find /media/Linux/mysql -type f -mtime +5 -exec rm {} \;
        cp /var/local/backups/mysql/*.sql /media/Linux/mysql
        rm -f /var/local/backups/mysql/*.sql

        find /media/Linux/wordpress -type f -mtime +5 -exec rm {} \;
        cp /var/local/backups/wp/*.gz /media/Linux/wordpress
        rm -f /var/local/backups/wp/*.gz

        umount /media/Linux/

        echo "Backed-up to //Master2/Linux/mysql/" | tee -a $TEMPLOGX
        cat $TEMPLOGX | tee -a $LOGX
        cat $TEMPLOGX | ssmtp recipient@recipient.net
        rm $TEMPLOGX
fi
This works exactly as I expect it if I invoke it directly at the command line. I receive an e-mail with the success results. If I call this from root's crontab however, I receive the following error:
Code:
tar: Removing leading `/' from member names
/usr/local/scripts/foofarley_backup.sh: line 43: ssmtp: command not found
It says it does not know what ssmtp is?

Any ideas what would cause this behavior?

Chris
 
Old 08-14-2009, 12:13 PM   #2
weibullguy
ReliaFree Maintainer
 
Registered: Aug 2004
Location: Kalamazoo, Michigan
Distribution: Slackware 14.2
Posts: 2,815
Blog Entries: 1

Rep: Reputation: 261Reputation: 261Reputation: 261
Try using the full path to ssmtp.
 
Old 08-14-2009, 12:23 PM   #3
cmfarley19
Member
 
Registered: Nov 2002
Location: Central VA
Distribution: Ubuntu/Debian
Posts: 228

Original Poster
Rep: Reputation: 32
Duh... didn't think of that. Thanks. I'll give it a shot.
 
Old 08-14-2009, 01:09 PM   #4
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198
And a lot more besides!

Easiest solution with a working script is to change the first line to
Code:
#! /bin/bash -l
(thats a letter l) to simulate the login environment including the crucial $PATH variable
 
Old 08-14-2009, 01:24 PM   #5
cmfarley19
Member
 
Registered: Nov 2002
Location: Central VA
Distribution: Ubuntu/Debian
Posts: 228

Original Poster
Rep: Reputation: 32
catkin to the rescue on two of my issues today!!!
Thanks a lot!

Chris
 
  


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
works on command line but not in bash script tara Linux - General 7 02-09-2009 04:57 AM
Works from command line but not from the cron WingnutOne Linux - General 8 11-08-2007 09:18 AM
Command works in shell, fails as a cron task angeljusto Linux - Software 3 06-13-2007 08:06 AM
btdownload*.py, works from Command Line, not from cron SkipHuffman Linux - Software 11 12-30-2005 03:55 PM

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

All times are GMT -5. The time now is 01:25 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
Open Source Consulting | Domain Registration