Chef : run jenkins agent as a background process on a node - ruby

I'm running this command manually on console, java -jar agent.jar -jnlpUrl {{master_url}}/computer/{{slave_name}}/slave-agent.jnlp -secret {{secret_key}} > /dev/null 2>&1 &
basically it is starting the jenkins slave in background and it works fine, now when i try to do the same thing in Chef, the chef-client process hangs and gets timed out because process never goes in background and session remains active.
command = Mixlib::ShellOut.new("java -jar agent.jar -jnlpUrl {{master_url}}/computer/{{slave_name}}/slave-agent.jnlp -secret {{secret_key}}" > /dev/null 2>&1 &")
command.run_command

You really should be using a proper service layer for this, which is systemd in all modern Linux distros.

Related

springboot project building non-stop server on AWS EC2

I made java springboot project for the first time. SO need some advice.
I used gradle, war.
I git-pushed my project on AWS EC2.
and run java -jar filename.war which succeeded data communication with frontend.
But when I leave the terminal with ctrl +c , the java server stops.
Which way is the best to build non-stop server like Node pm2?
In my EC2 virtual machine, there are several projects and I use NginX to route each projects to different port numbers.
Thanks in advance!
Linux
You can use nohup:
nohup java -jar -Djava.awt.headless=true your_server.war > /path/log.out &
Later you can use:
ps aux // to get the $PID of your process
kill -9 $PID // to kill the process
Windows
Please check this answer

Run a springboot jar with Jenkins Build command

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&

start WebLogic from Jenkins

I want to stop and start WebLogic from Jenkins. If I start by shell script in Jenkins:
./startWebLogic.sh
the process is not finished, but WebLogic is started.
If I start by shell script with nohup in Jenkins:
. ./setWLSEnv.sh
nohup ./startWebLogic.sh > /dev/null &
the process is finished, but WebLogic is not started.
I tried the same script on the same server from putty, in result process is finished and WebLogic is started.
How I can start WebLogic and execute other shell script from Jenkins.
It worked for me. But this code has one issue. When you put your process in background you can't see logs of the process. And if your weblogic couldn't start at all you will know that only on the further stages (trying to deploy - in my case).
stage('Start WLS') {
steps {
sh '${DOMAIN_BIN_DIR}/startWebLogic.sh &'
}
}
Hope, someone will give a better solution with ability to determine the fact of Weblogic's fall on the running stage.

Nohup didn't work in Jenkins shell

I want my JBoss server to run in background, for that I am using nohup ./startPID.sh > /dev/null 2>&1& command. But when I pass same command in Jenkins, it doesn't work as expected. The console output in Jenkins says "command ran successfully" but in backend the JBoss server is still down.
Any inputs?
Regards
Manish Mehra
Use "at now" instead of "nohup"
In your job jenkins (execute shell) put :
set +e #so "at now" will run even if java -jar fails
#Run java app in background
echo "java -jar $(ls | grep *.jar | head -n 1)" | at now + 1 min
You could look at the JBoss management plugin
which spins up JBoss for you
This plugin allows to manage a JBoss Application Server during build
procedure.
With the plugin we can start/stop JBoss AS. It's very useful if we
need to run some integration tests against of the server. There is
also operation allows verification if artifacts are deployable.
It looks to be quite an old plugin but has cuurent users

How to deploy SpringBoot Maven application with Jenkins ?

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

Resources