I need a crontab script

Discussion in 'Programming/Scripts' started by bschultz, Apr 25, 2011.

  1. bschultz

    bschultz Member

    I need a cron script (either php or shell) that will:

    - delete all mp3's in a given directory
    - use wget to download the "new" mp3's
    - if wget failed (404 or any other reason) email (using sendmail) an error report
    - if wget downloaded the files successfully, chmod a remote directory (the remote server is a Mac, so the permissions change at will!), then scp them to the remote server (Mac, so wget doesn't work so well...or I'd run the script on that server)
    - if the scp didn't work (permissions, internet connection or any other reason) sendmail an error report

    I have everything working except the emailing of the error report....and the chmod.


    Here's what I have:

    Code:
    #!/bin/sh
    set -e
    
    cp /backup.mp3 /var/www/showdownloads/XXX/backup.mp3
    cd /var/www/showdownloads/XXX
    rm *.mp3
    wget -nd -r -l1 --no-parent -A.mp3 ftp://XXX:XXXX@XXX.XXX.XXX.XXX/directory/
    scp *.mp3 downloads@192.168.2.169:"/Volumes/XXX"
    exit
    
    Any ideas?

    Thanks!
     
  2. bschultz

    bschultz Member

    I've finally had some time to work on this again.

    I've decided to add error checking (based on exit codes) and email from the code I already have working.

    Here's the problem I've run into:

    I tried to add the error checking after the scp command (while including a non-working IP addres - - - trying to make it fail). The exit code of the scp command is returning 0 (success) on a "lost connection" attempt.

    Here's my code:

    Code:
    #!/bin/bash
    #set -o errexit -o nounset -o xtrace
    
    cd /var/www/weather/
    
    if [ $? -ne 0 ] ; then
    SUBJECT="Weather Currents Download Problem Encountered"
    # Email To ?
    EMAIL="me@me.com"
    # Email text/message
    EMAILMESSAGE="There was a problem downloading the currents for the weather.  Please check the server."
    echo "This is an email message test"> $EMAILMESSAGE
    echo "This is email text" >>$EMAILMESSAGE
    # send an email using /bin/mail
    /usr/sbin/sendmail -s "$SUBJECT" "$EMAIL" < $EMAILMESSAGE
    exit 1
    fi
    
    
    
    wget --no-passive-ftp 'ftp://user:password@remote_ip_address/Current.mp3'
    
    if [ $? -ne 0 ] ; then
    SUBJECT="Weather Currents Download Problem Encountered"
    # Email To ?
    EMAIL="me@me.com"
    # Email text/message
    EMAILMESSAGE="There was a problem downloading the currents for the weather.  Please check the server."
    echo "This is an email message test"> $EMAILMESSAGE
    echo "This is email text" >>$EMAILMESSAGE
    # send an email using /bin/mail
    /usr/sbin/sendmail -s "$SUBJECT" "$EMAIL" < $EMAILMESSAGE
    exit 1
    fi
    
    
    
    echo `scp Current.mp3 root@192.168.2.246:"/Volumes/Big\ Disk/ZWEATHER"`
    
    if [ $? -ne 0 ] ; then
    SUBJECT="Weather Currents Download Problem Encountered"
    # Email To ?
    EMAIL="me@me.com"
    # Email text/message
    EMAILMESSAGE="There was a problem downloading the currents for the weather.  Please check the server."
    echo "This is an email message test"> $EMAILMESSAGE
    echo "This is email text" >>$EMAILMESSAGE
    # send an email using /bin/mail
    /usr/sbin/sendmail -s "$SUBJECT" "$EMAIL" < $EMAILMESSAGE
    exit 1
    fi
    
    
    
    
    
    echo `rm Current.mp3`
    
    if [ $? -ne 0 ] ; then
    SUBJECT="Weather Currents Download Problem Encountered"
    # Email To ?
    EMAIL="me@me.com"
    # Email text/message
    EMAILMESSAGE="There was a problem downloading the currents for the weather.  Please check the server."
    echo "This is an email message test"> $EMAILMESSAGE
    echo "This is email text" >>$EMAILMESSAGE
    # send an email using /bin/mail
    /usr/sbin/sendmail -s "$SUBJECT" "$EMAIL" < $EMAILMESSAGE
    exit 1
    fi
    
    exit
    
    Here's the command run in Putty

    192.168.2.246 doesn't exist...and should return an exit code of >0. Why would this return an exit code of 0?
     
  3. bschultz

    bschultz Member

    I found it...I had copied and pasted a portion of previous code.

    The echo on the scp command caused the exit code of 0. After all, it did echo the command.

    As soon as I took out the echo and just used scp... everything worked as it should.
     

Share This Page