vinu
vinu

Reputation: 113

bash script to execute from where it was failed last time

Can anyone tell me how to write a script to execute from where it was stopped last time. My bash script contains 24 script files which is executing sequentially. But if any one of the script fails, next time when i execute the script file i dont want the script to start from script1, instead it should start from where it was failed last time. Please advise.

Upvotes: 4

Views: 265

Answers (2)

anishsane
anishsane

Reputation: 20980

One crude way:

#!/bin/bash
# Needs bash 4 or later, for `;&` to work
[ "$1" = "--frest_start" ] && rm statusfile

touch statusfile
read status < statusfile
[ "$status" = "" ] && status=0

case $status in
0) ./script1; echo 1 > statusfile ;&
1) ./script2; echo 2 > statusfile ;&
2) ./script3; echo 3 > statusfile ;&

# ....... & so on till

23) ./script24; echo 24 > statusfile ;;

esac

But doing it via Makefile seems a good solution too...

.NOTPARALLEL
.PHONY: all frest_start

all:
    make step1
    make step2
    make step3
    ....
    make step24

step%: script%
    "./$<"
    touch "$@"

frest_start:
    rm step*
    make all

Upvotes: 2

Nullpointer
Nullpointer

Reputation: 1872

#mail.sh

function errortest {
    "$@"
    local status=$?
    if [ $status -ne 0 ]; then
        echo "error with $1" >&2
    fi
    return $status
}
errortest sh /home/user1/test1.sh
errortest sh /home/user1/test2.sh

whenever occur any error on script, it's give error on terminal.i tested on my machine same.

Upvotes: -1

Related Questions