Resume from last failed command


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Resume from last failed command
# 15  
Old 09-30-2014
If you want to do it without a makefile:

Code:
#!/bin/sh

if [ "$#" -gt 0 ]
then
        X="$1"
elif [ -f lastfile ]
then
        read X < lastfile
fi

[ -z "$X" ] && X=1

R=1

while [ "$R" -eq 1 ]
do
        case "$X" in
        1)      command1 ;;
        2)      command2 ;;
        3)      command3 ;;
        4)      command4 ;;
        5)      command5 ;;
        *)      R=0 ;;
        esac

        if [ "$?" -ne 0 ]
        then
                echo "Error in loop $X" >&2
                exit 1
        fi

        let X=X+1
        # Save last-succeeded-operation + 1
        echo $X > lastfile
done

echo "Completed all loops, removing flag file"
rm -f lastfile

Then you could do ./script and it would remember where it left off, or ./script 2 to force it.

Last edited by Corona688; 09-30-2014 at 03:21 PM..
These 2 Users Gave Thanks to Corona688 For This Post:
# 16  
Old 09-30-2014
Hi,

OK, I can't let you have the whole script - but below is a typical section from the script. Please bear in mind that this is a singlr section of the script and that there are many such sections.

Code:
# ----------------------------------------------------------------------------------------------------
# check filesystems if not exists then create it"
# ----------------------------------------------------------------------------------------------------
#
#   STEPTOINFO[  4 ]=0 # check filesystems if not exists then create it
    STEPCOUNT=4
    if [ ${STEPTOINFO[$STEPCOUNT]} -ne 0 ]; then
#   {
        printf "$STEPCOUNT" >  "$SAVE_LAST_BREAK"    # save actally position
        printf "$STEPCOUNT; " >> "$SAVE_LAST_ACTIVITY"
#
        if [ "$GLOBAL_ZONE" = "$TRUE" ]; then
#       {
# ----------------------------------------------------------------------------------------------------
#
            ANZINFORMATION=$((ANZINFORMATION + 1))
            INSTALLINFORMATION[$ANZINFORMATION]="SECTION -  %2d : ${SECTION_INFO[$STEPCOUNT]}"
#
            showheader "${INSTALLINFORMATION[$ANZINFORMATION]}" "$STEPCOUNT" "$BACKGROUND"
#
            ERRORFOUND=0
            ERRORINFORMATION[$ANZINFORMATION]="Perfect"
#
# ----------------------------------------------------------------------------------------------------
# needed Filesystems:
#
            ZFS_RPOOL_FILESYS="rpool/UC4                                                            \
                               rpool/export rpool/export/home                                       \
                               rpool/local  rpool/local/Tivoli rpool/local/bmc rpool/local/oracle   \
                               rpool/zzz    rpool/zzz/core                                          \
                               rpool/oracle                                                         \
                               rpool/TAD4D"
# save this info in an array
#
            i=-1
            for ZFS_RP_FS in $ZFS_RPOOL_FILESYS
            do
                i=$((i + 1)); ZFS_RPOOLFS[$i]=$ZFS_RP_FS
            done
#
# ----------------------------------------------------------------------------------------------------
# needed Mountpoint for zfs filesystems:
#
            i=-1
            ZFS_FILESYSTEMS="/UC4                                                                \
                             /export     /export/home                                            \
                             /usr/local  /usr/local/Tivoli  /usr/local/bmc /usr/local/oracle     \
                             /zzz        /zzz/core                                               \
                             /oracle                                                             \
                             /opt/TAD4D"
# save this info in an array
#
            i=-1
            for ZFS_FS in $ZFS_FILESYSTEMS
            do

                i=$((i + 1)); ZFS_RPOOLFS[$i]=$ZFS_RP_FS
            done
#
# ----------------------------------------------------------------------------------------------------
# needed Mountpoint for zfs filesystems:
#
            i=-1
            ZFS_FILESYSTEMS="/UC4                                                                \
                             /export     /export/home                                            \
                             /usr/local  /usr/local/Tivoli  /usr/local/bmc /usr/local/oracle     \
                             /zzz        /zzz/core                                               \
                             /oracle                                                             \
                             /opt/TAD4D"
# save this info in an array
#
            i=-1
            for ZFS_FS in $ZFS_FILESYSTEMS
            do
                i=$((i + 1)); ZFS_FILESYS[$i]=$ZFS_FS
            done
#
# ----------------------------------------------------------------------------------------------------
# needed quotas for zfs filesystems:
#
            ZFS_FILESYS_QUOTAS="250M                                                              \
                                  3G          2G                                                  \
                                  3G          262M               520M            520M             \
                                  2G          1G                                                  \
                                 10G                                                              \
                                250M"
