Check if agent is running as a service

189 views
Skip to first unread message

Adakar

unread,
Dec 3, 2019, 8:56:43 AM12/3/19
to Jenkins Developers
Hi There :)

I'm working on a Jenkins plugin. I'm looking for a way to check if the Jenkins agent is running as a Windows service. So far my idea is to check if the WINSW_SERVICE_ID is set. Is there any other way? Any suggestions are highly appreciated :)

BR, Adakar

Jonathan Mackenzie

unread,
Dec 3, 2019, 10:56:54 AM12/3/19
to jenkin...@googlegroups.com
C:\WINDOWS\system32>sc query "jenkins"

SERVICE_NAME: jenkins
        TYPE               : 10  WIN32_OWN_PROCESS
        STATE              : 4  RUNNING
                                (STOPPABLE, NOT_PAUSABLE, ACCEPTS_SHUTDOWN)
        WIN32_EXIT_CODE    : 0  (0x0)
        SERVICE_EXIT_CODE  : 0  (0x0)
        CHECKPOINT         : 0x0
        WAIT_HINT          : 0x0

C:\WINDOWS\system32>sc query "fred"
[SC] EnumQueryServicesStatus:OpenService FAILED 1060:

The specified service does not exist as an installed service.


C:\WINDOWS\system32>

--
You received this message because you are subscribed to the Google Groups "Jenkins Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-de...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-dev/3193b476-8414-4c83-a3f3-a45cd6cbe1b5%40googlegroups.com.

Adakar

unread,
Dec 4, 2019, 5:14:40 AM12/4/19
to Jenkins Developers
Hi Jonathan,

Thank you for the answer! I know that I can check that with the help of the "sc query <name>"
A problem I see with this solution is the lack of knowledge about the service name. Services can be installed under custom names(e.g. jenkinsslave-D__tmp_win_Agent), right?
I wonder if there is a Jenkins Java API to get information about the name or if the agent was started as a service?
So far I found that the plugin sees the WINSW_SERVICE_ID env variable when the agent is running as a service but I'm not sure if this check is reliable and sufficient.

BR, Adakar

W dniu wtorek, 3 grudnia 2019 16:56:54 UTC+1 użytkownik Jonathan Mackenzie napisał:
C:\WINDOWS\system32>sc query "jenkins"

SERVICE_NAME: jenkins
        TYPE               : 10  WIN32_OWN_PROCESS
        STATE              : 4  RUNNING
                                (STOPPABLE, NOT_PAUSABLE, ACCEPTS_SHUTDOWN)
        WIN32_EXIT_CODE    : 0  (0x0)
        SERVICE_EXIT_CODE  : 0  (0x0)
        CHECKPOINT         : 0x0
        WAIT_HINT          : 0x0

C:\WINDOWS\system32>sc query "fred"
[SC] EnumQueryServicesStatus:OpenService FAILED 1060:

The specified service does not exist as an installed service.


C:\WINDOWS\system32>

On Tue, Dec 3, 2019 at 5:56 AM Adakar <jaku...@gmail.com> wrote:
Hi There :)

I'm working on a Jenkins plugin. I'm looking for a way to check if the Jenkins agent is running as a Windows service. So far my idea is to check if the WINSW_SERVICE_ID is set. Is there any other way? Any suggestions are highly appreciated :)

BR, Adakar

--
You received this message because you are subscribed to the Google Groups "Jenkins Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkin...@googlegroups.com.

Jonathan Mackenzie

unread,
Dec 5, 2019, 1:36:58 AM12/5/19
to jenkin...@googlegroups.com
Yeah you can also use

C:\WINDOWS\system32>net start | findstr "Jen"
   Jenkins

C:\WINDOWS\system32>

but it sounds to me like you have a wider use case here. Maybe you'd like to explain it?



To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-de...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-dev/6599e4e8-fded-4ab2-8e72-0651f718d9e9%40googlegroups.com.

Markus Winter

unread,
Dec 5, 2019, 3:04:25 AM12/5/19
to jenkin...@googlegroups.com
as you said you write a plugin you could just check what kind of launcher is configured for the agent within your Java code
Anyway it would help to understand why you need to know if it is running as Windows Service.
A couple of years ago we made the experience that running Windows agents via Service was not very stable, we lost frequently the connection. We then installed cygwin and start the agents via ssh. Since then we do not have any problems anymore.

Adakar

unread,
Dec 5, 2019, 5:26:33 AM12/5/19
to Jenkins Developers
My intention is to customize a plugin behavior based on the fact if the agent is running as a service. If it does, it should stop the build. If not, the build should be continued. I simply want my plugin to stop builds which are executing on agents that are running as a Windows Service.
--
You received this message because you are subscribed to the Google Groups "Jenkins Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkin...@googlegroups.com.

Daniel Beck

unread,
Dec 5, 2019, 5:57:53 AM12/5/19
to JenkinsCI Developers
On Thu, Dec 5, 2019 at 11:26 AM Adakar <jaku...@gmail.com> wrote:
My intention is to customize a plugin behavior based on the fact if the agent is running as a service. If it does, it should stop the build. If not, the build should be continued. I simply want my plugin to stop builds which are executing on agents that are running as a Windows Service.

That looks a bit like an XY problem (http://xyproblem.info/), if so, it would be helpful if you could provide additional context.

For example, if there's something preventing builds run from a Windows service to be completed successfully (e.g. no desktop access) in your environment, perhaps you can check for that directly rather than trying to identify whether you're running as a service? 

Adakar

unread,
Dec 6, 2019, 5:05:40 AM12/6/19
to Jenkins Developers
Hi Daniel,

Thanks for the explanation and pointing this out :)

Bingo! The access to a display is mandatory for the plugin to be executed succesfully. That's exactly the reason why I wanted the plugin to stop the execution if the agent is running as a service. Although information if it is a service or not might still be useful for more detailed output.

Matt Sicker

unread,
Dec 6, 2019, 3:02:27 PM12/6/19
to jenkin...@googlegroups.com
A low tech workaround for this would be using node labels to define
which nodes have the graphical environment and which ones don't.
> --
> You received this message because you are subscribed to the Google Groups "Jenkins Developers" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-de...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-dev/a1563dfd-f5f7-4e8a-9cc5-c0b46c322e82%40googlegroups.com.



--
Matt Sicker
Senior Software Engineer, CloudBees

Daniel Beck

unread,
Dec 6, 2019, 3:35:14 PM12/6/19
to Jenkins Dev


> On 6. Dec 2019, at 21:02, Matt Sicker <msi...@cloudbees.com> wrote:
>
> A low tech workaround for this would be using node labels to define
> which nodes have the graphical environment and which ones don't.

I wonder whether this is something that could be done automatically in platformlabeler plugin. Probably would need a native component though.

Mark Strasser

unread,
Dec 7, 2019, 10:09:04 AM12/7/19
to Jenkins Developers
Do services start in headless mode? If so you could just query the system property "java.awt.headless"?

https://www.oracle.com/technical-resources/articles/javase/headless.html

Thanks
-Mark

Reply all
Reply to author
Forward
0 new messages