0

I have a Python code running on Spyder on our server which needs to run constantly. However, from time to time our server breaks and it is restarted. Unfortunately, my code stops running as well and I need to restart Spyder and the code manually when opening it the next day.

Is there any way to restart the code automatically when the server is restarted?

Thank you a lot!

  • How do you keep state? – Klaus D. Sep 29 '21 at 08:34
  • If your server runs Ubuntu you can create a service for it with [systemd](https://manpages.ubuntu.com/manpages/xenial/en/man5/systemd.service.5.html). This is also available on more linux OSs and there are similar alternatives for other OSs. – Toni Sredanović Sep 29 '21 at 08:36
  • I have my code running on Windows – Data_Science_110 Sep 29 '21 at 08:40
  • You can also restart the code through [crontab](https://man7.org/linux/man-pages/man5/crontab.5.html) and process detection script. On windows, see [Scheduled_tasks_and_cron_jobs_on_Windows](https://active-directory-wp.com/docs/Usage/How_to_add_a_cron_job_on_Windows/Scheduled_tasks_and_cron_jobs_on_Windows/index.html) – Trock Sep 29 '21 at 08:42
  • Spyder is an IDE. You don't have to run your Python program inside it. And if it is a production program, you shouldn't. You can run a Python program directly from the command line using `"C:\Program Files\Python39\python.exe" myprogram.py`. (Your location for the Python executable may be different. If you don't know what it is, do `where python` at the command line to find out.) – BoarGules Sep 29 '21 at 08:51
  • Starting it after reboot is one thing to consider, the other one is crashing which can be solved [by this](https://stackoverflow.com/a/69319924/5994041) for the script alone or utilizing a framework that comes with own auto-reloader/restarter. – Peter Badida Sep 29 '21 at 08:55

2 Answers2

0

You can add as a crontab rule what you want to run after reboot.

You can use this line in crontab by using crontab -e command, if you use server running on Linux platform.

@reboot <your_python_file_path>

ultdevchar
  • 132
  • 4
  • 1
    OP is running Windows. Proposing a `crontab` entry is not helpful. The closest thing Windows has is a scheduled task and they work quite differently. – BoarGules Sep 29 '21 at 08:48
  • I thought server is running on Linux platform. I'm editing my answer. – ultdevchar Sep 29 '21 at 08:51
0

Simply utilize shell:startup and add your Python location + path there (or create a Batch script for it):

myprogram.bat:

python.exe main.py

Alternatively, if you don't have python location in your PATH env variable, utilize the full path:

myprogram.bat:

"C:\Program Files\Python39\python.exe" main.py
Peter Badida
  • 11,310
  • 10
  • 44
  • 90