# save this info in an array
#
            i=-1
            for ZFS_FSQ in $ZFS_FILESYS_QUOTAS
            do
                i=$((i + 1)); ZFS_FSQUOTAS[$i]=$ZFS_FSQ
            done
#
# ----------------------------------------------------------------------------------------------------
#
            i=-1
            while [ $i -lt $(( ${#ZFS_FILESYS[*]} - 1)) ]
            do
#           {
                i=$((i + 1))
                typeset -L20 TMP_ZFS_RPOOLFS
                typeset -L20 TMP_ZFS_FILESYS
                TMP_ZFS_RPOOLFS="'${ZFS_RPOOLFS[$i]}'"
                TMP_ZFS_FILESYS="'${ZFS_FILESYS[$i]}'"
#
                /usr/sbin/zfs list | grep "${ZFS_RPOOLFS[$i]}" > /dev/null 2>&1
                RC=$?
                if [ $RC -eq 0 ]; then
#               {
#           {
                i=$((i + 1))
                typeset -L20 TMP_ZFS_RPOOLFS
                typeset -L20 TMP_ZFS_FILESYS
                TMP_ZFS_RPOOLFS="'${ZFS_RPOOLFS[$i]}'"
                TMP_ZFS_FILESYS="'${ZFS_FILESYS[$i]}'"
#
                /usr/sbin/zfs list | grep "${ZFS_RPOOLFS[$i]}" > /dev/null 2>&1
                RC=$?
                if [ $RC -eq 0 ]; then
#               {
                    if [ -d "${ZFS_FILESYS[$i]}" ]; then
#                   {
# >> zfs rpool/fs is available !!!
# >> directory    is available !!!
                        showmessages "...     NOTE: $TMP_ZFS_RPOOLFS and $TMP_ZFS_FILESYS exists" "Perfect" "$BACKGROUND"
#                   }
                    else
#                   {
                        showmessages "...     NOTE: $TMP_ZFS_FILESYS not exists" "Faulty" "$BACKGROUND"
                        ERRORFOUND=1
#                   }
                    fi
#               }
                else
#               {
# >> zfs rpool/fs is NOT available !!!
                    showmessages "...     NOTE: $TMP_ZFS_RPOOLFS not exists" "Faulty" "$BACKGROUND"
                    if [ ! -d "${ZFS_FILESYS[$i]}" ]; then
#                   {
# >> directory is NOT available !!!"
                        showmessages "...     NOTE: $TMP_ZFS_FILESYS not exists" "Faulty" "$BACKGROUND"
                        ERRORFOUND=1
#                   }
                    else
#                   {
                        showmessages "...     NOTE: $TMP_ZFS_FILESYS is a directory not a ZFS-Filesystem from $TMP_ZFS_RPOOLFS" \
                                     "Faulty"                                                                                   \
                                     "$BACKGROUND"
                        ERRORFOUND=1
#                   }
                    fi
#               }
                fi
#           }
            done
#
# --------------------------------------------------------------------------------------------------------
#
        if [ $ERRORFOUND -ne 0 ]; then
            GLOBAL_ERROR=-1
            ERRORINFORMATION[$ANZINFORMATION]="Faulty"
        fi
        fi
#
# ----------------------------------------------------------------------------------------------------
#
            if [ $RETURNNEEDED -eq 0 ]; then
                printf "\nenter <RETURN> to continue : "; read FORWARD
            fi
#       }
        fi
#   }
    fi

You should be able to see how the checking is done in each section from this.

Regards

Dave
# 17  
Old 09-30-2014
@ Corona688: there's a missing closing quote if [ "$? -ne 0 ]
But nice one otherwise, i've tried a similar one with just a loop, but since the loop gone through all things, the error was overwritten and not reportet.. i'd had expected seomthing similar for a case statement..
As in, the case got catched and therefor would return true... thx for clearing this wrong assumption Smilie
# 18  
Old 09-30-2014
Quote:
Originally Posted by sea
@ Corona688: there's a missing closing quote if [ "$? -ne 0 ]
Thanks, fixed.

Quote:
But nice one otherwise, i've tried a similar one with just a loop, but since the loop gone through all things, the error was overwritten and not reportet.. i'd had expected seomthing similar for a case statement..
The trick is to save the next number after every success; otherwise, don't bother saving. Otherwise, you will be off-by-one...

Also, don't put anything but the program line in the cases, one per case. If you put anything else, it will pollute the value of $?. If you want any logging/etc, put it above the case or after the save.
# 19  
Old 09-30-2014
Hi,

The script, will save the step count at the beginning of each step. When you run the script a second or subsequent time, the script will check the section to start at - as it writes the count at the beginning of each section. The script will start at the section that has failed.

Regards

Dave
# 20  
Old 10-10-2014
Wow, this worked. Thank you !!

Quote:
Originally Posted by Corona688
If you want to do it without a makefile:

Code:
#!/bin/sh

if [ "$#" -gt 0 ]
then
        X="$1"
elif [ -f lastfile ]
then
        read X < lastfile
fi

[ -z "$X" ] && X=1

R=1

while [ "$R" -eq 1 ]
do
        case "$X" in
        1)      command1 ;;
        2)      command2 ;;
        3)      command3 ;;
        4)      command4 ;;
        5)      command5 ;;
        *)      R=0 ;;
        esac

        if [ "$?" -ne 0 ]
        then
                echo "Error in loop $X" >&2
                exit 1
        fi

        let X=X+1
        # Save last-succeeded-operation + 1
        echo $X > lastfile
