On a VPS, i have a Python script running 24/7. For various reasons I need that script to reboot every tot. hours, basically it just needs to restart every 2-3-4 hours. I might be able to find a solution to do that in code, but I would rather see if i can do that from Linux.

To start it, I use nohup python3 myscript.py. Is there any way to do that? Do i need an external dependency?

  • 2
    What does tot. hours mean? You can put in a script, call it script &, wait for some time, kill it and start it again in a loop. Commented Apr 30, 2020 at 8:10
  • I can't do it manually, it needs to do it by itself; so every 6 hours, for example, the process should be killed and restart instantly
    – Jack022
    Commented Apr 30, 2020 at 8:15

1 Answer 1


You can run use cron to schedule the script every X hours. When the new instance starts, it

  • locates the existing instance (checking /var/run/{scriptname}.pid)
  • kills it if it exists
  • write its own process id to /var/run/{scriptname}.pid
  • continues until killed by next

Variants of cron may also let you start the script when the system boots.

A better solution is to create/install your script as a systemd service, and have it be restarted periodically.

  • This is the most reasonable way to go, thank you!
    – Jack022
    Commented Apr 30, 2020 at 8:56

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .