Jump to content

Create Windows Services


 Share

Recommended Posts

Hi Guys,

    I need help.
    I created a service in Windows to start every time I reboot the machine.
    At startup the service is returning error: 1053 The service did not respond to the start or control request in a timely manner.
    I want to know if the problem is in the .EXE that I generated or in the service created via AutoIT?

 

include <Services_1.au3>

    If @OSArch = "X64" Then
        $path = "c:\Program Files (x86)\teste"
        DirCreate($path)
    Else
        $path = "c:\Program Files\teste"
        DirCreate($path)
    EndIf


Global $retorno = _Service_Create("Agente.Teste", _
        "Agente - Teste", _
        $SERVICE_WIN32_OWN_PROCESS, _
        $SERVICE_AUTO_START, _
        $SERVICE_ERROR_IGNORE, _
        $path & "\Agente.Teste.exe")
        
MsgBox(0, "$retorno",  $retorno)
MsgBox(0, "@error", @error)

 

Tks.

Blois

Edited by Blois
Link to comment
Share on other sites

writing a Windows service is not trivial. fortunately someone already did all the hard work for you:

 

however, consider this: if automatically starting your script with Windows is all you need, you can simply use a scheduled task. it's much simpler.

Signature - my forum contributions:

Spoiler

UDF:

LFN - support for long file names (over 260 characters)

InputImpose - impose valid characters in an input control

TimeConvert - convert UTC to/from local time and/or reformat the string representation

AMF - accept multiple files from Windows Explorer context menu

DateDuration -  literal description of the difference between given dates

Apps:

Touch - set the "modified" timestamp of a file to current time

Show For Files - tray menu to show/hide files extensions, hidden & system files, and selection checkboxes

SPDiff - Single-Pane Text Diff

 

Link to comment
Share on other sites

there are many ways to launch a program at logon. the above suggestion will launch the program at user logon, under the user account, and is easily accessible to anyone not a complete novice.

to launch a program at system start (before any user logon - naturally under the local SYSTEM account) you'd want to use a scheduled task. if you have some unique technical features - like a filesystem driver - then you'd use a Windows service.

Signature - my forum contributions:

Spoiler

UDF:

LFN - support for long file names (over 260 characters)

InputImpose - impose valid characters in an input control

TimeConvert - convert UTC to/from local time and/or reformat the string representation

AMF - accept multiple files from Windows Explorer context menu

DateDuration -  literal description of the difference between given dates

Apps:

Touch - set the "modified" timestamp of a file to current time

Show For Files - tray menu to show/hide files extensions, hidden & system files, and selection checkboxes

SPDiff - Single-Pane Text Diff

 

Link to comment
Share on other sites

7 hours ago, orbs said:

writing a Windows service is not trivial. fortunately someone already did all the hard work for you:

 

however, consider this: if automatically starting your script with Windows is all you need, you can simply use a scheduled task. it's much simpler.

This scheduled task is not effective for me.

I use this UDF Servicesv4 for create services in Windows.

I necessary starting my .EXE in windows service, but return error 1053 in startup in services.msc

 

 

Tks

Blois

Link to comment
Share on other sites

4 minutes ago, Blois said:

This scheduled task is not effective for me.

just for my curiosity, why is that?

 

you description is clear. the issue is obviously with your EXE - for a program to act like a service, in order to function properly, it must respond to requests from Windows Services Manager (a component of Windows in charge of managing services). i recommend you study the example script attached to the UDF and modify it to your needs.

Signature - my forum contributions:

Spoiler

UDF:

LFN - support for long file names (over 260 characters)

InputImpose - impose valid characters in an input control

TimeConvert - convert UTC to/from local time and/or reformat the string representation

AMF - accept multiple files from Windows Explorer context menu

DateDuration -  literal description of the difference between given dates

Apps:

Touch - set the "modified" timestamp of a file to current time

Show For Files - tray menu to show/hide files extensions, hidden & system files, and selection checkboxes

SPDiff - Single-Pane Text Diff

 

Link to comment
Share on other sites

21 hours ago, orbs said:

just for my curiosity, why is that?

 

you description is clear. the issue is obviously with your EXE - for a program to act like a service, in order to function properly, it must respond to requests from Windows Services Manager (a component of Windows in charge of managing services). i recommend you study the example script attached to the UDF and modify it to your needs.

Excuse me, but you said using the insert solution in Windows Startup is not effective for me.

I downloaded the UDF v4 from this link and even trying to just run the example it already displays error about the const variables in SecurityConstants.au3.

