By scholastik
Hi there, I’m new to this, but I have small basic experience with my Raspberry Pi and Debian.
What I want to reach: I want a Debian system, where I can run a Python 3.4 script, which includes an endless loop (so in the script is a “while True” condition). It makes some API calls to several websites, calculates some numbers and save the result in a .txt file and also prints some of the results. This should be done 24hours a day nonestop. Of course it should be only accessed by me, regardless where I am.
What I already did:
First Question about second SSH: But now in Step Four, there is something written about SSH again. Why? I already enabled SSH to access my droplet. Why another SSH ? My thought is: “no one except me should be able to access my droplet, so why a second security measure? The first should be save enough?” Or is there another reason for this second SSH?
Second Question: If access my droplet via Putty and start the script, it will run. But if I close Putty, I think also my script is shut down. But I want the script to run 24h a day. At my Raspberry Pi I have a desktop environment with Remote access from my PC (xrdp). I just start the terminal and start the script in it. When closing the remote access, the script is not shut down. Now I can start the remote connection from time to time, to check the prints from my script, if it is still running without problems. This is what I also want for my droplet now. The Desktop is not necessary I think (only 512 ram), but I want a way, to see the prints from the script and to check from time to time, if everything is still running.
Third Question: Is there anything else I should do? Maybe a VPN (what is a VPN?) ?
Thank You :)
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!
For your first question, if you’ve already set up SSH access following the first tutorial then no need to do it again while following the second.
For your second question: Yes, closing PuTTY will stop the script if you just started it by running it directly as it logs out your user and ends it session. There are a number of ways to ensure that a long running process persists after logging out. You could use a tool like screen to keep your session open after closing PuTTY. Or you could set up your script to be run like a service.
Debian 8 uses systemd for it’s init system. To run your script as a service, you can create a systemd unit file: /etc/systemd/system/myscript.service
[Unit]
Description=My script
[Service]
Type=simple
ExecStart=/usr/bin/python /path/to/myscript.py
[Install]
WantedBy=multi-user.target
You can then start and stop your script like you would any other system service:
root@debian:~# sudo service myscript start
root@debian:~# sudo service myscript status
● myscript.service - My script
Loaded: loaded (/etc/systemd/system/myscript.service; disabled)
Active: active (running) since Fri 2015-10-30 13:33:31 EDT; 1s ago
Main PID: 1220 (python)
CGroup: /system.slice/myscript.service
└─1220 /usr/bin/python /root/myscript.py
You can find some more in-depth info on working with systemd here:
Heya,
The second SSH configuration you’re referring to in Step Four is not about setting up a new SSH connection but rather hardening the existing SSH configuration to make it more secure. The changes suggested in the guide include:
Even if you’re the only one accessing your server, it’s essential to follow these best practices to avoid potential unauthorized access.
I’ll recommend using Systemd. Using a service manager like systemd is a more modern and robust approach, especially for long-term and production setups. Here’s how you can run your Python script as a service:
Creating a Service File:
First, you’ll need to create a systemd service file:
sudo nano /etc/systemd/system/myscript.service
[Unit]
Description=My Python Script Service
After=network.target
[Service]
Type=simple
User=username
WorkingDirectory=/path/to/your/script/directory
ExecStart=/usr/bin/python3 /path/to/your/script.py
Restart=always
[Install]
WantedBy=multi-user.target
Replace username with your user’s name and adjust the paths (/path/to/your/script/directory and /path/to/your/script.py) to point to your script.
Starting and Enabling the Service:
Once the service file has been created:
sudo systemctl daemon-reload
sudo systemctl start myscript.service
sudo systemctl enable myscript.service
This approach ensures that your script starts automatically if the server reboots and can be managed using standard systemctl commands. It’s more consistent and integrates well with the system’s standard service management tools.
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.