Jenkins bash scripts in temp folder not executing - shell

I have a simple code as script file
#!/bin/bash
name1=$1
name2=$2
echo " $name1 $name2"
when i run in putty connected to the centos system inside the jenkins contained, it is giving output.
But when i try it through jenkins build, It is saying failure as the file is not found.
Started by user Jenkins Admin
Running as SYSTEM
Building in workspace /var/jenkins_home/workspace/my_first_job
[my_first_job] $ /bin/sh -xe /tmp/jenkins5284289512384758361.sh
+ Name=Madan
+ /tmp/1.sh Madan
/tmp/jenkins5284289512384758361.sh: 3: /tmp/jenkins5284289512384758361.sh: /tmp/1.sh: not found
Build step 'Execute shell' marked build as failure
Finished: FAILURE
This is the error.

Related

How to execute shell commands in windows slave machine from Jenkins?

I have connected windows machine as a slave to a Jenkins job
I have installed gitbash.exe in the windows slave machine
I need to execute shell commands from Jenkins in windows slave
Found some solutions like,
Specifying the shell.exe path in the Jenkins > System configuration > executable shell and choose "execute shell" in the build step
- But i should not do this as my Jenkins has several jobs configured with different slaves
How can i execute shell commands from jenkins build step?
Jenkins will execute whatever you have in your build steps on the slave that the build runs on. So if you have an "Execute Shell" step in your build, when it runs on any slave it will execute those steps.
the Git Bash shell installs here by default
C:\Program Files\Git\usr\bin
On your windows slave machine, make that directory (or where-ever you installed to) the first item in the PATH (on the windows machine itself, not in jenkins), and restart your slave service
Since jenkins just runs sh.exe to execute the shell commands, it will find it via the PATH on the machine

Jenkins does not set all properties for Siebel gateway server

We are currently using Siebel 15.14.
I am trying to start our gateway server by creating a job in Jenkins that executes a script. In turn, the script runs a "set up environment properties" script and the command that start the gateway server.
All these happen over an SSH build step in Jenkins.
Specifically:
1) Jenkins job has 1 build step "execute shell script on remote server using ssh": /mypath/start_gateway.sh
2) start.gateway.sh contains:
i) . /mygatewaypath/setupparameters.sh
ii) /mygatewaypath/gtwysrvr/bin/start_ns -r /mygatewaypath/gtwysrvr
3) setupparameters.sh contains:
if [ ! -d "$HOME" ]; then
echo "ERROR: HOME must be set prior to starting the Siebel server!"
exit 1
fi
SIEBEL_ROOT=/mygatewatpath/gtwysrvr ; export SIEBEL_ROOT
SIEBEL_HOME=/mygatewatpath/gtwysrvr ; export SIEBEL_HOME
.
. (other variables go here)
.
Although the jenkins job has a successful build and prints out:
executing script:
/var/SP/administration/scripts/start_gateway.sh
started at Mon Sep 18 18:53:48 2017, pid: 147189
[SSH] exit-status: 0
Finished: SUCCESS
When i try to start the gateway server manager i receive the below error:
Fatal error (2555922): Could not open connection to Siebel Gateway configuration store (%1:%2)., exiting...
Also, if i start the application server (which is based on the above gateway server) , the application server is not started correctly.
It Seems that the environments variables of the setupproperties.sh script are not set fully, so the gateway server start in an unstable state.
try to look at the gateway log file in:
/pathtoyourgateway/log/NameSrvr.log
You should get the error.

How to execute sh file from jenkins running on windows

