Help with shutdown abort script

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
BrentNewland
Posts: 13
Joined: 17 May 2010 15:20

Help with shutdown abort script

#1 Post by BrentNewland » 14 Dec 2010 12:58

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?

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Help with shutdown abort script

#2 Post by aGerman » 14 Dec 2010 13:20

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

ChickenSoup
Posts: 79
Joined: 13 Dec 2010 10:32

Re: Help with shutdown abort script

#3 Post by ChickenSoup » 14 Dec 2010 13:24

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.

ChickenSoup
Posts: 79
Joined: 13 Dec 2010 10:32

Re: Help with shutdown abort script

#4 Post by ChickenSoup » 14 Dec 2010 13:31

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

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Help with shutdown abort script

#5 Post by aGerman » 14 Dec 2010 14:06

@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

BrentNewland
Posts: 13
Joined: 17 May 2010 15:20

Re: Help with shutdown abort script

#6 Post by BrentNewland » 14 Dec 2010 15:08

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?

ChickenSoup
Posts: 79
Joined: 13 Dec 2010 10:32

Re: Help with shutdown abort script

#7 Post by ChickenSoup » 14 Dec 2010 15:26

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.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Help with shutdown abort script

#8 Post by aGerman » 14 Dec 2010 15:40

@BrentNewland
Your positive feedback is enough donation. Glad I could help.

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

Regards
aGerman

ChickenSoup
Posts: 79
Joined: 13 Dec 2010 10:32

Re: Help with shutdown abort script

#9 Post by ChickenSoup » 14 Dec 2010 15:59

Regardless, it returns errorlevel 0 (zero).

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Help with shutdown abort script

#10 Post by aGerman » 14 Dec 2010 16:10

Well, in this case I suppose there is no safe method to find out if it succeeded or not.

Regards
aGerman

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Help with shutdown abort script

#11 Post by aGerman » 14 Dec 2010 16:30

[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)

ChickenSoup
Posts: 79
Joined: 13 Dec 2010 10:32

Re: Help with shutdown abort script

#12 Post by ChickenSoup » 15 Dec 2010 07:22

Very nice. That does work in XP as well.

Post Reply