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!
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:
Write Your Python Script: For example, let’s say you have your_script.py in /path/to/your_script.py.
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
/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:
multi-user system has been reached.systemd that it’s a simple, one-process service.systemd will restart it.systemd about the new service and start it:sudo systemctl daemon-reload
sudo systemctl start your_script.service
sudo systemctl enable your_script.service
Additional Commands:
sudo systemctl stop your_script.servicesudo systemctl restart your_script.servicejournalctl. 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.
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.