Cygwin installed in my windows and able to execute the sh file using cmd prompt.
same Cygwin plugin has been installed in jenkins which also running in windows.
I created a job in jenkins build step->execute shell command I am giving the command as sh /cygdrive/d/539707/data/getchanges/gymBuild.sh while executing the job I am facing below exception.
NOTE 1: In Jenkins/configuration/ under shell I didn't mention any path
workspace] $ sh -xe
D:\539707\tomcat-7.0.12\temp\hudson4624102689815543789.sh FATAL:
command execution failed java.io.IOException: Cannot run program "sh"
(in directory "C:\Users\539707.jenkins\jobs\Test_Gym\workspace"):
CreateProcess error=2, The system cannot find the file specified at
java.lang.ProcessBuilder.start(ProcessBuilder.java:1047) at
hudson.Proc$LocalProc.(Proc.java:244) at
hudson.Proc$LocalProc.(Proc.java:216) at
hudson.Launcher$LocalLauncher.launch(Launcher.java:815) at
hudson.plugins.cygpath.CygpathLauncherDecorator$1.launch(CygpathLauncherDecorator.java:66)
at hudson.Launcher$ProcStarter.start(Launcher.java:381) at
hudson.tasks.CommandInterpreter.perform(CommandInterpreter.java:95)
at
hudson.tasks.CommandInterpreter.perform(CommandInterpreter.java:64)
at hudson.tasks.BuildStepMonitor$1.perform(BuildStepMonitor.java:20)
at
hudson.model.AbstractBuild$AbstractBuildExecution.perform(AbstractBuild.java:782)
at hudson.model.Build$BuildExecution.build(Build.java:205) at
hudson.model.Build$BuildExecution.doRun(Build.java:162) at
hudson.model.AbstractBuild$AbstractBuildExecution.run(AbstractBuild.java:534)
at hudson.model.Run.execute(Run.java:1738) at
hudson.model.FreeStyleBuild.run(FreeStyleBuild.java:43) at
hudson.model.ResourceController.execute(ResourceController.java:98)
at hudson.model.Executor.run(Executor.java:410) Caused by:
java.io.IOException: CreateProcess error=2, The system cannot find the
file specified at java.lang.ProcessImpl.create(Native Method) at
java.lang.ProcessImpl.(ProcessImpl.java:385) at
java.lang.ProcessImpl.start(ProcessImpl.java:136) at
java.lang.ProcessBuilder.start(ProcessBuilder.java:1028) ... 16 more
Build step 'Execute shell' marked build as failure
Finished: FAILURE
NOTE 2: In Jenkins/configuration/ under shell I mention C:\cygwin\bin\mintty.exe
After that below is the output
$ C:\cygwin\bin\cygpath -w C:\cygwin\bin\mintty.exe [workspace] $
C:\cygwin\bin\mintty.exe -xe
D:\539707\tomcat-7.0.12\temp\hudson4745164988293910592.sh
/usr/bin/mintty: unknown option '-x' Try '--help' for more
information. Build step 'Execute shell' marked build as failure
Finished: FAILURE
Kindly suggest How to execute sh file from jenkins running on windows
For each windows slave you can do the following to add cygwin to path assuming that your slave has cygwin installed already
Jenkins - Manage Jenkins - Manage Nodes
node - Configure
Environment variables : list of key-value pairs
add: name: PATH value: ${PATH};path-to-cygwin\bin
E.g., name: PATH value: ${PATH};d:\tools\cygwin\bin
here is the solution: Shell executable path to cygwin_home\bin\sh and in jenkins build step->execute shell command give the file name ex *.sh or clear Shell executable path and in jenkins build step->execute windows batch command sh path*.sh

TeamCity configuration for BrowserStack tests