Then I created the service with another Services.au3 that I found and it worked, however when starting the service I created it returns error 1053.

My service installation code:

include <Services_1.au3>

    If @OSArch = "X64" Then
        $path = "c:\Program Files (x86)\teste"
        DirCreate($path)
    Else
        $path = "c:\Program Files\teste"
        DirCreate($path)
    EndIf


Global $retorno = _Service_Create("Agente.Teste", _
        "Agente - Teste", _
        $SERVICE_WIN32_OWN_PROCESS, _
        $SERVICE_AUTO_START, _
        $SERVICE_ERROR_IGNORE, _
        $path & "\Agente.Teste.exe")
        
MsgBox(0, "$retorno",  $retorno)
MsgBox(0, "@error", @error)

My Code Agente.Teste.exe

#include <Misc.au3>

#NoTrayIcon
_Singleton("Agente.Teste.exe") ;Única instância na máquina

Global $path

If @OSArch = "X64" Then
    $path = "c:\Program Files (x86)\teste"
    DirCreate($path)
Else
    $path = "c:\Program Files\teste"
    DirCreate($path)
EndIf

While 1
    If Not ProcessExists("Teste.exe") Then
        Run($path & "\Teste.exe")
    EndIf
    Sleep(30000)
WEnd

 

The service I created starts Agent.teste.exe

Can I explain it better?

 

 

Edited by Blois
Link to comment
Share on other sites

4 minutes ago, Blois said:

displays error about the const variables in SecurityConstants.au3.

Did you try to correct those errors so it could be used? That UDF was created in 2008, probably it has variables that are already part of AutoIt that have been added in the last 11 years.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

1 hour ago, BrewManNH said:

Did you try to correct those errors so it could be used? That UDF was created in 2008, probably it has variables that are already part of AutoIt that have been added in the last 11 years.

I've compiled and handled the bugs and now it worked.
I inserted a msgbox inside the area

#region -> insert your running code here

I tried the service but msgbox does not appear in any moment.
what did I do wrong?

Link to comment
Share on other sites

Services run in Session 0, and unless they're set to interactive, you can't see things like message boxes and guis. There are ways to get around this, and a google search should help in that.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

4 hours ago, BrewManNH said:

Services run in Session 0, and unless they're set to interactive, you can't see things like message boxes and guis. There are ways to get around this, and a google search should help in that.

 

I have already configured the "Allow the service to interact with the desktop" option in the Logon tab in Windows Services. but the error continues.

 

Tks. Blois

Link to comment
Share on other sites

the "Allow the service to interact with the desktop" feature has been deprecated since Windows Server 2008 (and Windows Vista for desktop systems). here is a nicely-written blog post describing the issues it caused.

the official approach is not to have the service interact with the desktop, but to communicate with a client application that runs under the user context. search the forum and wiki for "IPC" (Inter-Process Communication) for details on how to implement this.

Signature - my forum contributions:

Spoiler

UDF:

LFN - support for long file names (over 260 characters)

InputImpose - impose valid characters in an input control

TimeConvert - convert UTC to/from local time and/or reformat the string representation

AMF - accept multiple files from Windows Explorer context menu

DateDuration -  literal description of the difference between given dates

Apps:

Touch - set the "modified" timestamp of a file to current time

Show For Files - tray menu to show/hide files extensions, hidden & system files, and selection checkboxes

SPDiff - Single-Pane Text Diff

 

Link to comment
Share on other sites

7 hours ago, Blois said:

Excuse me, but you said using the insert solution in Windows Startup is not effective for me.

I downloaded the UDF v4 from this link and even trying to just run the example it already displays error about the const variables in SecurityConstants.au3.

Then I created the service with another Services.au3 that I found and it worked, however when starting the service I created it returns error 1053.

My service installation code:

include <Services_1.au3>

    If @OSArch = "X64" Then
        $path = "c:\Program Files (x86)\teste"
        DirCreate($path)
    Else
        $path = "c:\Program Files\teste"
        DirCreate($path)
    EndIf


Global $retorno = _Service_Create("Agente.Teste", _
        "Agente - Teste", _
        $SERVICE_WIN32_OWN_PROCESS, _
        $SERVICE_AUTO_START, _
        $SERVICE_ERROR_IGNORE, _
        $path & "\Agente.Teste.exe")
        
MsgBox(0, "$retorno",  $retorno)
MsgBox(0, "@error", @error)

