Report this

What is the reason for this report?

How do I keep my Python script running?

Posted on June 10, 2015

Hello, I recently purchased a droplet and have gotten everything up and running. I am using it for a Reddit bot I coded in Python. However, I noticed that in order to run the script I need to open Putty on my computer and keep it open. Is there anyway to run my Python script on my droplet without needing to use my computer.

Basically I am looking to run my python script 24/7 without use of my computer, which is what I thought the VPS was for, I just cannot figure it out. I am rather new to this so I apologize for my terminology or level of question.

Language: Python OS: Ubuntu



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.

@raghavyadav990 if that’s the case screen doesn’t work 24/7

Use screen or nohup.

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.