Whats the "cleanest" way to stop OctoPrint in a virtual environment?

Title says it all really...

If I want to stop OctoPrint from the command line in my python 3 environment so I can hop across to a python 2 environment is there a better way than just killing the process?

Ta!

Andy B.

This depends on how you are running it. If you ran it as a service, you can stop the service. But AFAIK, stopping the service also just kills the process. I don't think there's anything wrong with killing the process per-see, just as long as you do it in a friendly way (ie: SIGTERM instead of SIGKILL).

SIGTERM (which is sent using a normal kill command) tells the application to terminate itself. SIGKILL (kill -9) actively stops the process, and may leave some resources (opened files) in an unclean state.

Or at least, that's how I think it works.

1 Like

Thanks for that, so my alias of:

kill `ps|awk '/octoprint/ {print $1}'`;deactivate

Should do the trick...

1 Like

I suppose you'd have to test that. I'd assume that it would need to be prepended by sudo.

No, it works as is - sudo isn't required because the same user that runs "octoprint serve" kills the process, the only thing I've changed is dropping the "-9" on the "kill"

1 Like

I will note that dropping the SIGKILL flag doesn't 100% result in shutting down OctoPrint, say, when it's in a fairly crashed state. (I had a plugin which spawned an RFID thread that used to do this from what I remember.)

And in another case, someone had done a threading.Thread() with a daemon = True and upon an exception crash it seemed to leave things in an ugly state.

Not sure what the best practice on this is for a kiosk-style app but maybe...

  • try the SIGTERM version
  • wait a moment
  • ps
    • conditionally SIGKILL

Being as this is only for my development environment (WSL on Windows 10) I don't want to "over engineer" things - its only a second or two to reload... But after spending a while messing with single and double quotes and backslashes (awk in an alias is a real pain) the penny dropped and I came up with this:

   alias octokill='pkill octoprint;deactivate'

Which kills the process and deactivates the virtual environment in one hit.

Should have remembered by own motto - Engineers derive it from first principles - Doh!

1 Like

I wrote this two days ago for my own use, for what it's worth (completely unrelated to your issue):

~/scripts/killhmi

#!/bin/bash

for pid in $(ps -ax -o pid,command | grep mprint-hmi | grep python | awk '{print $1}'); do kill -9 $pid; done
1 Like