Page 1 of 1

Help with shutdown abort script

Posted: 14 Dec 2010 12:58
by BrentNewland
Sometimes, I update a program that wants to restart the computer, or a program wants to force a restart for some reason, and I want to leave the computer on.

I was able to make a script to run "shutdown -a" (abort) constantly, but I would like to take it one step further.

Code: Select all

:start
shutdown -a
goto start


When "shutdown -a" runs and there is no shutdown to abort, it displays:

Unable to abort the system shutdown because no shutdown was in progress.(1116)

When there is a shutdown to abort, and it aborts it, the program exits without displaying anything at the command prompt.

What I would like to do is have it hide all the output (@echo off), and print a message to the window if a shutdown was aborted.

Does anyone have some advice as to where I should start?

Re: Help with shutdown abort script

Posted: 14 Dec 2010 13:20
by aGerman
Try this:

Code: Select all

@echo off
:start
shutdown -a 2>nul &&echo shutdown was aborted
goto start

2>nul eliminates outputting of error messages by redirecting to NUL
&& associates the following command if the previous command was successful (errorlevel 0)

Regards
aGerman

Re: Help with shutdown abort script

Posted: 14 Dec 2010 13:24
by ChickenSoup
You can start with this:

Code: Select all

shutdown 1>nul 2>&1

All responses are sent to null (like linux).

Then maybe errorlevel variable can be used... not sure on this side. A FOR statement could probably handle this as well.

Re: Help with shutdown abort script

Posted: 14 Dec 2010 13:31
by ChickenSoup
Here is using a FOR statement to capture output to variable and then run a simple comparison:

Code: Select all

@echo off
:start
FOR /f "tokens=*" %%a IN ('shutdown -a') DO SET shutdownstatus=%%a
IF NOT "%shutdownstatus%"=="Unable to abort the system shutdown because no shutdown was in progress." echo shutdown was aborted
GOTO start

Re: Help with shutdown abort script

Posted: 14 Dec 2010 14:06
by aGerman
@ChickenSoup

On my computer (Win7) this doesn't work because the message is written to stdErr.

But this would process the message because it is redirected to stdOut:

Code: Select all

FOR /f "tokens=*" %%a IN ('shutdown -a 2^>^&1') DO SET shutdownstatus=%%a


But also this doesn't work for me because my error message isn't "Unable to abort the system shutdown because no shutdown was in progress.(1116)". I avoid language depending solutions. :wink:

Regards
aGerman

Re: Help with shutdown abort script

Posted: 14 Dec 2010 15:08
by BrentNewland
aGerman wrote:Try this:

Code: Select all

@echo off
:start
shutdown -a 2>nul &&echo shutdown was aborted
goto start

2>nul eliminates outputting of error messages by redirecting to NUL
&& associates the following command if the previous command was successful (errorlevel 0)

Regards
aGerman



I tried yours (since it was the first one) and it worked perfectly.

BTW, I double checked and the message with 1116 at the end is in Vista, XP is the same but without the 1116. I imagine trying to detect the specific message would be counter-productive (since it wouldn't be compatible with different languages), so I'm glad you had a solution to a problem I hadn't thought of.


Thanks guys for the quick responses. I noticed you guys don't have a donation link... you've helped me in the past, and I'd like to return the favor. Do you take donations, or is there anything else I can do?

Re: Help with shutdown abort script

Posted: 14 Dec 2010 15:26
by ChickenSoup
I was testing on Windows XP. All output for shutdown on XP appears to go to stdOut. So, you method would not work on XP. But I like that win 7 allows for a single line solution.

Re: Help with shutdown abort script

Posted: 14 Dec 2010 15:40
by aGerman
@BrentNewland
Your positive feedback is enough donation. Glad I could help.

@ChickenSoup
Did you check if shutdown returns an errorlevel on XP?

Regards
aGerman

Re: Help with shutdown abort script

Posted: 14 Dec 2010 15:59
by ChickenSoup
Regardless, it returns errorlevel 0 (zero).

Re: Help with shutdown abort script

Posted: 14 Dec 2010 16:10
by aGerman
Well, in this case I suppose there is no safe method to find out if it succeeded or not.

Regards
aGerman

Re: Help with shutdown abort script

Posted: 14 Dec 2010 16:30
by aGerman
[EDIT]
Wait. There is no message at all if it succeeded. This is a point we could exploit.
This line works for me and I think it should work on XP too:

Code: Select all

shutdown -a 2>&1 |findstr . >nul ||echo shutdown was aborted

[/EDIT)

Re: Help with shutdown abort script

Posted: 15 Dec 2010 07:22
by ChickenSoup
Very nice. That does work in XP as well.