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
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
4
  • 2
    Would this accomplish the exact same thing? timeout 8h sh -c 'while true; do python3 /Users/Name/Desktop/pythoncode.py; done' – Ardalan Ghazizadeh Jan 2 at 4:31
  • Yes same thing here if you do have timeout installed :) – Reda Salih Jan 2 at 14:27
  • /usr/bin/timeout is in the coreutils package, so most probably it's installed. – glenn jackman Jan 2 at 15:18
  • Great. Thank you! – Ardalan Ghazizadeh Jan 2 at 22:46

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.