Second SSH necessary? How to keep the droplet active without an active session?

Posted October 30, 2015 4.9k views

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 :)

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.

Submit an Answer
1 answer

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

Description=My script

ExecStart=/usr/bin/python /path/to/


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/

You can find some more in-depth info on working with systemd here:

by Justin Ellingwood
In this article, we will discuss how to install and use "screen", a tool that allows you to manage multiple shell sessions inside a single terminal. The screen program is very useful for multi-tasking in an ssh environment, due to its ability to resume terminal sessions and perform multiple-concurrent tasks.
  • Thank you very much :)

    The ExecStart in the unit file, how does it work with python 3 ? I think if I type “Python” into the command line, it will use Python 2. So I have to start it with “Python3”. The path to my script will be “/home/scholastik/” , or should I change this path? Should it be root? Should be in the python folder?

    After starting the service (my script), shutting down putty and start putty again, how do I see the prints from my script? Will they be shown with the command “sudo service myscript status” ?

    • If the script should be run as a specific user, add the line User=scholastik under the [Service] section.

      As for the paths, you’ll want to use the full path to the file and Python 3, so putting that together:

      ExecStart=/usr/bin/python3 /home/scholastik/

      Checking the output really depends on your script. If it’s writing to a specific file, then you could use tail -f to follow it. For something that you depend on logging specific output over time, it’s probably a good idea to add a timestamp to the output so you can have a better idea for when a specific line was written.

      • thank you. I added the User=scholastik.

        The script uses a txt file for configuration. So it opens the txt file and gets some values. I noticed, that when I call “service myscript status” (aftrer starting it first of course) I get an error report, that the configuration file does not exist (it is in /home/scholastik/). After placing the file directly into the root “/” folder, it is found.

        What to change, that the txt file is found in the home\scholastik folder?
        (Same problem for the logfile. In the root directory it has no permission to create the log file)

        The output topic:
        The changes in the log file can be easily accessed by WinSCP, so that is no problem. I mean the “print("hello”)“ commands in my python file. Will they be shown in the putty terminal after calling "service myscript status” ? Or how do I see live all prints ?

        when calling the status, I see the info “Hint: Some lines were ellipsized, use -l to show in full.” What does it mean? where to puit the -l to see it in full?