Error: "xxx: command not found"
Symptom
When running a command in a Jenkins pipeline script, an error of "xxx: command not found" is displayed, but the same command executes successfully in a login shell.
Possible Causes
After the PATH environment variable is modified on the server, the modification has not taken effect in the Jenkins pipeline script execution environment.
Troubleshooting Procedure
- Solution 1
Add the environment variable to the ~/.bashrc file on the server and restart the Jenkins service.
systemctl daemon-reload systemctl restart jenkins
- Solution 2
- Create the ~/jenkins.bashrc file and add the environment variable of the corresponding command. For example:
export HELLO_WORLD=/opt/path/helloworld export PATH=$HELLO_WORLD/bin:$PATH
- Add the following shell command to the pipeline script:
source ~/jenkins.bashrc
- Create the ~/jenkins.bashrc file and add the environment variable of the corresponding command. For example:
- Solution 3
Add the environment variable configuration under Manage Jenkins > Nodes > Select the target node > Configure Node > Node Properties.
- In solution 1, after the Jenkins service is restarted, only changes in the user environment file ~/.bashrc take effect, while changes in the system environment file /etc/profile do not. That is, if environment variables are modified in /etc/profile, the corresponding variables will not be available in Jenkins.
- When executing a command with sudo, it searches only /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin by default, and does not use the PATH environment variable. In such cases, it is not recommended to use sudo. If sudo must be used, the issue can be resolved by adding a symbolic link of the command to a directory included in the search path of sudo. Other methods, such as modifying the sudoers file, are also available; refer to the relevant documentation for details.
- When multiple executable versions of a command exist on the server (for example, Java 8 and Java 11), if the environment variable for Java is changed from Java 8 to Java 11 but Java 8 is still being used, ensure that the modification is made by prepending the corresponding environment variable.
When executing a command, the system searches for the executable based on the environment variable in order of priority. On servers where multiple executable versions exist, if a new path is appended to the end of PATH (for example, $PATH:xxx), it may be overridden by existing paths, causing the old version to still be selected. Therefore, the new path should be prepended to PATH (for example, xxx:$PATH).
# export PATH=/opt/path/binary/bin:$PATH