0

I'm doing a Python webserver (e.g. using Flask or Bottle, etc.), and I would like to start it, and be able to close the SSH terminal and let it running.

Which way would be the pythonic recommended way to do it?

  1. Create a daemon, and use python myapp.py start, python myapp.py stop. However the python-daemon module has nearly no doc, doesn't support triggering an action just before exiting (I added a few lines to it to support it), so it's a bit a hack of an undocumented/not-really-maintained module, so even if it works, I'm not 100% happy with it.

  2. Use nohup python myapp.py &, but then the drawback is that you have to ps aux |grep py, then find the relevant PID and kill 12345 to stop it. Here again, it doesn't allow to do actions before stopping (e.g. save the database to disk, etc.), so it's not a very nice solution.

  3. screen -dmS myapp python myapp.py to start, then you can log out from SSH terminal. Then later you can connect to it again with screen -r myapp, and then CTRL+C can stop it (provided KeyboardInterrupt is well handled). That's what I would use currently. But I'm not sure if using screen to let a server run forever is a good idea (what happens if the logging is really verybose? also is there a risk that introducing the screen layer would make it bloated?)

  4. Another cleaner solution? I hope there's a cleaner solution than 1, 2, 3 that all have drawbacks.

Note: I would like to avoid installing new managers (upstart or supervisor), and do it with the least number of tools possible, to avoid new layers of complexity.

Basj
  • 41,386
  • 99
  • 383
  • 673
  • 2
    Write a service that is managed by systemd (or similar, eg upstart or supervisor). – Daniel Roseman Feb 23 '18 at 16:20
  • @DanielRoseman I would like to avoid installing new managers (upstart or supervisor), and do it with the least number of tools possible, how would you do it? – Basj Feb 23 '18 at 16:23
  • You will probably want to use an application server like GUnicorn as well, so use systemd or supervisor to manage the GUnicorn process. – Sven Marnach Feb 23 '18 at 16:23
  • 2
    At least one of those things will already be installed, since it will be managing the system itself. If you have a recent distribution of Linux, it will probably be systemd. – Daniel Roseman Feb 23 '18 at 16:24
  • what flavor of linux are you using? – Jim Factor Feb 23 '18 at 18:21
  • @JimFactor Debian 8, I've never used systemd for such things because I wanted to do it with Python and simple tools only, and avoid having to create files outside of the app's folder (i.e. creating a systemd service involves creating files outside of the app's path, and then when you deploy it on another system, you need to remember those things, etc. and I feared it would not be as portable as other solutions). – Basj Feb 23 '18 at 22:50

0 Answers0