0

I'd like to create a script that will do the following. Start at a given time during the day and end at another given time.

So for example, I have a program I'd like to test, so my script would be set to start at say 10:00pm and continue to run until 9:00am.

This follows on from my other question about running programs again and again.

I have the following:

#!/bin/bash

trap "echo manual abort; exit 1"  1 2 3 15;
RUNS=0;
while open  -W /Path/to/Program.app
do
    RUNS=$((RUNS+1));
    echo $RUNS > /temp/autotest_run_count.txt;
done

exit 0

This script essentially runs my program (in Mac OSX) and catches any failures, otherwise it will re-run the program when it closes.

I'd like to be able to run this like I mentioned above. Start at 10:00pm. Finish at 9:00am.

Your advice is always useful.

Thanks!

Euden

  • 3
    If your OS has something like a cron daemon. Have cron start your script at what ever interval you please and also have cron kill all running instances of your program at a slighly shifted interval. – Bananguin Jul 29 '14 at 9:47
1

I have extended your script, so that you can run it once on startup and it will do it's job between 9PM and 9AM.

#!/bin/bash -·
LOGFILE="/tmp/autotest_run_count.txt"

trap "echo manual abort; exit 1"  1 2 3 15
RUNS=0
while [ 1 ] ; do·
    HOUR="$(date +'%H')"
    if [ $HOUR -ge 21 -a $HOUR -lt 9 ] ; then
        # run program
        libreoffice || exit 1
        RUNS=$((RUNS+9))
        echo $RUNS > $LOGFILE
    else
        echo $RUNS, waiting H=$HOUR > $LOGFILE
        # note: calculating the time till next wakeup would be more 
        # efficient, but would not work when the time changes abruptly
        # e.g. a laptop is suspended and resumed
        # so, waiting a minute is reasonably efficient and robust
        sleep 60
    fi
done
1

I use this script. It starts another script at a given time and stops it at another given time. In between it periodically checks if the script is still running and, if not, restarts it. You just have to change starttime, stoptime, scriptname and the shell you are using according to your needs and comment out the relevant "while"-lines (date+time or just time).

    #!/bin/bash

    if [ "$1" = "go" ]; then

        starttime1=0900 #e.g. 201712312355 for date+time or 1530 for time
        stoptime1=1100
        scriptname1="./myscript"

        # wait for starttime
        echo "Waiting for " "$starttime1" " to start " "$scriptname1";
        #while [ $(($(date +"%Y%m%d%H%M") < $starttime1)) = 1 ]; do #day and time
        while [ $((10#$(date +"%H%M") != 10#$starttime1)) = 1 ]; do #just time. 10# forces following number to be interpreted as base 10. Otherwise numbers with leading zero will be interpreted as octal and this causes errors.
            sleep 10;
        done;

        # run target script
        lxterminal -e "$scriptname1";

        # check if the target script is running, until the stoptime is reached and restart it if necessary
        echo "Waiting for " "$stoptime1" " to stop " "$scriptname1";
        #while [ $(($(date +"%Y%m%d%H%M")<$stoptime1)) = 1 ]; do #day and time
        while [ $((10#$(date +"%H%M") != 10#$stoptime1)) = 1 ]; do #just time. 10# forces following number to be interpreted as base 10. Otherwise numbers with leading zero will be interpreted as octal and this causes errors.
            sleep 10;
            if [ -z $(pidof -x "$scriptname1") ]; then
                echo "script was stopped.";
                lxterminal -e "$scriptname1";
            fi;
        done;

        # end the target script when the stoptime is reached
        kill $(pidof -x "$scriptname1");
        echo "ok.";

    else
        lxterminal -e "$0" go;
        exit;
    fi

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.