My Code Agente.Teste.exe

#include <Misc.au3>

#NoTrayIcon
_Singleton("Agente.Teste.exe") ;Única instância na máquina

Global $path

If @OSArch = "X64" Then
    $path = "c:\Program Files (x86)\teste"
    DirCreate($path)
Else
    $path = "c:\Program Files\teste"
    DirCreate($path)
EndIf

While 1
    If Not ProcessExists("Teste.exe") Then
        Run($path & "\Teste.exe")
    EndIf
    Sleep(30000)
WEnd

 

The service I created starts Agent.teste.exe

Can I explain it better?

 

 

Correct me if I'm wrong.  The first script create a directory either in C:\Program Files (x86)\teste or C:\Program Files\teste.   Then create a service called Agente - Teste and finally give you a message box to show you if there were any errors creating the services.

What I don't see is the part of the first script copying Agente.Teste.exe into C:\Program Files\teste? That could be the reason for error 1053 "not respond in a timely fashion.

AutoIt Scripts:NetPrinter - Network Printer UtilityRobocopyGUI - GUI interface for M$ robocopy command line
Link to comment
Share on other sites

I dont have any code to share here, but an opinion about the services udf, which is that it will randomly cause your service to crash/stop. I had to create a service to run on 500 pcs, and be very reliable, and that was not it. In the end, I created a script with a while loop that just never exited, and used nssm to register the service with windows. Now, two years later, the numbers of times I've known this service to crash is in single digits. 

 

 

Link to comment
Share on other sites

On 4/18/2019 at 5:52 PM, Danny35d said:

Correct me if I'm wrong.  The first script create a directory either in C:\Program Files (x86)\teste or C:\Program Files\teste.   Then create a service called Agente - Teste and finally give you a message box to show you if there were any errors creating the services.

What I don't see is the part of the first script copying Agente.Teste.exe into C:\Program Files\teste? That could be the reason for error 1053 "not respond in a timely fashion.

The file copy is performed in the installation script.
Script service installation:
It validates which directory is being used to create the correct path in services.msc

The Agent.Test.exe already exists there but when running it reports error 1053.

Link to comment
Share on other sites

On 4/18/2019 at 6:29 PM, kaisies said:

I dont have any code to share here, but an opinion about the services udf, which is that it will randomly cause your service to crash/stop. I had to create a service to run on 500 pcs, and be very reliable, and that was not it. In the end, I created a script with a while loop that just never exited, and used nssm to register the service with windows. Now, two years later, the numbers of times I've known this service to crash is in single digits. 

 

 

I do not download in my work, this is blocked, so I'm using AutoIT to create the service, since I can create the code.

Link to comment
Share on other sites

@Blois, your code for Agente.Teste.exe is wrong. there is no simpler way to describe it. your code must interact with Windows Service Manager, and you must look at the UDF example to see how it's done. #include the UDF is not enough in this case - you must use the appropriate functionality of the UDF to respond to requests from Windows Service Manager, and since you do not do that, Windows Service Manager tells you simply that "The service did not respond". how much more do you want the answer to be simplified?

Signature - my forum contributions:

Spoiler

UDF:

LFN - support for long file names (over 260 characters)

InputImpose - impose valid characters in an input control

TimeConvert - convert UTC to/from local time and/or reformat the string representation

AMF - accept multiple files from Windows Explorer context menu

DateDuration -  literal description of the difference between given dates

Apps:

Touch - set the "modified" timestamp of a file to current time

Show For Files - tray menu to show/hide files extensions, hidden & system files, and selection checkboxes

SPDiff - Single-Pane Text Diff

 

Link to comment
Share on other sites

2 hours ago, orbs said:

@Blois, your code for Agente.Teste.exe is wrong. there is no simpler way to describe it. your code must interact with Windows Service Manager, and you must look at the UDF example to see how it's done. #include the UDF is not enough in this case - you must use the appropriate functionality of the UDF to respond to requests from Windows Service Manager, and since you do not do that, Windows Service Manager tells you simply that "The service did not respond". how much more do you want the answer to be simplified?

So my agent should be script: ServiceExample_v4.au3?
Instead of what I did?

 

In ServiceExample_v4.au3 I entered the code:

#region --> insert your running code here
    _Sleep(1000)
    MsgBox(0,"", "TESTE DENTRO DO CÒDIGO")
    
#region --> insert your running code here

But did not show msgbox. :( 

I'm sorry but some things still confuse me, so many questions.

Edited by Blois
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...