Script to automatically restart service after system start

Discussion in 'Scripting' started by Markus Wetzel, Jan 18, 2010.

  1. Hi,

    after booting my w2k3 server I have to restart a service (OpenVPN) in
    order to have it working correctly. I don't know if this is an issue
    with the OpenVPN config but for now I would like to have a script that
    automatically restarts the OpenVPN service ~5 minutes after the system
    startup.

    I wrote a little batch file (c:\temp\openvpnrestart.bat) with sleep and
    net stop/start commands and created a new scheduled task that should
    start while starting the system (with user "Administrator"). But it
    seems that the script does not work properly while beeing started by a
    scheduled task. Starting it manually works perfectly.

    How am I able to automatically restart a service ~5 minutes after system
    and service startup?

    Kind regards,
    Markus
     
    Markus Wetzel, Jan 18, 2010
    #1
    1. Advertisements

  2. Let's have a look at your script!
     
    Pegasus [MVP], Jan 18, 2010
    #2
    1. Advertisements

  3. Am 18.01.2010 13:47, schrieb Pegasus [MVP]:
    No problem:

    @echo off
    sleep 300
    echo "OpenVPN Service beenden"
    net stop "OpenVPN Service"
    sleep 10
    echo "OpenVPN Service starten"
    net start "OpenVPN Service"

    I have to correct myself: The script is in the directory C:\Programme
    (German windows) and is called "OpenVPN restart.bat"

    It works great when I start it by clicking on it. (For testing purposes
    with sleep 5. Could the sleep 300 command be a problem? Maybe script
    timeout or something like that?)
     
    Markus Wetzel, Jan 18, 2010
    #3
  4. When you want your batch files to be robust then you must fully qualify all
    executables that are not native to Windows. Sleep.exe is not native to
    Windows and it could be stored anywhere on your disk. You can actually
    replace it with ping.exe, which is a native Windows program. You should also
    add some diagnostics to your batch file so that you can see what's going on.
    Try this version:

    @echo off
    echo Batchdatei gestartet am %date% um %time:~0, 5% >> c:\test.txt
    ping localhost -n 300 > nul
    echo "OpenVPN Service beenden" >> c:\test.txt
    net stop "OpenVPN Service" 1>> c:\test.txt 2>>&1
    echo 10 Sekunden warten >> c:\test.txt
    ping localhost -n 10 > nul
    echo "OpenVPN Service starten" >> c:\test.txt
    net start "OpenVPN Service" 1>> c:\test.txt 2>>&1
    echo Batchdatei beendet um %time:~0, 5% >> c:\test.txt
    echo\ >> c:\test.txt

    If the batch file still fails to run then you can examine the full runtime
    details in c:\test.txt.
     
    Pegasus [MVP], Jan 18, 2010
    #4
  5. Am 18.01.2010 17:38, schrieb Pegasus [MVP]:
    Thank you, I will give it a try and report back to you if it still does
    not work.
     
    Markus Wetzel, Jan 18, 2010
    #5
  6. I booted the machine several times but after each restart the OpenVPN
    service is not working correctly until I do a manual restart of the
    service. According to the logfile the batch file worss properly but it
    seems that it has no real effect on the OpenVPN issue. So I still have
    to do a manual restart of the service.

    So there has to be another problem but sadly this will have nothing to
    do with m.p.w.server.scripting
     
    Markus Wetzel, Jan 19, 2010
    #6
  7. Thanks for the feedback. I am not familiar with the OpenVPN service and I
    therefore do not know what the issue is that you observe. Presumably it
    allows you to set up a VPN with some other machine. Could it be that this
    VPN is context-sensitive? Take, for example, the simple command

    net use Q: \\Server\Share

    When executed as part of the startup script then it will work perfectly
    well, yet drive Q: will be invisible to the user who subsequently logs on.
    Why? Because it was executed within the context of the startup environment
    and NOT within the context of the user's logon session.
     
    Pegasus [MVP], Jan 19, 2010
    #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.