0

I have a simple script

#!/usr/bin/env python
# coding: utf-8

i=0

while (True):
    print(i)
    i=i+1

I want this script to run in background If the server crashes, I want it to automatically restart an pick where the program left of How do I do that

PRATHAMESH
  • 123
  • 2
  • 3
  • 11

2 Answers2

0

You have tu run your script as a service: The file must have .service extension under /lib/systemd/system/ directory.

Now Your system service has been added to your service. Let’s reload the systemctl daemon to read new file. You need to reload this deamon each time after making any changes in in .service file.

sudo systemctl daemon-reload

Now enable the service to start on system boot, also start the service using the following commands.

sudo systemctl enable dummy.service
sudo systemctl start dummy.service
Paolo Mossini
  • 1,064
  • 2
  • 15
  • 23
0

I personally use supervisord to handle processes. To make your script start where it left off you need some kind of persistence, like a file or a database where you can put the last state of your script and read at the restart.

qwertasyx
  • 25
  • 8