LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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!

Notices


Reply
  Search this Thread
Old 11-29-2013, 01:28 PM   #1
makem
LQ Newbie
 
Registered: Nov 2013
Posts: 16

Rep: Reputation: Disabled
Trapping errors in script which is started at boot time.


I have a working script which backs up from one HD to another overnight. I need to put some type of trap in the script to deal with errors.

The machine is a Raspberry Pi running Raspbian and the script is in /usr/local/bin and is called from /etc/rc.local.

I have tried this error trap without success:

Code:
#!/bin/bash
f () {
    errcode=$?

v1="Error "
v2=$errcode
v3=" the command executing at the time of the error was " 
v4=$BASH_COMMAND
v5=" on line "
v6=${BASH_LINENO[0]}
v7="$v1 $v2 $v3 $v4 $v5 $v6"

    echo "$v7" | mail -s "Error" makem@myhome.co.uk
    exit $errcode  # or use some other value or do return instead
}
trap f ERR SIGURG

# Test trap with a typo in the filename

    /usr/bin/rsync -avx --delete /media/HDD1/shares/mailwashe /media/HDD2/shares/>>/media/HDD1/shares/rsync_log 2>&1
I test this trap by introducing a typo in one of the backup file names (mailwashe)and the trap works fine in a terminal and sends me an email with all the error info. The error number 23, is also sent to rasync.log and the script completes.

However, if I insert the same error script in my backup script, when it runs at boot time with the same typo, errors are not trapped and the script completes. The script is in /user/local/bin

Can anyone assist in working out why the trap only works from the command prompt?

The complete boot time script with the trap above is:

Code:
#!/bin/bash

# error trap follows
f () {
    errcode=$? # save the exit code as the first thing done in the trap function

v1="Error "
v2=$errcode
v3=" the command executing at the time of the error was " 
v4=$BASH_COMMAND
v5=" on line "
v6=${BASH_LINENO[0]}
v7="$v1 $v2 $v3 $v4 $v5 $v6"

    echo "$v7" | mail -s "Error" makem@myhome.co.uk
    exit $errcode  # or use some other value or do return instead
}
trap f ERR

# backup follows

begin=$(date --date="19:00" +%s)
end=$(date --date="21:00" +%s)
now=$(date +%s)

if [ "$begin" -le "$now" -a "$now" -le "$end" ]; then


        echo "/sbin/shutdown -h 15" | sudo at 20:40
        echo "AllwaySync will backup the PC to HDD1 then I will shut down afterwards" | mail -s "AllwaySync time" makem@myhome.co.uk

else

        begin=$(date --date="3:00" +%s)
        end=$(date --date="6:00" +%s)
        now=$(date +%s)

if [ "$begin" -le "$now" -a "$now" -le "$end" ]; then

        /usr/bin/rsync -avx --delete /media/HDD1/shares/myprofile /media/HDD2/shares/
        /usr/bin/rsync -avx --delete /media/HDD1/shares/hanprofile /media/HDD2/shares/
        /usr/bin/rsync -avx --delete /media/HDD1/shares/mailwasher /media/HDD2/shares/>>/media/HDD1/shares/rsync_log 2>&1
        /usr/bin/rsync -avx --delete /media/HDD1/shares/archive /media/HDD2/shares/

        date >>/media/HDD1/shares/rsync_log 2>&1
        echo "Success" | mail -s "Backup Completed" makem@myhome.co.uk

        /sbin/shutdown -h 25

else

        echo "Playtime but don't forget to shut me down!" | mail -s "rsyncs not being done" makem@myhome.co.uk

        fi

fi
The naff emails were sent during testing and I have left them in for now.

I could just run the script without an error trap as it is, however, having tried to set a trap I really do not want to be beaten.

Last edited by makem; 11-30-2013 at 03:07 PM.
 
Old 12-03-2013, 09:19 AM   #2
elucches
Member
 
Registered: Jan 2011
Posts: 91

Rep: Reputation: 8
Path to mail?
 
Old 12-03-2013, 09:31 AM   #3
makem
LQ Newbie
 
Registered: Nov 2013
Posts: 16

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by elucches View Post
Path to mail?
I don't think so because the paths work correctly if I introduce an error and run the script manually.

The trap does not work when the script is initiated on boot.
 
Old 12-03-2013, 09:34 AM   #4
sag47
Senior Member
 
Registered: Sep 2009
Location: Orange County, CA
Distribution: Kubuntu x64, Raspbian, CentOS
Posts: 1,832
Blog Entries: 36

Rep: Reputation: 451Reputation: 451Reputation: 451Reputation: 451Reputation: 451
You can't assume the paths would be the same in a cron environment depending on your configuration. If you want to be sure you should manually set the $PATH at the beginning of the script.
 
Old 12-03-2013, 10:34 AM   #5
makem
LQ Newbie
 
Registered: Nov 2013
Posts: 16

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by sag47 View Post
You can't assume the paths would be the same in a cron environment depending on your configuration. If you want to be sure you should manually set the $PATH at the beginning of the script.
I will try setting $PATH=/usr/local/bin but I think the problem is that the script is a #!/bin/bash script and files in /etc/rc.local where it is called from expect #!/bin/sh although I use /bin/bash /usr/local/bin/shutauto

I have tried putting the script in /etc/init.d and creating a symlink in /etc/rc2.d (default runlevel) but I keep getting an inserve warning that the link file is corrupt because I am not sure how to properly create it.
 
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
Trying to restore lilo and my hdd after it started getting errors, wont boot ? zonemikel Linux - Software 6 08-24-2009 12:00 AM
Bash script shows strange behaviour when started at boot time michael_kaeppler Linux - General 2 05-11-2008 09:37 PM
Plz help on how to add/remove services started at boot time on linux rs_vijay Linux - Newbie 8 11-12-2006 05:09 AM
problem with too many 'ppp' interfaces started at boot time utumno Linux - Networking 1 06-12-2005 05:11 AM
How Do I Have a script started at boot time? Chijtska Linux - General 12 03-18-2002 06:41 PM


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