0

I have a script and it doesn't work proper, so in bash I let script in while loop and I wanna my script can close itself after a while, I tried to use threading.timer but my code wont run quit() or exit() command, could anyone please help me?

#!/usr/bin/env python3
import threading
from time import sleep

def qu():
  print("bye")
  exit()

t=threading.Timer(5.0,qu)
t.start()

while(True):
  sleep(1)
  print("hi")
Martin Alonso
  • 726
  • 2
  • 9
  • 16
Saeb Molaee
  • 109
  • 15

1 Answers1

1

You could use the os._exit function instead of exit()

Getting the code as follows:

#!/usr/bin/env python3
import threading
import os
from time import sleep

def qu():
    print("bye")
    os._exit(0)

t=threading.Timer(5.0,qu)
t.start()

while(True):
    sleep(1)
    print("hi")

Anyways I would suggest you to checkout this question as it is similar to yours.

Martin Alonso
  • 726
  • 2
  • 9
  • 16
  • tnx , for recalling my script i use `while [ True ];do python3 script.py;sleep 5;`done;` is there any way I recall my script by pytion itself? – Saeb Molaee Aug 14 '17 at 20:26
  • @SaebMolaee you're welcome, to call any script from python you can use the [subprocess module](https://docs.python.org/3/library/subprocess.html) – Martin Alonso Aug 14 '17 at 21:11