Run a background process permanently on a node through a script on Jenkins and let Jenkins build successfully
The exact details depend on your operating system (which you did not tell), but the Jenkins wiki has a page about this: https://wiki.jenkins-ci.org/display/JENKINS/Spawning+processes+from+build
How can I call a shell script to start a backend Java process?
Jenkins is probably waiting for some pipes to close. Your background process has inherited some file descriptors and is keeping them open for as long as it runs.
If you are lucky, the only file descriptors are 0, 1 and 2 (the standard ones.) You might want to check the file descriptors of the background process using lsof -p PID
where PID is the process id of the background process.
You should make sure all of those file descriptors (both inputs and outputs) are redirected for the background process, so start it with something like:
nohup daemon </dev/null >/dev/null 2>&1 &
Feel free to direct the output to a file other than /dev/null
but make sure you keep the order of the redirections. The order is important.
If you plan to start background processes from a Jenkins job, be advised that Jenkins will kill background processes when build ends. See https://wiki.jenkins-ci.org/display/JENKINS/ProcessTreeKiller on how to prevent that.
Jenkins seems to be the target for nohup in a script started via ssh, how can I prevent that?
If I understood the question correctly, Jenkins is killing all processes at the end of the build and you would like some process to be left running after the build has finished.
You should read https://wiki.jenkins-ci.org/display/JENKINS/ProcessTreeKiller
Essentially, Jenkins searches for processes with some secret value in BUILD_ID environment variable. Just override it for the processes you want to be left alone.
don't fail jenkins build if execute shell fails
To stop further execution when command fails:
command || exit 0
To continue execution when command fails:
command || true
Is there any way to clean up a Jenkins Worflowjob workspace with a groovy script via Jenkins script console?
Alright, investigating this more and googling even more and listening to your ideas (this one particularly inspired me by Daniel Spilker) I have achieved what I wanted, which is:
Independently to CLEAN-UP Pipeline Job's WORKSPACE via Jenkins script console
(only using Jenkins available means and no messing up with Job configuration, nor updating Jenkinsfile, nor replaying)
The code is not surprisingly difficult and for manual demonstration it looks like this:
Jenkins jenkins = Jenkins.instance
Job item = jenkins.getItemByFullName('Sandbox/PipelineTests/SamplePipeline')
println("RootDir: " + item.getRootDir())
for (Node node in jenkins.nodes) {
// Make sure slave is online
if (!node.toComputer().online) {
println "Node '$node.nodeName' is currently offline - skipping workspace cleanup"
continue
}
println "Node '$node.nodeName' is online - performing cleanup:"
// Do some cleanup
FilePath wrksp = node.getWorkspaceFor(item)
println("WRKSP " + wrksp)
println("ls " + wrksp.list())
println("Free space " + wrksp.getFreeDiskSpace())
println("===== PERFORMING CLEAN UP!!! =====")
wrksp.deleteContents()
println("ls now " + wrksp.list())
println("Free space now " + wrksp.getFreeDiskSpace())
}
Its output, if your job is found, looks like:
Result
RootDir: /var/lib/jenkins/jobs/Sandbox/jobs/PipelineTests/jobs/SamplePipeline
....
.... other node's output noise
....
Node 'mcs-ubuntu-chch' is online - performing cleanup:
WRKSP /var/lib/jenkins/workspace/Sandbox/PipelineTests/SamplePipeline
ls [/var/lib/jenkins/workspace/Sandbox/PipelineTests/SamplePipeline/README.md, /var/lib/jenkins/workspace/Sandbox/PipelineTests/SamplePipeline/.git]
Free space 3494574714880
===== PERFORMING CLEAN UP!!! =====
ls now []
Free space now 3494574919680
Mission completed:)
References
Mainly Jenkins javadoc
- https://javadoc.jenkins.io/hudson/model/Node.html
- https://javadoc.jenkins.io/hudson/model/TopLevelItem.html
- https://javadoc.jenkins.io/hudson/model/Item.html
- https://javadoc.jenkins.io/hudson/FilePath.html
Why does `su` not work in Jenkins?
su
doesn't enter an interactive session when in a non-interactive session the way it does in an interactive session.
In a shell script you get to run a single command in the su
context su <user> <command>
.
Related Topics
How to Dump Part of Binary File
Docker: Are You Trying to Connect to a Tls-Enabled Daemon Without Tls
How to Compare Two Tarball's Content
Linux - Yum Install Gcc - Missing Kernel-Headers
Linux: Screen Desktop Video Capture Over Network, and Vnc Framerate
Linux Script with Curl to Check Webservice Is Up
How to Remove Jenkins Completely from Linux
Git Merge Branch of Another Remote
Why Using Dirname in Find Command Gives Dots for Each Match
Changing Highlight Line Color in Emacs
Can Clang Compile Code with Gcc Compiled .A Libs
Loading Module on Keyboard Hotplug
What Is Active Memory and Inactive Memory
How to Swap /Dev/Sda with /Dev/Sdb
How to Programmatically Switch to a Specific Window in Compiz