Report this

What is the reason for this report?

how to keep running python script all the time

Posted on August 11, 2018

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



This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

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.

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.

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.

Heya,

Setting up a Python script to run as a system service using systemd is a common way to ensure it’s always running. Here’s a step-by-step guide for setting up a Python script as a systemd service:

  1. Write Your Python Script: For example, let’s say you have your_script.py in /path/to/your_script.py.

  2. Ensure your script is executable: Make your Python script executable by adding a shebang line at the top and changing its permissions:

#!/usr/bin/env python3
chmod +x /path/to/your_script.py
  1. Create a Systemd Service File: Create a new service file in /etc/systemd/system/, e.g., your_script.service.
sudo nano /etc/systemd/system/your_script.service

Add the following content to the service file:

[Unit]
Description=My Python Script Service
After=multi-user.target

[Service]
Type=simple
ExecStart=/path/to/your_script.py
Restart=on-abort

[Install]
WantedBy=multi-user.target

Explanation:

  • Description: A short description of your service.
  • After: This service will start after the multi-user system has been reached.
  • Type: Tells systemd that it’s a simple, one-process service.
  • ExecStart: Path to the executable Python script.
  • Restart: If the script aborts unexpectedly, systemd will restart it.
  • WantedBy: Identifies the target that the service should be started under.
  1. Reload Systemd and Start Your Service: Once the service file is saved and closed, update systemd about the new service and start it:
sudo systemctl daemon-reload
sudo systemctl start your_script.service
  1. Enable Your Service to Start on Boot: If you want your Python script to start automatically on system boot:
sudo systemctl enable your_script.service
  1. Additional Commands:

    • Stop the service: sudo systemctl stop your_script.service
    • Restart the service: sudo systemctl restart your_script.service
    • View logs: Use journalctl. For example: journalctl -u your_script.service.

Remember that when you modify the service file or the script itself, you should usually reload or restart the service to apply the changes.

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.