In TeamCity, I'm setting up "command line" runner type. My aim is to run the commandline script in the window & let it run. Then go to next build step without closing the command line window which is opened in the previous step. In my last build step, I want to close the command line window which is still open during the initial build step.
Above scenario is based on my build steps while configuring 'BrowserStack" tests project. I want to execute BrowserStackLocal.cmd in my 'Command Line' runner type step & let it run. Then go to next build step in which i have all my tests. Finally in the last build step, I want to close the command line window after completing the tests.
Currently the build step doesn't know what to do after executing 'start BrowserStackLocal.exe -force' as it will be waiting for a reply code.
Any ideas on how to move to the next build step?
As pointed out by Alina you need to setup a 'Command Line' build step for launching BrowserStackLocal in background before running the tests. The config can be like:
Runner Type = Command Line
Run = Custom Script
Using 'advanced options' set Working directory where BrowserStackLocal is present if it differs from the checkout directory.
Custom Script samples:
For Linux/Mac
#!/bin/bash
./BrowserStackLocal <BrowserStack Key> -force &
For Windows
start /B BrowserStackLocal.exe <BrowserStack Key> -force
You can put a wait to allow the BrowserStackLocal process to start before moving to the next build step in your Team City project. Also, you can set up a post build action to kill the running BrowserStackLocal processes.
Had the same issue recently, starting the binary as a daemon will continue to the next build step.
Open connection:
browserstacklocal.exe --key %system.BrowserStackKey% --local-identifier %system.BrowserStackLocalIdentifier% --force --daemon start
Close connection:
browserstacklocal.exe --local-identifier %system.BrowserStackLocalIdentifier% --daemon stop
you just have to press ALT and TAB
If 'BrowserStack' has an appropriate start-stop commands then you can use them (for example like Tomcat). If not then you can start 'BrowserStack' in background, then run tests and then kill it using the following command option:
-force - kill other running instances of BrowserStack Local

Run a command shell in jenkins

