how to keep running python script all the time

August 11, 2018 563 views
Python Ubuntu 18.04

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

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.

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.

Have another answer? Share your knowledge.