Octoprint Windows Restart

Hi all,

First time posting, so hopefully this is in the right place!

Wasn't sure where to post this, but thought it might be useful to someone! I've recently moved from my underpowered RPi B+ to an intel nuc running windows instead, but couldn't find an easy way to issue a "Reset command" soooo... incase anyone in the same situ as I was, please see my solution below;

Create a .bat file inside of the octoprint server folder - mine's located at "C:\OctoPrint\venv\Scripts"

I called the file "Reset.bat" open it and enter the code below;

@if octoprint.==. (goto error) ELSE (goto restart)

:restart
@taskkill /f /im octoprint.exe >nul
@timeout /t 3 /nobreak >nul
@start octoprint.exe serve >nul
@echo restart complete
@goto exit

:error
@echo Oops... something went wrong!
@goto exit

:exit

Then hit save.

Then on octoprint settings, go to "Server" and paste the location of the reset file into the restart octoprint box - mine was " "C:\OctoPrint\venv\Scripts\Restart.bat" " (I'm not sure if the " " around the command is needed, but it seems to work with it there... sooo i've left it.)

Sidenote, my script may be wrong in places, i'm not versed in bat script, just something that I found on the internet and adapted for this. Any potential fixes or something similar, please post below and i'll update the code accodingly. :slight_smile:

Hope this helps someone!

3 Likes

Why all the @ symbols?

I think I'd just do an @echo off at the beginning if you don't want command echoing.

Secondly, the ELSE (goto restart) and the parentheses to its left are unnecessary. Program flow should just go to the next line and continue.

@echo off
cls
if octoprint.==. goto error
taskkill /f /im octoprint.exe   > nul
timeout /t 3 /nobreak           > nul
start octoprint.exe serve       > nul
echo restart complete
goto exit

:error
echo Oops... something went wrong!

:exit
2 Likes

Does this work if you're running multiple instances of Octoprint? I tried it and it keeps closing my second instance and opening up my first instance, which is completely unuseful.

The script was not written with multiple instances in mind; it first kills (stops) all the octoprint.exe processes (which includes both your instance), and then starts up just one of them.

What command do you use to start up your second instance?

Well, I modified the start command as seen below:

@echo off
cls
if octoprint.==. goto error
taskkill /f /im octoprint.exe   > nul
timeout /t 3 /nobreak           > nul
start octoprint.exe serve --basedir "C:\Users\natha\AppData\Roaming\octoprint2" --host=192.168.1.22 --port=5001      > nul
echo restart complete
goto exit

:error
echo Oops... something went wrong!

:exit

This way you can make a "restart" file for each instance. Unfortunately, I have been unable, so far, to figure out how to just stop 1 instance of the octoprint.exe, so right now restarting 1 instance will kill both and then only restart the one, which isn't too useful, though I'm halfway there. I tried adding the same line to the 'taskill' command, but it just gave me an error and then would open up a second instance of the instance I was trying to restart.

As a quick and dirty workaround, you could try to make a copy of the octoprint.exe file named octoprint2.exe. Then start the second instance with octoprint2.exe instead of octoprint.exe. Now you can make two scripts; one killing and restarting octoprint.exe, and one killing and restarting octoprint2.exe.

That did it. I had to redirect the desktop shortcut I had to the new octoprint2.exe, but not I can restart each individual instance, just need to add a 2,3,4,5... or whatever individual identifier you want, but it has to be unique, to the octoprint.exe (for example, octoprint2.exe or octoprintb.exe).

So, for me, to restart my first instance I use this:

@echo off
cls
if octoprint.==. goto error
taskkill /f /im octoprint.exe   > nul
timeout /t 3 /nobreak           > nul
start octoprint.exe serve --basedir "C:\Users\natha\AppData\Roaming\octoprint" --host=192.168.1.22 --port=5000      > nul
echo restart complete
goto exit

:error
echo Oops... something went wrong!

:exit

and for my second instance

@echo off
cls
if octoprint.==. goto error
taskkill /f /im octoprint2.exe   > nul
timeout /t 3 /nobreak           > nul
start octoprint2.exe serve --basedir "C:\Users\natha\AppData\Roaming\octoprint2" --host=192.168.1.22 --port=5001      > nul
echo restart complete
goto exit

:error
echo Oops... something went wrong!

:exit

These will obviously need to be 2 different .bat files that you'd need to put somewhere and then put the file location in Octoprint.

Update: So this is still giving me some issues. While it successfully closes the correct instance and reloads the "correct" instance, it not messes with the settings, as my first instance now steals the settings of the second instance. I will continue to try and debug

Alright, so after some more tweaking and troubleshooting, I think I found a fix. Probably not the way it "should be done", but it works and it's what I've been able to figure out at this point (This is my first dive into anything like this with Command Line coding and Batch Files. I'll continue to try and improve it, but I have a few other things to work on before I attempt to "optimize" it)

So, here's what I did. Instead of launching the octoprint.exe or octoprint2.exe file, with it's base directory, host, and port defined, I instead redirected it to the shortcut link I had already setup for each of my instances on my desktop. Again, probably not the cleanest or most optimized setup, but it works and each instance loads correctly with the correct settings loading (finally).

Restart1.bat:

@echo off
cls
if octoprint.==. goto error
taskkill /f /im octoprint.exe   > nul
timeout /t 3 /nobreak           > nul
start C:\Users\natha\Desktop\octoprint1.lnk
echo restart complete
goto exit

:error
echo Oops... something went wrong!

:exit

Restart2.bat:

@echo off
cls
if octoprint.==. goto error
taskkill /f /im octoprint2.exe   > nul
timeout /t 3 /nobreak           > nul
start C:\Users\natha\Desktop\octoprint2.lnk
echo restart complete
goto exit

:error
echo Oops... something went wrong!

:exit

You can obviously rename your desktop shortcuts whatever you want. just make sure that the "restart" batch file is updated to match.

1 Like

The old script was working for me just fine until the 1.4.1 (.2) upgrade. Now it does not stop the original instance and starts a new instance of octoprint.

I'm parsing thru task manager to see if perhaps the application name changed somehow.

This worked great for my Windows 10 OctoPrint server.

1 Like

Thank you! Worked perfectly. I suggest also adding the octoprint path into system variables in Windows if you haven't already done so (eg C:\Octoprint\venv\Scripts)