done

echo "Completed all loops, removing flag file"
rm -f lastfile

Then you could do ./script and it would remember where it left off, or ./script 2 to force it.
This User Gave Thanks to rprasad For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Solaris

Command failed for target 'check-recursive' error

Sorry if this is the wrong place for this. I haven't done UNIX Admin in a long time and am trying to update the utilities on a Solaris server so that I can get Apache 2.4.25 installed. I am finding a lot of utilities that are just too far out of date and some versions are no longer supported. So... (0 Replies)
Discussion started by: PJ_Namias
0 Replies

2. Solaris

Script redirect command output failed, why?

Hi, I put a for loop in a script to eject backup tapes from the robot. The command echo' output goes to the log file without problem, but command vmchange's output does not go to the log file although it's working fine. It still displays on the screen. I've tried '2>&1 1>$log', but nothing changed.... (5 Replies)
Discussion started by: aixlover
5 Replies

3. Hardware

Cannot resume from suspend with new motherboardktop, does not resume properly

I would like to get pm-suspend (or any other suspend method) working for a small new desktop computer. It is based on a Zotac GF-8200 ITX motherboard and an AMD Athlon II X@ 240 CPU using ArchLinux x86_64. The pm-suspend script works, apparently putting the machine into suspend correctly... (0 Replies)
Discussion started by: lagagnon
0 Replies

4. Solaris

command 'cc' failed even though gcc is installed

I'm trying to build some python modules on a Solaris 10 machine. It has gcc as /usr/sfw/bin/gcc. # CC=gcc python setup.py build running build running build_py running build_ext cc -c actread.c -o actread.o unable to execute cc: No such file or directory error: command 'cc' failed with exit... (8 Replies)
Discussion started by: aussieos
8 Replies

5. Shell Programming and Scripting

Script exits with $? not 0 randomly, how can I see what command failed?

Hi! I have this situation with 3 shellscripts. One is a "startscript" that simply calls other scripts. This one is scheduled with cron to run at regular intervals. That script runs what I'll refer to as Script 1. Script 1 in turn runs script 2 (import_catalogs_buyer.sh) Sometimes, seemingly... (2 Replies)
Discussion started by: trailsmoke
2 Replies

6. Shell Programming and Scripting

Rerunning a command in a script that failed?

I have a script that occasionally has a command here and there that fails and I would like to set my script up to just re run the command if the exit code is 1. Is there a simple way to do that without if/thens or redirecting to the command again? (5 Replies)
Discussion started by: trey85stang
5 Replies

7. UNIX for Advanced & Expert Users

rm & mv command failed due to too many files.

In our directory there are too many files, & if I try to execute mv *.gz or rm *.l command it fails, providing error string as - 'arg list too long'. This doesnt happen always, is there any way we know, limit on the rm & mv command so we can take care of this failure in future executions ? (9 Replies)
Discussion started by: videsh77
9 Replies

8. UNIX for Dummies Questions & Answers

Problem with Mail command: exec failed. errno=2.

I am trying to send email using the "mail" command. I keep getting an "exec failed. errno=2." message. Here is one the commands I have tried: mail test@mycompany.com < test1.out Any ideas what may be causing this error msg? (1 Reply)
Discussion started by: hawkman2k
1 Replies

9. What is on Your Mind?

Are companies viewing my resume? How do I track my resume visits?

Hi everybody, I am wondering if there is any tool or website out there which can track who is viewing my resume. It is very frustrating when you send your CV or Cover Letter and you receive no feedback from the company, you don't even know if they have checked it out. Thanks for your help (1 Reply)
Discussion started by: gearyipswich
1 Replies

10. Shell Programming and Scripting

Failed to get value from a file using sed command

Hi folks, I have the following file (tnsnames.ora): DB10g = (DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = buffy)(PORT = 1521)) (CONNECT_DATA = (SERVER = DEDICATED) (SERVICE_NAME = DB10g) ) ) EXTPROC_CONNECTION_DATA = (DESCRIPTION = (ADDRESS_LIST = ... (2 Replies)
Discussion started by: nir_s
2 Replies
Login or Register to Ask a Question