Sign in to follow this  
Followers 0
somebody

Installing and running as Windows Service

4 posts in this topic

I have used code posted here and here as guides to installing and running my AutoIt script as a service, but it is still not properly starting. After starting the service through Windows' "Services" manager, my script attempts to start but then dies after 30 seconds with an error of:

Error 1053: The service did not respond to the start or control request in a timely fashion.

Do I need some sort of return value passed from the AutoIt script to Windows so Windows will know the service started correctly?

Here is my code. It will print "hello world" 30 times to the log file until it dies. Any help is greatly appreciated, thanks!

CODE
#NoTrayIcon

#include <process.au3>

Dim $servicename = "HelloWorld"

Dim $log = "c:\temp\log.txt"

Dim $is_service_installed = 0

Dim $strComputer = "."

Dim $OWN_PROCESS = 16

Dim $NORMAL_ERROR_CONTROL = 1

Dim $INTERACTIVE = true

Check_Service_Installation()

If( $is_service_installed == 0 ) Then

Install_Service()

Else

WriteLog( $log, "Service already installed, skipping service installation" )

While 1

sleep(1000)

WriteLog( $log, "Hello World" )

WEnd

EndIf

Func Install_Service()

Dim $objWMIService, $objService, $objServices, $colListOfServices, $strService

$objWMIService = ObjGet("winmgmts:" & "{impersonationLevel=impersonate}!\\" & $strComputer & "\root\cimv2")

$objService = $objWMIService.Get("Win32_BaseService")

$errReturn = $objService.Create( $servicename, "Hello World!", @AutoItExe, $OWN_PROCESS, $NORMAL_ERROR_CONTROL, "Automatic", $INTERACTIVE, "LocalSystem", "" )

WriteLog( $log, "Service installed" )

EndFunc

Func Check_Service_Installation()

Dim $objWMIService, $objService, $colListOfServices, $strService

$objWMIService = ObjGet("winmgmts:root\cimv2")

$colListOfServices = $objWMIService.ExecQuery ("Select * from Win32_Service")

For $objService in $colListOfServices

If( $objService.Name == $servicename ) Then

$is_service_installed = 1

EndIf

Next

EndFunc

Func WriteLog($filename, $line)

$fh = FileOpen($filename, 1)

$ts = @YEAR & "." & @MON & "." & @MDAY & "-" & @HOUR & ":" & @MIN & ":" & @SEC & " -- "

FileWriteLine($fh, $ts & $line)

FileClose($fh)

EndFunc

Share this post


Link to post
Share on other sites



#3 ·  Posted (edited)

Yes, I followed the examples in the FAQ. I used "ServiceControl.au3" from SumTingWong's post: http://www.autoitscript.com/forum/index.php?showtopic=6487.

I did not use WinVNC as his "test.au3" uses. Instead, I used the following simple test script that prints "Hello World" to a file (this is not the same as my first script above):

CODE
#NoTrayIcon

#include <process.au3>

Dim $log = "c:\temp\log.txt"

While 1

sleep(1000)

WriteLog( $log, "Hello World" )

WEnd

Func WriteLog($filename, $line)

$fh = FileOpen($filename, 1)

$ts = @YEAR & "." & @MON & "." & @MDAY & "-" & @HOUR & ":" & @MIN & ":" & @SEC & " -- "

FileWriteLine($fh, $ts & $line)

FileClose($fh)

EndFunc

Here is my modified "test.au3" from SumTingWong:

CODE
#include "ServiceControl.au3"

Dim $nRet

; Create/delete the helloworld service

If _ServiceExists("", "helloworld") Then

MsgBox(4096,'debug:' , 'helloworld service exists') ;### Debug MSGBOX

If _ServiceRunning("", "helloworld") Then

MsgBox(4096,'debug:' , 'helloworld service running') ;### Debug MSGBOX

If _StopService("", "helloworld") Then

Sleep(1000)

MsgBox(4096,'debug:' , 'helloworld service stopped') ;### Debug MSGBOX

If _DeleteService("", "helloworld") Then

MsgBox(4096,'debug:' , 'helloworld service deleted') ;### Debug MSGBOX

EndIf EndIf Else

If _DeleteService("", "helloworld") Then

MsgBox(4096,'debug:' , 'helloworld service deleted') ;### Debug MSGBOX

EndIf EndIfElse

$nRet = _CreateService("", _

"helloworld", _

"Hello World", _

"c:\dev\hello_world.exe", _

"LocalSystem", _

"", _

BitOR($SERVICE_WIN32_OWN_PROCESS, $SERVICE_INTERACTIVE_PROCESS))

If $nRet Then

MsgBox(4096,'debug:' , 'helloworld service created') ;### Debug MSGBOX

If _StartService("", "helloworld") Then

MsgBox(4096,'debug:' , 'helloworld service started') ;### Debug MSGBOX

EndIf Else

MsgBox(4096,'debug:' , 'Failed to create helloworld service: ' & @error) ;### Debug MSGBOX

EndIfEndIf

SumTingWong's "test.au3" generates a popup message saying "helloworld service created". My "Hello World" script then runs for 30 seconds, but then stops. An error is then entered into the Windows Event Log:

The Hello World service failed to start due to the following error: The service did not respond to the start or control request in a timely fashion.

So I ask again: Does my "Hello World" script need to return anything to notify Windows that it correctly started? Thanks for your help.

Edited by somebody

Share this post


Link to post
Share on other sites

I got my "Hello World" script working with "runassvc". For some reason, it would not work through any other method. Oh well, but thanks for your help :)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0