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
Pavils Jurjans last edited by
No, my tests show that the order is not important what happens is, that the
server.pyis visible in the process list until the first http request. That request immediately kills that process. If I run the
server.pyafterwards, doesn't matter with or without
&, if works fine.
BaseHTTPServercan 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.pycode 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
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
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 pythonand 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.pyscript 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.
Pavils Jurjans last edited by
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_messagemethod in my HTTP handler. Tada! Everything works now.
class MyHandler(BaseHTTPServer.BaseHTTPRequestHandler): def log_message(self, format, *args): return
A somewhat brutal way to silence logging, so I'll replace it by logging to file, though.