'Jenkins does not recognize command sh?

I've been having a lot of trouble trying to get a Jenkinsfile to work. I've been trying to run this test script:

#!/usr/bin/env groovy
node {
    stage('Build') {
        echo 'Building....'
        // Create virtualenv
        sh 'echo "hi"'
    }
    stage('Test') {
        echo 'Building....'
    }
    stage('Deploy') {
        echo 'Deploying....'
    }
}

But I keep getting this error when trying to build:

Warning: JENKINS-41339 probably bogus PATH=/usr/lib64/ccache:/usr/lib64/ccache:$PATH; perhaps you meant to use ‘PATH+EXTRA=/something/bin’?
[test-job-jenkinsfile-pipeline] Running shell script
nohup: failed to run command `sh': No such file or directory

I updated all the pipeline plugins to the latest version and still run into this error. Any help?



Solution 1:[1]

So it seems the reason was that the global property PATH was causing the issue. By going to Manage Jenkins -> Configure System and deleting the PATH global property solved my issue. See JENKINS-41339.

Solution 2:[2]

Jonathan's answer is correct in that modifying $PATH using the Jenkins Environment Variable settings causes this problem - but just deleting the PATH customizations you have will likely cause you to lose functionality, especially if you have any Freestyle type projects in your Jenkins.

See, in the entire rest of the universe it's very common to edit the $PATH by setting it to your new thing plus the existing $PATH, like this:

PATH=/opt/blah/bin:$PATH

This prepends /opt/blah/bin to what's already in $PATH. So the final $PATH might look like: /opt/blah/bin:/usr/local/bin:/usr/sbin:/bin (this is just an example of course)

This actually works fine for Jenkins Freestyle projects. However, for Pipeline projects Jenkins for some reason does not actually evaluate and replace the $PATH variable in the variable you've set. So you literally end up with a path of /opt/blah/bin:$PATH - so nothing that was there before is still in your $PATH!

Apparently instead of just fixing that bug, Jenkins project decided to (1) detect the condition and display a weird warning ("Warning: JENKINS-41339 probably bogus") to imply you should check out that ticket and (2) create a brand new way of defining additions to PATH, which is the best solution to your problem because it allows you to customize the $PATH without breaking everything. You do this in Jenkins->Configure System.

  • Define a variable called PATH+EXTRA where EXTRA can apparently be whatever.

  • In that variable, just put your additions for the PATH. So in my example above, I would NOT set PATH at all, rather I'd just set: PATH+EXTRA=/opt/blah/bin

  • Now remove any defined PATH variable.

According to a related ticket, this is documented somewhere in Jenkins, but is not documented in the place it needs to be, in Manage Jenkins->Configure System.

Solution 3:[3]

In order to fix this issue, in case that you can't delete the PATH global property from "Manage Jenkins -> Configure System", you should add the following step:

withEnv(['PATH+EXTRA=/usr/sbin:/usr/bin:/sbin:/bin'])

Like following: for Scripted Pipeline:

node {
  stage ('STAGE NAME') {
    withEnv(['PATH+EXTRA=/usr/sbin:/usr/bin:/sbin:/bin']) {
      sh '//code block'
    }
  }
}

or for Declarative Pipeline:

pipeline {
  agent {
    label 'master'
  }

  stages {
    stage ('STAGE NAME') {
      steps {
        withEnv(['PATH+EXTRA=/usr/sbin:/usr/bin:/sbin:/bin']) {  
          sh '''
            //code block
          '''
        }
      }
    }

I hope this helps. I also struggled a lot to find a solution for this.

Solution 4:[4]

@XP84's solution worked for me. I was getting issue with running python3 on a Mac Jenkins, I added this to environment variable and it started working. Here's a screenshot if anyone was like me and was having trouble inputting the actual values in respective fields.

enter image description here

Solution 5:[5]

Jenkins doesn't know what you mean by sh

nohup: failed to run command `sh': No such file or directory

This means the executable for your shell is not in your path. Go to Manage Jenkins -> Configure System scroll down until you find the section labelled Shell

empty shell path in Jenkins

Add the path to the executable you want to use for your shell when you call sh. Alternatively make sure the location for the executable for sh is in the path being used by your Jenkins instance (which depending on other factors, may or may not be the same as the system path).

Some examples

On windows you might want sh to mean powershell*. You would do this by setting the shell path to point at powershell.

 C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe

If you are on *nix Jenkins will probably default to use whatever shell sh is already defined as for the user Jenkins is running under, but you could specify the path to a particular shell so jenkins will always use that shell. For example to always run sh as bash one could specify

/bin/bash

*Given that Jenkins has specific build steps for windows batch and powershell commands I tend to think of the Shell specifically as a *nix style shell. On a windows system you would need to install some sort of shell emulator, such as Cygwin.

Solution 6:[6]

Another way to avoid this issue and also not deleting global PATH at Manage Jenkins -> Configure System is to specify the PATH differently, apparently the following works fine and can append the PATH in the current environment

pipeline {
agent any
environment {
  PATH = "/usr/local/bin:$PATH"
}

but the following results into nohup: failed to run command sh': No such file or directory

pipeline {
agent any
environment {
  PATH = "/usr/local/bin:${env.PATH}"
}

Solution 7:[7]

Jenkins' most friendly and simplest solution while dealing with environment variables is to use Environment Injector Plugin

so to fix this issue you just need to do the following:

  1. Install Environment Injector Plugin
  2. Open your pipeline and enable Prepare an environment for the run check box and add PATH=/sbin:/usr/sbin:/usr/bin:/usr/local/bin:/bin inside Properties Content text box.

Solution 8:[8]

In my case, I did this.

  1. Remove Path variable from Jenkins Environment variables.

enter image description here

  1. Added 'withEnv(['PATH+EXTRA=/opt/flutter/bin'])' in my steps.

enter image description here

Solution 9:[9]

I tried all the above... but didn't work until I included in my steps container(). For some reason, when Jenkins is trying to run the .sh file it's trying on Jenkins master, even if my agent is set up:

agent {
    label "gen-java-slave"
}

Global pipeline environment block:

environment {
    PATH = "/usr/local/openjdk-11/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
}

Example of a working stage:

stage('Create a Dump for Senapt Arango DB') {
        steps {
            container('general-container-name') {
                withEnv(['PATH+EXTRA=/usr/bin/arangodump']) {  
                    sh '''
                        ./backup-arango-senapt.sh
                    '''
            }
        }
    }
}
           

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Vadim Kotov
Solution 2
Solution 3 Asgard
Solution 4 Genki
Solution 5
Solution 6 Tiko Lakin
Solution 7 Moamen Naanou
Solution 8 Rana Ranvijay Singh
Solution 9 emilianofs