0

I have this so far. The issue is, if pythoncode.py started before 8*60*60 seconds, then it can still continue running beyond 8*60*60 seconds until it finishes and only then will it stop. I need it to terminate pythoncode.py at exactly (loosely, +- 1 second is not an issue) 8*60*60 seconds even if it is running.

#! /bin/bash
end=$((SECONDS+8*60*60))

while [ $SECONDS -lt $end ]; do
    python3 /Users/Name/Desktop/pythoncode.py
done

I'm a beginner so explaining what to do specifically with this example would be appreciated over just a general answer. Thanks and happy new year.

  • 1
    Possibly related: [Automatically kill process if its runtime exceeds some predetermined value](https://unix.stackexchange.com/questions/21542/automatically-kill-process-if-its-runtime-exceeds-some-predetermined-value) – steeldriver Jan 02 '21 at 02:15

1 Answers1

1

You can call your python script in the background and use sleep function as a timer :

#Execute the script in the background
python3 /Users/Name/Desktop/pythoncode.py & 
#Get its process id
pypid=$!
#Wait for the needed period in seconds
sleep "$end"
#Then force the process to be terminated
kill -9 $pypid
Reda Salih
  • 1,724
  • 4
  • 9