How to open another cmd terminal via jenkins service - jenkins

I want to open another cmd terminal GUI via jenkins service, but I dont know how to do it.
in test.bat file, this file will run hello.bat in another cmd terminal
echo "testing ..."
start /min cmd /c hello.bat
in hello.bat file
echo "I see you 1"
echo "I see you 2"
echo "I see you 3"
ping 127.0.0.1 -n 8 > nul
I run test.bat manually and it works as expected, it trigger the test.bat in another cmd terminal.
But when I try to run test.bat from jenkins, I cannot see another cmd terminal GUI that running hello.bat
Below is the screenshot to prove it is running successful
but there is no another cmd terminal GUI is pop up during jenkins run in the jenkins agent PC
Below is my jenkins service setting, I tested with both Log on option, one is logon as user account, one as "Local System account" and tick the "allow service to interreact with desktop", both fail the expectation.
Please guide me on this issue, thank you !

You can use the Start a program build step in Jenkins and specify cmd.exe as the program to run.
Or you can use the bat step and specify cmd.exe as the command to run if you are using Jenkins pipelines as below:
pipeline {
stages {
stage('Open Command Prompt') {
steps {
bat 'cmd.exe'
bat 'echo Hello from the new command prompt window!'
}
}
}
}
you can read more about the bat command here:
https://www.jenkins.io/doc/pipeline/steps/workflow-durable-task-step/#bat-execute-windows-batch-command

Related

Jenkins hangs after "chroot . sh" during ssh

I'm doing a Jenkins freestyle build, ssh'ing into a VM and running some existing scripts. During a build step "Execute shell script on remote host using ssh", everything is working fine until I get to a command: "chroot . sh". This is mounting a rootfs which we build in, if I do this step manually it is bringing me to a sh prompt where I can run another script to do the actual build, but Jenkins just hangs forever at this point.
From looking around it looks like this is because the command does not return a completion signal?, so Jenkins is waiting indefinitely. I've also just tried doing the same steps in Putty, using a text file containing the commands I need. The Putty "script" also fails at this point, stopping any input because of the new sh prompt.
Is there any way around this? I've tried various solutions, like:
nohup chroot . sh 1>&2 - of course this doesnt work
and running the command in the background doesnt put me into the chroot environment I need.
Kind of confused at this point.
Edit:
Code snippet:
cd /home/dev/root_env
chroot . sh
cd /home/dev
./build.sh
Thats literally all I'm doing, however I freeze forever at line 2 of that.

Unable to start pm2 via jenkins pipeline

on a Windows machine, I have setup a very simple pipeline in Jenkins that does the following:
clone a git repository,
install the packages,
run the app via "pm2 start command"
Below is the entire pipleline script :
node {
stage('dev'){
git credentialsId: 'my-credentials', url: 'git#myurl.git'
bat 'npm install'
bat 'pm2 start src\\index.js --name myapp'
}
}
Everything works fine except running the pm2 command. The output error says :
'pm2' is not recognized as an internal or external command,
operable program or batch file.
However, I can easily run the exact same PM2 command via CMD, I have tried putting the last line command into a .bat file and asked jenkins to execute it, and get the same error.
Jenkins couldn't access the PM2 that was installed on the Windows machine globally that is due to the fact that Jenkin was running as the system (root) user while pm2 was running with the local user. I had to include PM2 in the package.config file of the project and then call it from the node_module folder.
\node_modules\.bin\pm2 start src\\index.js --name myapp

Jenkins run Shellscript via SSH is not leaving console

I'm using Jenkins to deploy my play application for this I've added SSH support to jenkins and I connect via ssh to the test server and then I run a shel script via ssh.
Thats working fine.
Not working ist finishing the job in jenkins.
The command in the the shell script is the following:
/usr/src/activator-dist-1.3.10/bin/activator "~ run" &
that only should run the activator, build and run the project
But then when The application is build and the activator runs the Jenkins job dosn't finish ... it always hang in console
looks like this:
When you run a script via ssh it will stay open until stdout/stderr are closed or a timeout occurs. In Jenkins it seems as if the script hangs.
So if you run a script as background job, make sure to redirect all its output to somewhere:
nohup yourCommand < /dev/null > /dev/null 2>&1 &
or
nohup yourCommand < /dev/null >> logfile.log 2>&1 &
See SSH Frequently Asked Questions for more details.

How can I start a bash login shell in jenkins pipeline (formerly known as workflow)?

I am just starting to convert my Jenkins jobs into the new Jenkins Pipeline(workflow) tool, and I'm having trouble getting the sh command to use a bash login shell.
I've tried
sh '''
#!/bin/bash -l
echo $0
'''
but the echo $0 command is always being executed in an interactive shell, rather then a bash login shell.
#izzekil is right!!!! Thank you so much!
So to elaborate a little bit about what is going on. I used sh with ''' , which indicates a multiple line script. HOWEVER, the resulting shell script that gets dumped on to the jenkins node will be one line down, rather then the first line. So I was able to fix this with this
sh '''#!/bin/bash -l
echo $0
# more stuff I needed to do,
# like use rvm, which doesn't work with shell, it needs bash.
'''

Why the environment variables shown by shell and jenkins different?

I found the environment variables shown by shell and jenkins is different. When I see $PATH by echo, it shows as follows;
# cat /etc/passwd | grep jenkins
jenkins:x:998:997:Jenkins Continuous Integration Server:/var/lib/jenkins:/bin/bash
# su jenkins
bash-4.2$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
However when I exec "echo $PATH" on Jenkins by (Build -> Execute shell), console log shows as follows;
[workspace] $ /bin/sh -xe /tmp/hudson6923847933544830986.sh
+ echo /sbin:/usr/sbin:/bin:/usr/bin
/sbin:/usr/sbin:/bin:/usr/bin
Finished: SUCCESS
Not only $PATH but many other variables are also different, but $PATH is important to execute command and I don't understand why they are not the same. Do you have any idea?
Maybe your user of shell and user who is building the job are not same.
run $ whoami command in your shell and jenkins (Build -> Execute shell).
You can also set PATH variable in Build -> Execute shell section or jenkins -> Manage Jenkins -> configure System -> Global properties section. check the Environment variables then Name: PATH, value: $PATH:/usr/local/sbin.

Resources