How do I run a batch file when a server reboots?

Discussion in 'Scripting' started by Marten, Aug 6, 2008.

  1. Marten

    Marten Guest

    I have several servers that I have scheduled to reboot Sundays at 3
    am. I'd like them to leave a mark that they did reboot as it seems
    some don't always invoke the SHUTDOWN command.

    I'd like to run a simple batch file that contains the line:

    echo rebooted > \\primarycontoller\bootlogs$\%computername%.txt

    The time stamp of the file will be enough to tell me if SHUTDOWN ran
    properly.

    My question is where do I put the call to this batch file so it gets
    executed during the reboot?

    Marten
     
    Marten, Aug 6, 2008
    #1
    1. Advertisements

  2. It seems you don't want to leave the mark for the ***boot-up***
    event but rather for the ***shut-down*** event. Use the Task
    Scheduler to invoke a batch file with two commands inside:
    - Your "echo" command (probably supplemented with %date%
    and %time%)
    - shutdown.exe
    You should also use the >> redirector so that subsequent
    shutdown events do not overwrite the previous ones.
     
    Pegasus \(MVP\), Aug 6, 2008
    #2
    1. Advertisements

  3. Marten

    Marten Guest

    That's a good method, thank you. I forgot to think of calling a batch
    file from the scheduler.

    My only concern is getting a false positive. By that I mean the first
    line (per your suggestion) runs and creates the mark file, but for
    what ever reason the Shutdown fails and the server doesn't reboot.
    Which has happened. My thinking was if I could put the call to my
    batch file in some equivalent of AUTOEXEC.BAT, I could be sure it was
    processed during the power up.

    The >> redirector is a consideration. I'm mostly concerned with thee
    last reboot. But having a bit of history isn't bad. As long as I
    remember to trim the file periodically.

    for the record, the Shutdown command used is:
    C:\WINDOWS\system32\shutdown.exe /r /d p:4:1

    Marten
     
    Marten, Aug 6, 2008
    #3
  4. I'm quite aware of the syntax for the shutdown command . . .

    Running a command at boot time (the equivalent of Autoexec.bat)
    is dead easy: Use the Task Scheduler to run it at boot time.
    However - how will you distinguish between a boot after a
    scheduled shutdown and a boot caused by a crash?

    About trimming the log file: If you schedule a reboot once every
    week then you accumulate some 2 kBytes per annum. I would
    not call this a huge amount of disk space - it's puny, so why worry?
     
    Pegasus \(MVP\), Aug 6, 2008
    #4
  5. Marten

    Marten Guest

    My apologies for a grammatical error. I should have written "the
    Shutdown command as I am using it is:" I added it in case you saw
    something that was incorrect or if someone else reading this thread
    wanted to know what I had done.

    I haven't worked much with the scheduler. I wasn't aware there was an
    at boot option. Thank you for pointing it out to me. As for
    distinguishing, since I know the time of the scheduled reboot, I'd
    expect anything outside that time frame would be questionable.

    The >> redirect is a consideration. I was originally only concerned
    with the last reboot.

    Thank you for the information. It helps me out.

    Marten
     
    Marten, Aug 6, 2008
    #5
  6. Thanks for the feedack.
     
    Pegasus \(MVP\), Aug 6, 2008
    #6
  7. Marten

    Greg Stigers Guest

    Aren't the events in the event log enough of a mark?
    ______
    Greg Stigers, MCSE
    remember to vote for the answers you like
     
    Greg Stigers, Aug 8, 2008
    #7
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.