Jump to content

How do I run a .bat as a service


MSF
 Share

Recommended Posts

I am trying to run my dos .bat script as a service. I have followed the instructions in _Service_ UDF / Run your exe as Service !.

My autoIT .exe is successfully installed and running as a service. However, when the trigger to run the dos command in my script is pulled, my .bat file does not execute.

I have tried many different forms of the RunWait(@ComSpec...) with no success. Here is my current implementation

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Version=Beta
#AutoIt3Wrapper_Change2CUI=y
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
; Example of using service UDF to make an exe possibly run as a service
; By Arcker
; 10/09/2008
#include<Service.au3>
#include<File.au3>

$sServiceName = "trigger agent"
If $cmdline[0] > 0 Then
    Switch $cmdline[1]
        Case "install", "-i", "/i"
            InstallService()
        Case "remove", "-u", "/u", "uninstall"
            RemoveService()
        Case Else
            ConsoleWrite(" - - - Help - - - " & @crlf)
            ConsoleWrite("params : " & @crlf)
            ConsoleWrite("  -i : install service" & @crlf)
            ConsoleWrite("  -u : remove service" & @crlf)
            ConsoleWrite(" - - - - - - - - " & @crlf)
            Exit
            ;start service.
    EndSwitch
EndIf
_Service_init($sServiceName)

func main(); my code starts here

    $file_loc = @ScriptDir
    $trigger = "runcom.txt"
    $bat = "runcom.bat"

    while 1

        $search = FileFindFirstFile($trigger)
        ;ConsoleWrite("$search = "&$search&@CR)

            If $search == 1 Then
                RunWait(@ComSpec & " cd \"&@CR)
                RunWait(@ComSpec & " runcom.bat")
            EndIf

            FileDelete($file_loc & "\" & $trigger)

        Sleep(1000)

        FileClose($search)

    WEnd
EndFunc

Func InstallService()
    ConsoleWrite("Installing service, please wait" & @CRLF)
    _Service_Create("", $sServiceName, "trigger agent", '"' & @ScriptFullPath & '"')
    If @error Then
        ConsoleWrite("Problem installing service, Error number is " & @error & @CRLF & " message  : " & _WinAPI_GetLastErrorMessage())
    Else
        ConsoleWrite("Installation of service successful")
    EndIf
    Exit
EndFunc   ;==>InstallService
Func RemoveService()
    _StopService("", $sServiceName)
    _DeleteService("", $sServiceName)
    if not @error then ConsoleWrite("service removed successfully" & @crlf)
    Exit
EndFunc   ;==>RemoveService
Link to comment
Share on other sites

I am trying to run my dos .bat script as a service. I have followed the instructions in _Service_ UDF / Run your exe as Service !.

My autoIT .exe is successfully installed and running as a service. However, when the trigger to run the dos command in my script is pulled, my .bat file does not execute.

I have tried many different forms of the RunWait(@ComSpec...) with no success. Here is my current implementation

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Version=Beta
#AutoIt3Wrapper_Change2CUI=y
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
; Example of using service UDF to make an exe possibly run as a service
; By Arcker
; 10/09/2008
#include<Service.au3>
#include<File.au3>

$sServiceName = "trigger agent"
If $cmdline[0] > 0 Then
    Switch $cmdline[1]
        Case "install", "-i", "/i"
            InstallService()
        Case "remove", "-u", "/u", "uninstall"
            RemoveService()
        Case Else
            ConsoleWrite(" - - - Help - - - " & @crlf)
            ConsoleWrite("params : " & @crlf)
            ConsoleWrite("  -i : install service" & @crlf)
            ConsoleWrite("  -u : remove service" & @crlf)
            ConsoleWrite(" - - - - - - - - " & @crlf)
            Exit
            ;start service.
    EndSwitch
EndIf
_Service_init($sServiceName)

func main(); my code starts here

    $file_loc = @ScriptDir
    $trigger = "runcom.txt"
    $bat = "runcom.bat"

    while 1

        $search = FileFindFirstFile($trigger)
        ;ConsoleWrite("$search = "&$search&@CR)

            If $search == 1 Then
                RunWait(@ComSpec & " cd \"&@CR)
                RunWait(@ComSpec & " runcom.bat")
            EndIf

            FileDelete($file_loc & "\" & $trigger)

        Sleep(1000)

        FileClose($search)

    WEnd
EndFunc

Func InstallService()
    ConsoleWrite("Installing service, please wait" & @CRLF)
    _Service_Create("", $sServiceName, "trigger agent", '"' & @ScriptFullPath & '"')
    If @error Then
        ConsoleWrite("Problem installing service, Error number is " & @error & @CRLF & " message  : " & _WinAPI_GetLastErrorMessage())
    Else
        ConsoleWrite("Installation of service successful")
    EndIf
    Exit
EndFunc   ;==>InstallService
Func RemoveService()
    _StopService("", $sServiceName)
    _DeleteService("", $sServiceName)
    if not @error then ConsoleWrite("service removed successfully" & @crlf)
    Exit
EndFunc   ;==>RemoveService

Hi,

try to run your runcom.bat with full path

e.g:

RunWait(@ComSpec & " /c " & @ScriptDir & "\runcom.bat")

instead of

RunWait(@ComSpec & " cd \"&@CR); this doesn't work. It's opening an DOS Shell without closing it. RunWait waits forever...

RunWait(@ComSpec & " runcom.bat"); just try my suggestion or try it without your cd \ command.

;-))

Stefan

Edited by 99ojo
Link to comment
Share on other sites

Hi,

try to run your runcom.bat with full path

e.g:

RunWait(@ComSpec & " /c " & @ScriptDir & "\runcom.bat")

instead of

RunWait(@ComSpec & " cd \"&@CR); this doesn't work. It's opening an DOS Shell without closing it. RunWait waits forever...

RunWait(@ComSpec & " runcom.bat"); just try my suggestion or try it without your cd \ command.

;-))

Stefan

Thanks for the suggestion. I tried RunWait(@ComSpec & "/c" & @ScriptDir & "\runcom.bat") but that didn't work.

perhaps the problem is with my .bat file?

runcom.bat:

@echo off
echo runcom.bat executed!!
pause
Link to comment
Share on other sites

Thanks for the suggestion. I tried RunWait(@ComSpec & "/c" & @ScriptDir & "\runcom.bat") but that didn't work.

perhaps the problem is with my .bat file?

runcom.bat:

@echo off
echo runcom.bat executed!!
pause

Hi,

just remove line RunWait(@ComSpec & " cd \"&@CR) from your script. Doesn't make sense.

Then recompile exe only with the RunWait(@ComSpec & " runcom.bat") command and try it again.

;-))

Stefan

Link to comment
Share on other sites

Hi,

just remove line RunWait(@ComSpec & " cd \"&@CR) from your script. Doesn't make sense.

Then recompile exe only with the RunWait(@ComSpec & " runcom.bat") command and try it again.

;-))

