1

I have a number of Python-based services on a Linux server that I start by running a .py file via a python3 bash command and that are supposed keep running indefinitely. One is a flask app, another is a watchdog script that will keep watching a directory and execute actions whenever a new file is added.

What's the most robust way of running these scripts on the server independently of a user session? I imagined creating a job in crontab that will run the respective Python commands every morning; but in case the processes are still running - won't this create more and more instances of the process each day?

What's the best practice for this? Is there an easy way to terminate the existing process first and then restart it?

4
  • 3
    This question would fit best on Server Fault. If your server is using systemd, this could be a solution for you: Writing a systemd Service in Python. Commented Jul 10 at 14:21
  • 1
    (external) servers usually have special functons to run web page and usually they have some dashboard to configure it, and start it. And they should care to keep it running (and restart it when it failed). Commented Jul 10 at 21:30
  • 1
    btw: as I remeber cron has option @reboot which can start program only when computer is restarting. It can be useful when you run own server - because it can restart your program when you have to restart your server. Commented Jul 10 at 21:32
  • Thanks but it's a production server that is going to run different types of services. I guess the systemd service option is the best solution. Thanks! Commented Jul 11 at 8:00

1 Answer 1

2

Probably better suited for server fault, but anyways:

Create a service. If you're using systemd, the suse documentation has a really good writeup on how to create it.

This will both make sure that there is only one instance running per service, as well as run it indefinitely if the service has no exit points.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.