1

So, here is my little problem:

I have a small python program and have to run it 24/7 with internet access. So using my laptop is not really a solution. But I can use a local server. My program is saved on the server. Is there a way to start the program headless on the server, so it can run for a long period of time?

Thanks

Hyperion
  • 121
  • 1
  • 3
  • You mean `setsid python myprog.py`? – kabanus Aug 30 '18 at 08:52
  • When I run this command from my laptop with a program located on the server, will it still run on the server even if I shut the notebook down? – Hyperion Aug 30 '18 at 08:57
  • Yes. Use `man setsid` to read about it. – kabanus Aug 30 '18 at 08:57
  • Yes, there is a way to run it either all the time (a service or daemon) or have it run at certain times (scheduled). The way to do it will depend on the server. Is it linux? Windows? Depending on what you want, and your server, search here for 'Running a scheduled job on linux server' as an example. – T Burgis Aug 30 '18 at 08:59
  • 1
    `screen` is also a very useful program. Basically lets you start a terminal, disconnect (but any running programs keep going) and then reconnect and resume at a later stage. – Martin Cook Aug 30 '18 at 09:00
  • @TBurgis It should run all the time. The server is a Windows server. – Hyperion Aug 30 '18 at 09:07
  • I'm afraid I'm not a windows person so can't help with the specifics but try searching here for 'How to run a service on a windows server' – T Burgis Aug 30 '18 at 09:46
  • Possible duplicate of [How do you run a Python script as a service in Windows?](https://stackoverflow.com/questions/32404/how-do-you-run-a-python-script-as-a-service-in-windows) – Azsgy Aug 30 '18 at 09:56

1 Answers1

4

This post assumes you are using linux. If this is not the case, I will still keep this answer around for anyone else. The general Principles will apply to any OS regardless.

While setsid is one way to put a program into the background, it is usually not what you want for a number of reasons:

  • If you ssh into the server again, there is no easy way to see the output of the program. Any output will not be kept.
  • If the program crashes, it won't be restarted.
  • If the server reboots, it won't be started.
  • There is no easy way to see the status, stop or restart it.

One slightly better method would be to use tmux (or the older screen). These can be used to detach a process, but still have access to it's output. (see this answer).

However, if you want to do things correctly, you should use a process manager/supervisor, such as systemd or supervisord.

For systemd, you can create the following file: /etc/systemd/system/yourprogramname.service

Inside it, place the following text:

[Unit]
Description=YourDescription

[Service]
ExecStart=/usr/bin/python3 /your/full/script/location.py
Restart=always

[Install]
WantedBy=multi-user.target

(These files support a number of additional options, you can view them at: http://0pointer.de/public/systemd-man/systemd.service.html)

Then reload the units with systemctl daemon-reload and enable your unit at boot with systemctl enable yourprogramname.service.

You can then:

  • Start it: systemctl start yourprogramname
  • Retart it: systemctl restart yourprogramname
  • Stop it: systemctl stop yourprogramname
  • Get the status: systemctl status yourprogramname
  • View the full logs: journalctl -u yourprogramname

(these commands all require sudo)

Azsgy
  • 3,139
  • 2
  • 29
  • 40
  • Do you know about something similar for Windows? I have no idea in which directory I have to save the file on a Windows server. – Hyperion Aug 30 '18 at 09:27
  • there are some interesting points. When I have time, I will try it and see what could be useful for me. – Hyperion Aug 30 '18 at 10:01