We have upgraded the community system as part of the upgrade a password reset is required for all users before login in.

RESOLVED: Launch a non-terminating Python script at boot

  • I have made a Python script that launches BaseHTTPServer http server, and never exits (unless the process is terminated, that is).

    I plan to put this in /etc/rc.local:

    cd /root/myproject
    python server.py

    However, before I do this, I'd like to be sure I won't cause something bad. A non-terminating script will keep running forever, and I better launch it the right way or I risk bricking the device (until factory reset anyway).

  • I've done the same thing and I simply added this line:

    (sleep 20;python /path/script_name.py)&

    The script works fine, except the BaseHTTPServer, that probably doesn't start (I've tried to increase the sleep time, but it still doesn't work). Otherwise if I run the script from onion terminal everything works perfectly.

  • This startup script in /etc/rc.local works fine, it launches the BaseHTTPServer and another backgroud python script.

    cd /root/myproject
    python server.py &
    sleep 20
    python monitor_script.py &
    exit 0

    Update: strangely, it killed off the server.py process for unknown reasons. Must study some log files to find out what happened.

  • @Pavils-Jurjans If you swap monitor_script.py with server.py would monitor_script.py get killed?

  • No, my tests show that the order is not important what happens is, that the server.py is visible in the process list until the first http request. That request immediately kills that process. If I run the server.py afterwards, doesn't matter with or without &, if works fine.

    Perhaps the BaseHTTPServer can not properly initialize something at the boot time, and that makes it fail at the first real request. I must implement some error logging in the server.py code to see what's going on.

  • @Boken-Lin in my python script I have a loop and everything works perfectly except for BaseHTTPServer; the internet connection is working, in the loop I have some functions that require an internet connection and these work nicely. As Pavils if I run the script directly from the terminal the BaseHTTPServer works

  • @Riccardo-Chiarini I think you are right, it probably requires some kind of networking service to be turned on before it can run. In this case, you should probably write an init.d script with start=40. This will tell the system to run this script only when network interfaces are up.

    @Pavils-Jurjans Looking forward to seeing your logs.

  • @Boken-Lin I've tried your advice, but it did't work. Anyhow I found a different solution; I've edited network script in etc/init.d/ and added this row in start_service():

    (killall -9 python; cd /script_path/; python script_name.py)

  • I have the exact same problem. I think it might have something to do with logging. I can start the script from the shell without problems and all is ok, however if I logout from the shell then the server stops working. Any ideas?

    Also, putting the server into the network script does nothing for me but make me require a factory reset!

  • @Boken-Lin Unfortunately I never succeeded in configuring my Omega project so that it launches a python Simple HTTP Server at startup without any user intervention. The server script is launched alright, but at first http request it dies with error it does not seem to be able to recover from.

    I use python's Simple HTTP Server (BaseHTTPServer.HTTPServer), and run my script at startup by adding the following three lines to /etc/rc.local:

     sleep 20
     cd /root/myProjectRoot
     python server.py &

    When I log on remotely via ssh, I can check if the python process running by $ ps | grep python and I can see that my server script is running. But once I make the http request, my script immediately fails. I hooked up logging to log file to catch more info about the error, and I've got this:

    [Errno 32] Broken pipe

    I am not sure how that can be fixed. If I run the server.py script from ssh session, it behaves normally, but it's not good enough because it stops when I close the ssh session. I need it to run completely in background.

  • Dammit, the problem was from totally different department!

    The actual error was caused by the Simple HTTP Server trying to output its log where there was no place where to output, as the script was launched by system at startup.

    I have silenced the console logging by implementing log_message method in my HTTP handler. Tada! Everything works now.

    class MyHandler(BaseHTTPServer.BaseHTTPRequestHandler):
        def log_message(self, format, *args):

    A somewhat brutal way to silence logging, so I'll replace it by logging to file, though.

Log in to reply

Looks like your connection to Community was lost, please wait while we try to reconnect.