Jenkins 构建后执行 nohup 脚本,前台不退出解决

2,606次阅读

共计 783 个字符,预计需要花费 2 分钟才能阅读完成。

脚本内容

#!/bin/bash
cat /dev/null > nohup.out
NUM=netstat -tunlp | grep 8000 | wc -l
if [ ${NUM} -eq 0 ];then
echo "Service not start.starting......."
    nohup python36 /data/webPage/manage.py runserver 0.0.0.0:8000 &
else
    echo "Service already run on 0.0.0.0:8000"
fi

脚本内容很简单,就是检测 8000 端口是否监听,没有监听的话尝试启动服务,监听的话就正常退出即可。

问题

Jenkins 构建拉取代码后,将代码通过 Publish Vver SSH 插件推送到业务机器,然后执行脚本时,脚本中的 nohup 命令无法正常退出,导致构建任务前台卡住。如图:

Jenkins 构建后执行 nohup 脚本,前台不退出解决

解决

查询后发现:

Since you are executing a script from a non-TTY environment; The Jenkins is not able to get the exit properly, out of your script.What you want is to exit immediately, after script execution! Don’t want to wait for the entire timeout to happen and then disconnect improperly!

意为:

从一个非 tty 环境执行脚本;Jenkins 不能正常从你的脚本中退出

[v_notice]解决方案:在PTY中使用Exec[/v_notice]

Jenkins 构建后执行 nohup 脚本,前台不退出解决

Jenkins 构建后执行 nohup 脚本,前台不退出解决

虽然构建完成了,但是服务却没有启动,可能的原因是因为,在执行openapi start 命令时,刚启动,pty(伪终端)就断开连接,

解决的办法就是,加 nohup 让脚步在后台运行,也就即使伪终端断开了,项目依然可以启动完成。

Jenkins 构建后执行 nohup 脚本,前台不退出解决

 

 

 

 

正文完
 
mervinwang
版权声明:本站原创文章,由 mervinwang 2019-01-10发表,共计783字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
文章搜索