is there any way to force Jenkins job to run on foreground?
this is the script I use to start the Job.
cd %LibPath%
java -cp "%LibPath%/*;" STARTJenkins %TestPlanName% %User% %Password% NONE %WORKSPACE%
Issue seems to persist only if user running Jenkins is locked
Related
I am trying to automate a SpringBoot Application through Jenkins.
I have myApp.jar and currently, I run it using the following command
nohup java -jar myApp.jar &
Press Ctrl^C or Ctrl^Z and process keep running in the background.
Logs will be added in nohup.out
Now I want the same process to be done using Jenkins.
In Jenkins, build section, I have selected Execute Shell Script with the above command.
When build is triggered. I can see Application startup Log in Jenkins Log but the problem is, build never finishes.
I have tried
BUILD_ID=dontKillMe timeout --foreground 30 nohup java -jar website-status.jar &
also
BUILD_ID=dontKillMe nohup java -jar website-status.jar &
timeout is killing the process. I don't want process to be killed.
Edit 1:
I have tried this as well. Build keeps running.
JENKINS_NODE_COOKIE=dontKillMe nohup java -jar website-status.jar &
When we execute command like this
nohup java -jar myApp.jar &
a prompt is shown saying log will be written in nohup.out file and this was causing the script to hang forever in Jenkins.
I changed the command and now no prompt and works fine, logs are written in server.log
nohup java -jar myApp.jar >> server.log 2>&1&
I am trying to run a .bat script that uninstall 32/64 bit agents and re-install 64-bit agents on almost 100 window servers.
Can you please guide me how this can be done?
I already tried running for one server to test
psexec \\<windows server> -u <username> -p <password> <command>
But fails with error
COULD NOT START PSEXESVC service on <windows server>
Do we need such service on all machines? Please suggest any other way?
PSEXEC is not a standard windows feature /service.
If you wish to use PS Exec you will need to install it on all of the relevant systems first.
Otherwise, your options are:
CMD:
A) Does the "Agent" Installer you are using allow you to install / uninstall to a given server in it's command line syntax?
If so utilize that.
B) Put the Installer in central location (the Active Directory Netlogon Folder is great for this) along with a CMD script to run the uninstall and install processes.
Then Use SCHTasks to connect to each server and create a Scheduled task that runs using an administrative Username and password for that server, and set it to run with the highest privileges, set the action to be the path to the CMD script in netlogon that you have previously tested and nwo is working, set the task to run 1 minute in the future, or on demand (If on demand you then run schtasks again to execute the task).
Powershell:
Work on getting a working CMD Script to run and use Invoke Command to run that command script on the remote systems. As in Option B from CMD options you can keep the CMD script and Agent installer in the netlogon folder.
I am using the "Send files or execute commands over SSH after the build runs" option in my Jenkins job configuration. I am running a .bat file on a remote server. The .bat file is starting an authentication server. The authentication server needs to remain up and running on the remote server.
The authentication server is delivered with a .bat file to start and stop the server. When I run the delivered .bat file my jenkins job hangs and never completes. The delivered .bat file named startAuth.bat looks like this:
call java -jar Auth.jar db migrate Auth.yml
call java -jar Auth.jar server Auth.yml
Based on some end user restrictions, I cannot modify the startAuth.bat file, so I have create another .bat file to call startAuth.bat named runStartAuth.bat. It looks like this:
cd c:\tmp
start runStartAuth.bat
exit /b
My thinking was by using "start" the .bat should be run in a separate process, one that could remain up and running until the next Jenkins job run, and the calling .bat would exit with the exit /b line. Unfortunately, the Jenkins job seems to ignore the exit and just spins and spins.
What am I doing wrong?
I'm not really an expert with Jenkins ... but I think since the slave agent JVM wrapper over your batch file knows that the child process has not yet finished, it will not return control to the executor.
Instead, can you try having the same commands on the Jenkins slave node configuration ? I believe you will have slave launcher prefix command in the Advanced section of the slave node.
I have a Spring Boot application which runs on embedded Tomcat servlet container mvn spring-boot:run . And I don’t want to deploy the project as separate war to standalone Tomcat.
Whenever I push code to BitBucket/Github, a hook runs and triggers Jenkins job (runs on Amazon EC2) to deploy the application.
The Jenkins job has a post build action: mvn spring-boot:run, the problem is that the job hangs when post build action finished.
There should be another way to do this. Any help would be appreciated.
The problem is that Jenkins doesn't handle spawning child process from builds very well. Workaround suggested by #Steve in the comment (nohuping) didn't change the behaviour in my case, but a simple workaround was to schedule app's start by using the at unix command:
> echo "mvn spring-boot:run" | at now + 1 minutes
This way Jenkins successfully completes the job without timing out.
If you end up running your application from a .jar file via java -jar app.jar be aware that Boot breaks if the .jar file is overwritten, you'll need to make sure the application is stopped before copying the artifact. If you're using ApplicationPidListener you can verify that the application is running (and stop it if it is) by adding execution of this command:
> test -f application.pid && xargs kill < application.pid || echo 'App was not running, nothing to stop'
I find very useful to first copy the artifacts to a specified area on the server to keep track of the deployed artifacts and not to start the app from the jenkins job folder. Then create a server log file there and start to listening to it on the jenkins window until the server started.
To do that I developed a small shell script that you can find here
You will also find a small article explaining how to configure the project on jenkins.
Please let me know if worked for you. Thnaks
The nohup and the at now + 1 minutes didn't work for me.
Since Jenkins was killing the process spun in the background, I ensured the process to not be killed by setting a fake BUILD_ID to that Jenkins task. This is what the Jenkins Execute shell task looks like:
BUILD_ID=do_not_kill_me
java -jar -Dserver.port=8053 /root/Deployments/my_application.war &
exit
As discussed here.
I assume you have a Jenkins-user on the server and this user is the owner of the Jenkins-service:
log in on the server as root.
run sudo visudo
add "jenkins ALL=(ALL) NOPASSWD:ALL" at the end (jenkins=your Jenkins-user)
Sign In in Jenkins and choose your jobs and click to configure
Choose "Execute Shell" in the "Post build step"
Copy and paste this:
service=myapp
if ps ax | grep -v grep | grep -v $0 | grep $service > /dev/null
then
sudo service myapp stop
sudo unlink /etc/init.d/myapp
sudo chmod +x /path/to/your/myapp.jar
sudo ln -s /path/to/your/myapp.jar /etc/init.d/myapp
sudo service myapp start
else
sudo chmod +x /path/to/your/myapp.jar
sudo ln -s /path/to/your/myapp.jar /etc/init.d/myapp
sudo service myapp start
fi
Save and run your job, the service should start automatically.
This worked for me on jenkins on a linux machine
kill -9 $(lsof -t -i:8080) || echo "Process was not running."
mvn clean compile
echo "mvn spring-boot:run" | at now + 1 minutes
In case no process on 8080 it will print the message and will continue.
Make sure that at is installed on your linux machine. You can use
sudo apt-get install at
to install at
I'd like to be able to set up a scheduled job that restarts Jenkins every night (Windows7). Is there a way to set up a job to run that will do a safeRestart programmatically that I can put in my Windows scheduler?
If the above CLI method did not work (in my case it failed on: hudson.security.AccessDeniedException2: anonymous is missing the Overall/Read permission)
You can create a Groovy plugin build step and "Execute a system Groovy script":
import hudson.model.*;
Hudson.instance.doSafeRestart(null);
or the new Jenkins instance
import jenkins.model.*
Jenkins.instance.doSafeRestart(null);
Then, you can set this job to be triggered by schedule.
For example, to restart Jenkins daily on midnight, set "Build periodically": H 00 * * *
Posting this here for others that come here from Google. You can add a job to Jenkins to restart itself using the CLI. Add a job with a shell step to execute:
java -jar "$JENKINS_HOME/war/WEB-INF/jenkins-cli.jar" -s "$JENKINS_URL" safe-restart
You can use the safe-restart command in the Jenkins CLI.
For Jenkins with LDAP enabled, Direct invocation of safe-restart command didn't work. This JENKINS JIRA helped
Login first
java -jar jenkins-cli.jar -s http://localhost:8080 login --username "$JCLIUSER" --password "$JCLIPASSWD"
Safe Restart after that
java -jar jenkins-cli.jar -s http://localhost:8080 safe-restart --username "$JCLIUSER" --password "$JCLIPASSWD"
Solution: Use Jenkins Resftul API in order to restart your jenkins service, and create some crontab task that performs it on daily basis - (On Linux/Unix/Mac versions)
, The basic flags on jenkins related to restarts are:
--restart (force restart , terminating current session)
--safeRestart (restart after all builds and jobs are completed [Graceful restart])
Example:
The task would be:
curl -x POST -u <user>:<token> <jenkins-ip>/safeRestart
For adding a crontab:
#Go to your scripts directory
cd ~/scripts
# edit new script file
sudo vim restart-jenkins-task.sh
# paste the task above ^ ...
# make the script executable
chmod +x restart-jenkins-task.sh
# add crontab task
sudo crontab -e
# paste the following: run the new script every night at 00:00 (12 am midnight)
0 0 * * * ~/scripts/restart-jenkins-task.sh
Pay Attention:
The best practice to manage global maintenance tasks is generating some automation user that deals with the application and maintenance tasks and giving him proper permissions (only users with administrator privilege can perform restart).
For Generating user access with administrator permissions (Manage Jenkins -> Manage and Assign Roles -> Assign Roles -> create user and toggle administrator permission),
For generating access token for authenticating Jenkins from commandline ( Jenkins -> Go to User -> Add new token)