Discussion:
Script to automatically restart service after system start
(too old to reply)
Markus Wetzel
2010-01-18 11:53:06 UTC
Permalink
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
Pegasus [MVP]
2010-01-18 12:47:55 UTC
Permalink
Post by Markus Wetzel
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
Let's have a look at your script!
Markus Wetzel
2010-01-18 16:09:10 UTC
Permalink
Post by Pegasus [MVP]
Let's have a look at your script!
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?)
Pegasus [MVP]
2010-01-18 16:38:39 UTC
Permalink
Post by Markus Wetzel
Post by Pegasus [MVP]
Let's have a look at your script!
@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?)
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.
Markus Wetzel
2010-01-18 16:46:51 UTC
Permalink
Thank you, I will give it a try and report back to you if it still does
not work.
Markus Wetzel
2010-01-19 01:32:00 UTC
Permalink
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
Pegasus [MVP]
2010-01-19 07:53:00 UTC
Permalink
Post by Markus Wetzel
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
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.

Loading...