I'm trying to execute a command shell in Jenkins, I'm working on Windows 7. In the console output I have this:
Building in workspace C:\Program Files (x86)\Jenkins\workspace\test
[test] $ sh -xe C:\Windows\TEMP\hudson6299483223982766034.sh
The system cannot find the file specified
FATAL: L'exécution de la commande a échoué.
java.io.IOException: Cannot run program "sh" (in directory "C:\Program Files (x86)\Jenkins\workspace\test"): CreateProcess error=2, Le fichier spécifié est introuvable
at java.lang.ProcessBuilder.start(Unknown Source)
at hudson.Proc$LocalProc.<init>(Proc.java:244)
at hudson.Proc$LocalProc.<init>(Proc.java:216)
at hudson.Launcher$LocalLauncher.launch(Launcher.java:816)
at hudson.Launcher$ProcStarter.start(Launcher.java:382)
at hudson.tasks.CommandInterpreter.perform(CommandInterpreter.java:97)
at hudson.tasks.CommandInterpreter.perform(CommandInterpreter.java:66)
at hudson.tasks.BuildStepMonitor$1.perform(BuildStepMonitor.java:20)
at hudson.model.AbstractBuild$AbstractBuildExecution.perform(AbstractBuild.java:779)
at hudson.model.Build$BuildExecution.build(Build.java:205)
at hudson.model.Build$BuildExecution.doRun(Build.java:162)
at hudson.model.AbstractBuild$AbstractBuildExecution.run(AbstractBuild.java:537)
at hudson.model.Run.execute(Run.java:1744)
at hudson.model.FreeStyleBuild.run(FreeStyleBuild.java:43)
at hudson.model.ResourceController.execute(ResourceController.java:98)
at hudson.model.Executor.run(Executor.java:374)
Caused by: java.io.IOException: CreateProcess error=2, Le fichier spécifié est introuvable
at java.lang.ProcessImpl.create(Native Method)
at java.lang.ProcessImpl.<init>(Unknown Source)
at java.lang.ProcessImpl.start(Unknown Source)
... 16 more
Build step 'Exécuter un script shell' marked build as failure
[BFA] Scanning build for known causes...
[BFA] No failure causes found
[BFA] Done. 0s
Finished: FAILURE
Any idea?
For Windows slave, please use Execute Windows batch command.
For Unix-like slave like linux or Mac, Execute shell is the option.
As far as I know, Windows will not support shell scripts out of the box. You can install Cygwin or Git for Windows, go to Manage Jenkins > Configure System Shell and point it to the location of sh.exe file found in their installation. For example:
C:\Program Files\Git\bin\sh.exe
There is another option I've discovered. This one is better because it allowed me to use shell in pipeline scripts with simple sh "something".
Add the folder to system PATH. Right click on Computer, click properties > advanced system settings > environmental variables, add C:\Program Files\Git\bin\ to your system Path property.
IMPORTANT note: for some reason I had to add it to the system wide Path, adding to user Path didn't work, even though Jenkins was running on this user.
An important note (thanks bugfixr!):
This works. It should be noted that you will need to restart Jenkins in order for it to pick up the new PATH variable. I just went to my services and restated it from there.
Disclaimer: the names may differ slightly as I'm not using English Windows.
This is happens because Jenkins is not aware about the shell path.
In Manage Jenkins -> Configure System -> Shell, set the shell path as
C:\Windows\system32\cmd.exe
Error shows that script does not exists
The file does not exists. check your full path
C:\Windows\TEMP\hudson6299483223982766034.sh
The system cannot find the file specified
Moreover, to launch .sh scripts into windows, you need to have CYGWIN installed and well configured into your path
Confirm that script exists.
Into jenkins script, do the following to confirm that you do have the file
cd C:\Windows\TEMP\
ls -rtl
sh -xe hudson6299483223982766034.sh
Go to Jenkins -> Manage Jenkins -> Configure System -> Global properties
Check the box 'Environment variables' and add the JAVA_HOME path = "C:\Program Files\Java\jdk-10.0.1"
*Don't write bin at the end
I was running a job which ran a shell script in Jenkins on a Windows machine. The job was failing due to the error given below. I was able to fix the error thanks to clues in Andrejz's answer.
Error :
Started by user james
Running as SYSTEM
Building in workspace C:\Users\jamespc\.jenkins\workspace\myfolder\my-job
[my-job] $ sh -xe C:\Users\jamespc\AppData\Local\Temp\jenkins933823447809390219.sh
The system cannot find the file specified
FATAL: command execution failed
java.io.IOException: CreateProcess error=2, The system cannot find the file specified
at java.base/java.lang.ProcessImpl.create(Native Method)
at java.base/java.lang.ProcessImpl.<init>(ProcessImpl.java:478)
at java.base/java.lang.ProcessImpl.start(ProcessImpl.java:154)
at java.base/java.lang.ProcessBuilder.start(ProcessBuilder.java:1107)
Caused: java.io.IOException: Cannot run program "sh" (in directory "C:\Users\jamespc\.jenkins\workspace\myfolder\my-job"): CreateProcess error=2, The system cannot find the file specified
at java.base/java.lang.ProcessBuilder.start(ProcessBuilder.java:1128)
at java.base/java.lang.ProcessBuilder.start(ProcessBuilder.java:1071)
at hudson.Proc$LocalProc.<init>(Proc.java:250)
at hudson.Proc$LocalProc.<init>(Proc.java:219)
at hudson.Launcher$LocalLauncher.launch(Launcher.java:937)
at hudson.Launcher$ProcStarter.start(Launcher.java:455)
at hudson.tasks.CommandInterpreter.perform(CommandInterpreter.java:109)
at hudson.tasks.CommandInterpreter.perform(CommandInterpreter.java:66)
at hudson.tasks.BuildStepMonitor$1.perform(BuildStepMonitor.java:20)
at hudson.model.AbstractBuild$AbstractBuildExecution.perform(AbstractBuild.java:741)
at hudson.model.Build$BuildExecution.build(Build.java:206)
at hudson.model.Build$BuildExecution.doRun(Build.java:163)
at hudson.model.AbstractBuild$AbstractBuildExecution.run(AbstractBuild.java:504)
at hudson.model.Run.execute(Run.java:1853)
at hudson.model.FreeStyleBuild.run(FreeStyleBuild.java:43)
at hudson.model.ResourceController.execute(ResourceController.java:97)
at hudson.model.Executor.run(Executor.java:427)
Build step 'Execute shell' marked build as failure
Finished: FAILURE
Solution :
1 - Install Cygwin and note the directory where it gets installed.
It was C:\cygwin64 in my case. The sh.exe which is needed to run shell scripts is in the "bin" sub-directory, i.e. C:\cygwin64\bin.
2 - Tell Jenkins where sh.exe is located.
Jenkins web console > Manage Jenkins > Configure System > Under shell, set the "Shell executable" = C:\cygwin64\bin\sh.exe > Click apply & also click save.
That's all I did to make my job pass. I was running Jenkins from a war file and I did not need to restart it to make this work.

Resources