Stefan

Sorry for the lack of explanation. I did remove RunWait(@ComSpec & " cd \"&@CR).

func main()

    $file_loc = @ScriptDir
    $trigger = "runcom.txt"
    $bat = "runcom.bat"

    while 1

        $search = FileFindFirstFile($trigger)
        
            If $search == 1 Then
                
                RunWait(@ComSpec & "/c" & @ScriptDir & "\runcom.bat");Should use $file_loc but that wouldn't make a difference in this case

            EndIf

            FileDelete($file_loc & "\" & $trigger)

        Sleep(1000)

        FileClose($search)

    WEnd
EndFunc
Edited by MSF
Link to comment
Share on other sites

Sorry for the lack of explanation. I did remove RunWait(@ComSpec & " cd \"&@CR).

func main()

    $file_loc = @ScriptDir
    $trigger = "runcom.txt"
    $bat = "runcom.bat"

    while 1

        $search = FileFindFirstFile($trigger)
        
            If $search == 1 Then
                
                RunWait(@ComSpec & "/c" & @ScriptDir & "\runcom.bat");Should use $file_loc but that wouldn't make a difference in this case

            EndIf

            FileDelete($file_loc & "\" & $trigger)

        Sleep(1000)

        FileClose($search)

    WEnd
EndFunc

Hi,

1) Maybe a dumm question, but your runcom.bat is in the same directory then your service exe file?

RunWait(@ComSpec & "/c" & @ScriptDir & "\runcom.bat")

2) The service is running under user account or local system

If local system the flag intract with desktop must set, otherwise you never see a shell. Only in taskmgr as cmd.exe.

For some debugging i would change the following:

3) Just change your runcom.bat to

@echo off
net send %computername% runcom executed
pause
and be sure that messenger service is running.

4) If you test your service while you are logged on and the service is running under your account, put a msgbox before the runwait.

;-))

Stefan

Edited by 99ojo
Link to comment
Share on other sites

Hi,

1) Maybe a dumm question, but your runcom.bat is in the same directory then your service exe file?

2) The service is running under user account or local system

If local system the flag intract with desktop must set, otherwise you never see a shell. Only in taskmgr as cmd.exe.

For some debugging i would change the following:

3) Just change your runcom.bat to

@echo off
net send %computername% runcom executed
pause
and be sure that messenger service is running.

4) If you test your service while you are logged on and the service is running under your account, put a msgbox before the runwait.

;-))

Stefan

1. Yes

2. Local System Account: Interact with desktop checked - Still fails

3. Done

4. Inserted a msgbox statement and it was never called. However, the trigger file was deleted as expected.

Link to comment
Share on other sites

1. Yes

2. Local System Account: Interact with desktop checked - Still fails

3. Done

4. Inserted a msgbox statement and it was never called. However, the trigger file was deleted as expected.

Hi,

there might be restrictions for local system account.

1) Change your echo in bat to: echo runcom executed >.\executed.txt and look if file is created.

2) If file isn't created try to run service under administrator account.

3) You might run your exe as sheduled task.

;-))

Stefan

Edited by 99ojo
Link to comment
Share on other sites

@MSF,

In this post, this line of code is missing one space:

RunWait(@ComSpec & "/c" & @ScriptDir & "\runcom.bat")

RunWait(@ComSpec & " /c" & @ScriptDir & "\runcom.bat")

The help file states:

; don't forget " " before "/c"

However, the help file also states:

To run DOS commands, try RunWait(@ComSpec & " /c " & "commandName") ; don't forget " " before "/c"

A ".bat" file is not a DOS command and does not require the command interpreter provided via ComSpec.

Try simply:

RunWait(@ScriptDir & "\runcom.bat")

Then you can chase down any service/permissions/interaction issues :-)

Edit: pasting from the help file messes stuff up in Firefox... not not in IE8.

Edited by herewasplato

[size="1"][font="Arial"].[u].[/u][/font][/size]

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...