similar question has been asked at
https://www.digitalocean.com/community/questions/how-do-i-keep-my-python-script-running
but the solution mentioned there is to use screen. its not the solution. it doesn’t serve the purpose of running a pything script without logging in everyday. please help

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

×
2 answers

Hi, screen works all the time no need to login, you can use nohup too nohup python script.py &.

Also you can create a service for it if you are running Ubuntu 15.04 or newer you can create a simple systemd service file to run it as follows

[Unit]
Description=A test unit

[Service]
ExecStart=<full_path_to_script>
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=hello

Put these lines in a file called hello.service in /etc/systemd/system.

Make sure your script file is executable by running sudo chmod +x <full_path_to_script>

Now reload systemd by running sudo systemctl daemon-reload, start the service sudo systemctl start hello.

Now you can check if the service is running or not using sudo systemctl status hello and stop it using sudo systemctl stop hello

I hope this helps you.

  • Thanks! This helped, however when I run the service, the script encounters errors like no module named MODULE_NAME. I suspected environment issues and I activate the environment first in the script but I still get the error. How can I overcome this?

Depending on what your script is supposed to do, instead of running it on your computer, you could run it on something like a Raspberry Pi.

You could also run it online. There are various services that allow you to do that relatively easily. However, I don’t think many of them will allow you to run a script 24/7 (especially for free) and it is quite likely that even if they did, your script would stop running every time there was a maintenance. You can circumvent this by getting your own server.

Submit an Answer