Jenkins fails to execute nohup java -jar package using shell script

This article is an original article by joshua317, please indicate when reprinting: Reprinted from joshua317 blog  jenkins uses shell script to execute nohup java -jar package failed- joshua317's blog

1. Problems

When the shell script is executed through jenkins, the script is started by nohup java -jar &, which shows that the execution is successful, but the service is not started. The script is as follows:

#! /bin/bash
nohup java -Xms800m -Xmx800m -XX:PermSize=256m -XX:MaxPermSize=512m -XX:MaxNewSize=512m -jar /usr/local/joshua317-test-core-1.0-SNAPSHOT.jar &

That is to say, when the Jenkins is built and pushed to the business server, when the script is executed, the nohup command in the script cannot exit normally, and the build foreground task is stuck, or nohup is always invalid.

Two, investigation

1. Execute the script directly on the business server, and it can run normally, indicating that there is no problem with the script. However, once it is released in conjunction with jenkins, although jenkins shows success, the shell script is not executed.

2. Find a simple script to test

#! /bin/bash
echo "hello" >> /usr/local/test.txt

There is content in /usr/local/test.txt, indicating that the script can be executed successfully, and it also indicates that there is a problem when Jenkins executes the script with nohup. That is to say, if the build command contains  nohup xxx & the trigger build and the build is successful, although there is no problem and no error reported in the output of the build task, the shell script is not executed.

Three, the reason

By default, jenkins will kill the spawned process triggered by the shell command during the build process after the build is complete.

Jenkins identifies whether a process is a derived process based on the BUILD_ID. Therefore, after modifying the BUILD_ID, jenkins cannot identify whether it is a derived process, and the process can keep running in the background.

The conclusion is that the Jenkins program is only responsible for running the pseudo command line nuhup command, and does not guarantee whether the command after nuhup is successfully run.

Four, solve

(1) Use  BUILD_ID=xxx , where xxx can be any content (as long as it is not the original BUILD_ID content), and the nohup output content needs to be redirected to the file, such as "/usr/local/nohup.out"

#! /bin/bash
BUILD_ID=dontKillMe
nohup java -Xms800m -Xmx800m -XX:PermSize=256m -XX:MaxPermSize=512m -XX:MaxNewSize=512m -jar /usr/local/joshua317-test-core-1.0-SNAPSHOT.jar  > /usr/local/nohup.out 2>&1 &

(2) Use at now instead of nohup command.

#! /bin/bash
#so "at now" will run even if java -jar fails
set +e 
#Run java app in background
echo "java -jar /usr/local/joshua317-test-core-1.0-SNAPSHOT.jar" | at now

set -e: If the return value is non-zero during execution, the entire script will exit immediately

set +e: If the return value is non-zero during execution, the following script will continue to be executed

5. Expansion

1. When executing shell scripts in Jenkins, pay attention to using the full path

/bin/sh /usr/local/service/start.sh

2. The first line of the shell script file should be declared as a shell script

#! /bin/bash
....

3. When using jenkins to execute the shell script, if there is a nohup command, it needs to be added BUILD_ID=dontKillMeafter nohup

Need to add 2>&1 &

Note: BUILD_ID= can be followed by other content, such as BUILD_ID=joshua317

#! /bin/bash
BUILD_ID=dontKillMe
nohup java -Xms800m -Xmx800m -XX:PermSize=256m -XX:MaxPermSize=512m -XX:MaxNewSize=512m -jar /usr/local/joshua317-test-core-1.0-SNAPSHOT.jar  > /usr/local/nohup.out 2>&1 &

4. If you encounter it, [Build step 'Send files or execute commands over SSH' changed build result to UNSTABLE] it is basically because there is a problem with the shell script.

5. About the instructions added after nohup

(1) If there is no "&" at the end of the command, it will become "java -jar xxx.jar", which means that in the current shell window, you can press CTRL + C to interrupt the program running, or close the window directly, and the program will exit directly. Add at the end of the command "&", it becomes "java -jar xxx.jar &", which means that the program will stop running when the window is closed. & stands for running the command in the background.

java -jar /usr/local/joshua317-test-core-1.0-SNAPSHOT.jar &

(2) The "nohup java -jar xxx.jar &" part of the command means that the running command will not be hung up. When the account is logged out or the terminal is closed, the program will still run. Note that all output from this job is redirected to the nohup.out file.

The part of the command "nohup java -jar xxx.jar > nohup.out &" means to run the command without hanging up. When the account exits or the terminal is closed, the program still runs, and all the output of the job is redirected to nohup.out in the file. "> nohup.out" This command is to specify the log output file. ">>" indicates that the output will be redirected to nohup.out by appending.

nohup java -jar /usr/local/joshua317-test-core-1.0-SNAPSHOT.jar > /usr/local/nohup.out &

#或者
nohup java -jar /usr/local/joshua317-test-core-1.0-SNAPSHOT.jar >> /usr/local/nohup.out &

(3) Standard input file (stdin): The file descriptor of stdin is 0, and Unix programs read data from stdin by default. Standard output file (stdout): The file descriptor of stdout is 1, and Unix programs output data to stdout by default. Standard error file (stderr): The file descriptor of stderr is 2, and the Unix program will write error information to the stderr stream. Mask output to prevent output: /dev/null is a special file, and the content written to it will be discarded; if you try to read from this file, you will not be able to read anything. But the /dev/null file is very useful. Redirecting the output of a command to it will have the effect of "disabling output". "> /usr/local/nohup.out 2>&1": means combine stdout and stderr and redirect to "/usr/local/nohup.out".

nohup java -jar /usr/local/joshua317-test-core-1.0-SNAPSHOT.jar > /usr/local/nohup.out 2>&1 &
或者
nohup java -jar /usr/local/joshua317-test-core-1.0-SNAPSHOT.jar >> /usr/local/nohup.out 2>&1 &
或者
nohup java -jar /usr/local/joshua317-test-core-1.0-SNAPSHOT.jar > /dev/null 2>&1 &

6. The use of at one-time scheduled tasks of linux

Using at can only perform one-time tasks; using the at command requires the atd process to be started.

(1) Install the at command:

Check whether the atd process is enabled:

ps -ef | grep atd

If there is no need to install the at command

yum -y install at

(2) Set the automatic start at command

chkconfig --level 35 atd on

(3) Start the atd process

service atd start

If after using the at command, the following error is reported:

Can't open /var/run/atd.pid to signal atd. No atd running?It means that the atd process is not running, and you need to execute the command to start the atd process:service atd start

(4) Easy to use

#1.显示版本信息
at -V
#2.查看延时任务列表
at -l 
#3.查看任务内容
at -c 任务号
#4.删除任务
at -r 任务号
#5. now +时间,时间以minutes、hours、days或weeks为单位,比如“now +5 days”代表命令在5天之后的此时此刻执行
#(1)立即执行
at now
#(2)一分钟后执行
at now +1min
#(3)五天后执行
at now +5 days
#(4)设置5天后晚上00:00执行
at 00:00 +5 days
#(5)设置在2022年7月20号凌晨01:00执行
at 01::00 2022-7-20

#6.管道方式执行一次任务
echo hello>/usr/local/at.txt|at now

This article is an original article by joshua317, please indicate when reprinting: Reprinted from joshua317 blog  jenkins uses shell script to execute nohup java -jar package failed- joshua317's blog

Guess you like

Origin blog.csdn.net/joshua317/article/details/125871391