How to identify Jenkins Slave Operating System in a Build

Jenkins has very useful build step of executing Windows batch or Linux Shell command. User has option to choose Jenkins slave by restricting job to run on slave with specific condition. Jenkins slave may be either Windows or Linux client. We have a way to find Operating Systems of Jenkins slave machine.

While using Jenkins with Windows and Linux slave and Jenkins’ build step as Windows batch or Linux shell commands, Major challenge in job configuration is random slave choice and hence random operating systems of slave.

We know that any Jenkins job having Linux shell command won’t run on Windows slave and below error usually occurs.

[workspace] $ sh -xe C:\Windows\TEMP\
The system cannot find the file specified
FATAL: command execution failed

How to identify Jenkins Slave Operating System in a Build
In the same way any Jenkins job having Windows batch command won’t run on Linux slave and below error message is displayed.

[SLAVE OS] $ cmd /c call /tmp/hudson1452251523978015579.bat
FATAL: command execution failed Cannot run program "cmd" (in directory "/jenkins/workspace/SLAVE OS"): error=2, No such file or directory



How to Make Jenkins Build Step Independent of Jenkins Slave Operating Systems

Jenkins has huge repository of open source plugins and one of them is “Conditional Build Step“. Click here to Download. We can take help of this plugin and find slave operating systems of Jenkins slave. Once we have Jenkins slave then we can make ongoing build steps compatible to Jenkins OS.

Steps to know Jenkins Slave OS

  1. Install Conditional Build Step plugin
  2. You will get below option as Jenkins build step after installation of above plugin
    Add Conditional Build Step
  3. Add Conditional Build Step – Single or Multiple.
  4. In Run? Condition select “Execute Shell” and enter command
     echo "IsShell"
  5. Provide Linux Shell Commands as Steps to run if condition is met.
  6. Add another Conditional Build Step – Single or Multiple.
  7. In Run? Condition Select “Execute Windows Batch Commands” and enter command
  8. Provide Windows batch commands as Steps to run if condition is met.
     echo "IsWindows"


Above job will be able to run on any – Windows or Linux slave without showing above errors.

Let me know if you face any difficulties in configuration of the same, I will be happy to help you guys.

8 Responses

  1. Satish says:

    Hi, thanks for the suggestions in the post. Although it works, the console output will be a bit messy if you choose to run a shell command & windows command in the conditions (With Jenkins ignoring the failed method). Alternate method which worked for me & looked a bit cleaner is choosing the node option in the Step condition. If you have multiple nodes with the same OS, then you can multi select the right nodes.

    • Chintan Mehta says:

      Hi, Yes you are right but here my concern is I have one jenkins job which I am not sure on which OS i will run. Depending on scenarios I may run it on Windows or Linux. I didn’t want to change anything on job so above option will automatically handle OS and execute appropriate shell commands or windows command.
      If you have got what I mean and have some good options plz suggest so I can share.

  2. Ivan says:


    I had the same problem and I just fixed it doing a ‘strings match’ on the environment variables for the logged-in user name.

    String1: ${ENV,var=”USER”}
    String2: jenkins

    String1: ${ENV,var=”USERNAME”}
    String2: jenkins

    I hope it helps.

    • Ryan says:

      I would argue that this is the best method listed here. It prevents any java IO api errors from corrupting the console output (that other steps in the build procedure may act upon).

  3. Philip says:

    Lot’s can be done cross platform by implementing it as python or groovy scripts

  4. ravi says:

    What if the Slave machine is a MAC OS? How to find it?

    • techiepie says:

      With the similar approach, I would find a command which is not present on Linux and available on Mac to detect exception. Never got chance to make mac slave machine. Would update if I ever get.

Leave a Reply