0

I would like to create a bash script which runs a command, then ends it after 10 minutes (in itself not difficult). However, if called again I want to it reset the timeout of the original script to 0 and exit.

The purpose is to have a command which runs as long as the script has been called in the last 10 minutes. I did think about file + timestamp, but it doesn't an elegant solution. Perhaps signalling?

Thanks in advance! James

3
  • Will Timeout should be 0 permanently after First execution? or Do you want it to get reset to 10 mins after some time?? Commented Apr 4, 2014 at 12:33
  • Yes, I would suggest using signalling to achieve this Commented Apr 4, 2014 at 14:32
  • @VasantaKoli I want to reset to a 10 minute timeout again Commented Apr 4, 2014 at 22:03

2 Answers 2

2

Save the process id of the script to a file when the script is run and remove the file when your script is finished running. If the file with the PID in exists, you can use that as a condition to send a signal to the process to reset the counter.

#Set the counter to 0 at start and reset the counter on receiving signal USR1
i=0
trap "i=0" USR1

#If script already running, send signal to the PID and exit
pid_file=/tmp/myscriptpid
if [[ -e $pid_file ]]
then
  kill -s USR1 $(cat $pid_file)
  exit 0
fi

#Otherwise capture the PID and save to file, clean up on exit
echo "$$" >$pid_file
trap 'rm -f "$pid_file"' EXIT

Then you'll want to run your command in the background and kill it once you're done:

#Run the command in the background
your_command &
your_command_pid=$!

#Increment $i once each second. 
count() {
  sleep 1
  ((i++))
}

#Note that $i is reset to 0 if the script receives the USR1 signal.
while (( $i < 600 ))
do
  count
done

#Kill the running command once the counter is up to 600 seconds
kill "$your_command_pid"
Sign up to request clarification or add additional context in comments.

2 Comments

Hi, sorry maybe I didn't explain well. I want the command to run along as the file keeping being called. If the file hasn't been called, that's when I need it to die. Think the code helps me on the right path though, thanks :)
I assumed the command you want to run does not terminate and runs indefinitely. If you want to call the same command again and again over the 10 minute period (unless the 10 minute timeout is renewed by running this script again), you'll want to stick the command call in the while loop somewhere.
0

Hi jleck

Check below script and update.

#!/bin/bash
now=$(date +%s)
final=$(awk 'END {print}' /var/log/count.log 2> /dev/null)
when=$(date --date @${final} 2> /dev/null)

if (($(echo $final | wc -w) > 0)); then
        t1=$(expr $(expr $now - $final) / 60)
        if (($t1 < 30)); then
                time=0
        else
                time=600
        fi
else
        time=600
fi

if (( $time > 0 )); then
        your_command &
        PID=$!
        sleep $time
        kill -9 $PID
        date +%s >> /var/log/count.log
else
        echo "Command Was Executed on $when, Kindly Execute after $(expr 30 - $t1) minutes"
fi

In this after First execution timeout will reset to 0 and will be again 10 minutes after 30 Minutes Diffrence

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.