Вы находитесь на странице: 1из 2

Writing a Bash script to run and end at specic ...

https://unix.stackexchange.com/questions/1471...

Unix & Linux Stack Exchange works best with JavaScript enabled
sign up

Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like
operating systems.. It's 100% free, no registration required.

log in

tour

Take the 2-minute tour

help

Writing a Bash script to run and end at specific times


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
/ bash

/ shell-script

/ osx

/ time

asked Jul 29 at 8:25


Ben Crazed Up Euden
41

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 at 9:47

1 Answer
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

1 din 2

13.12.2014 18:50

Writing a Bash script to run and end at specic ...

https://unix.stackexchange.com/questions/1471...

Unix & Linux Stack Exchange works best with JavaScript enabled
fi
done

edited Jul 29 at 13:11

answered Jul 29 at 12:30


Floyd
131

2 din 2

13.12.2014 18:50

Вам также может понравиться