Jenkins pipeline sh fail with "cannot run program nohup" on windows

BashJenkinsWindows 10Jenkins Pipeline

Bash Problem Overview


I have windows 10 and I want to execute the sh command in the Jenkinsfile from Jenkins pipeline using bash for Ubuntu for windows, but it doesn't work

I have the following stage in my Jenkins pipeline :

stage('sh how to') {
        steps {
            sh 'ls -l'
        }
    }

The error message is :

> [C:\Program Files (x86)\Jenkins\workspace\pipelineascode] Running shell script Cannot run program "nohup" (in directory "C:\Program Files (x86)\Jenkins\workspace\pipelineascode"): CreateProcess error=2, Le fichier spécifié est introuvable

I tried changing Jenkins parameter->shell executable with > C:\Windows\System32\bash.exe

but same error...

how to run sh script using windows 10's bash?

Bash Solutions


Solution 1 - Bash

From a very quick search, it looks like your error is related to the following issue : JENKINS-33708

The main cause looks like the sh step is not supported on the Windows. You may use bat or install Cygwin for instance.

Nevertheless two solutions were proposed in the previous link, suggesting you to do the following steps :

  • Install git-bash
  • Ensure the Git\bin folder (i.e.: C:\Program Files\Git\bin) is in the global search path, in order for Jenkins to find sh.exe
  • Make nohup available for Jenkins, doing the following in git-bash (adapt your paths accordingly) :
  • mklink "C:\Program Files\Git\bin\nohup.exe" "C:\Program Files\git\usr\bin\nohup.exe"
  • mklink "C:\Program Files\Git\bin\msys-2.0.dll" "C:\Program Files\git\usr\bin\msys-2.0.dll"
  • mklink "C:\Program Files\Git\bin\msys-iconv-2.dll" "C:\Program Files\git\usr\bin\msys-iconv-2.dll"
  • mklink "C:\Program Files\Git\bin\msys-intl-8.dll" "C:\Program Files\git\usr\bin\msys-intl-8.dll"

Depending on your installation you may have to use these paths :

  • mklink "C:\Program Files\Git\cmd\nohup.exe" "C:\Program Files\git\usr\bin\nohup.exe"
  • mklink "C:\Program Files\Git\cmd\msys-2.0.dll" "C:\Program Files\git\usr\bin\msys-2.0.dll"
  • mklink "C:\Program Files\Git\cmd\msys-iconv-2.dll" "C:\Program Files\git\usr\bin\msys-iconv-2.dll"
  • mklink "C:\Program Files\Git\cmd\msys-intl-8.dll" "C:\Program Files\git\usr\bin\msys-intl-8.dll"

Solution 2 - Bash

With Git for Windows 2.16.2, I was able to add C:\Program Files\Git\usr\bin to the PATH (rather than C:\Program Files\Git\bin) and consequently my sh commands work in both FreeStyle and Pipeline builds. No mklink was necessary. (Source)

Solution 3 - Bash

With Git for Windows, I had to add C:\Program Files\Git\bin to the PATH environment variable of the slave node in Jenkins (to get access to sh), then add C:\Program Files\Git\usr\bin to the PATH locally on the Windows slave too (to get access to nohup).

Solution 4 - Bash

If you are executing on Windows, just change sh to bat. it will work as expected. Example:

pipeline { agent any stages { stage ('Compile Stage') {

        steps {
            withMaven(maven : 'apache-maven-3.6.1') {
                bat'mvn clean compile'
            }
        }
    }

} }

Solution 5 - Bash

Windows doesn't understand the "sh" command. To enable this, add

C:\Program Files\Git\bin &

C:\Program Files\Git\usr\bin

to the System Environment variable PATH, than restart your system.

Than execute your command in jenkins, it will work.

Solution 6 - Bash

Switching sh to bat worked for me - I am running Jenkins on Windows. But only after I had resolved an issue caused by the fact I had not configured my tools (maven and the JDK) correctly in Jenkins either.

Solution 7 - Bash

In my case I replaced 'sh' by 'bat' in Pipeline script and worked.

Solution 8 - Bash

sh is not windows command. The simple way to enable the use of 'sh' command in windows is to install GIT BASH

Once you install GIT BASH, then you need to set below environment variables path.

  1. C:\Program Files\Git\bin : This path contains sh.exe, bash.exe and git.exe
  2. C:\Program Files\Git\usr\bin : This path contains several Linux based exe and dll (cat.exe, find.exe etc.)

By setting above configuration you will be able to execute 'sh' command in Jenkinsfiles on Jenkins installed on windows machine.

Solution 9 - Bash

I was getting the same error below solutions worked for me..

  1. Install git-bash

  2. for windows use "bat" instead of "sh"

  3. set "C:\Program Files\Git\usr\bin" to PATH(user variable)

Solution 10 - Bash

My observation is that the agent seems to be trying to run nohup in the context where the agent.jar is run, not in the container. It didn't matter what I put in the container, the error message was the same. By putting nohup and sh in the PATH where the jenkins agent is running, I see a change in behavior.

> git config core.sparsecheckout # timeout=10 > git checkout -f c64c7bf905b6a4f5a8f85eb23bbd108f4c805386 sh: /home/jenkins/workspace/projname/simple_docker@tmp/durable-9fedc317/jenkins-log.txt: No such file or directory sh: /home/jenkins/workspace/projname/simple_docker@tmp/durable-9fedc317/jenkins-result.txt.tmp: No such file or directory mv: cannot stat '/home/jenkins/workspace/projname/simple_docker@tmp/durable-9fedc317/jenkins-result.txt.tmp': No such file or directory

I am seeing a folder /home/jenkins/workspace/projname/simple_docker@tmp/durable-9fedc317 which contains a file "script.sh" with the contents "docker inspect -f . repositoryname:tagname"

When this docker command is run manually on the command line, it always produces a single line of output consisting of a single period character. I have no doubt this is not what the jenkins system is looking for.

Solution 11 - Bash

So you want the job running under WSL. If you want all jobs running under WSL have you considered installing Jenkins under WSL? Then everything is already in GNU land and you don't have to bridge the envrionment/culture of windows to GNU from within your Jenkins configuration.

Solution 12 - Bash

I got the above issue in windows 10 and just added the path "C:\Program Files\Git\usr\bin" to the system variables then it started working.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionflopicView Question on Stackoverflow
Solution 1 - BashJohn-PhilipView Answer on Stackoverflow
Solution 2 - BashthSoftView Answer on Stackoverflow
Solution 3 - BashsonicwaveView Answer on Stackoverflow
Solution 4 - BashRaghwendra SonuView Answer on Stackoverflow
Solution 5 - BashNarender GusainView Answer on Stackoverflow
Solution 6 - BashMikeView Answer on Stackoverflow
Solution 7 - BashNavraj JoshiView Answer on Stackoverflow
Solution 8 - BashPushkar Raj SindalView Answer on Stackoverflow
Solution 9 - BashPramod ParmarView Answer on Stackoverflow
Solution 10 - BashIRayTraceView Answer on Stackoverflow
Solution 11 - BashSweavoView Answer on Stackoverflow
Solution 12 - BashN V D Munesh AdabalaView Answer on Stackoverflow