简体   繁体   English

Jenkins运行的Shell脚本创建了非终止过程

[英]Shell script run by Jenkins creates non-terminating process

I'm trying to use Jenkins to create a special git repository. 我正在尝试使用Jenkins创建一个特殊的git存储库。 I created a free-style project that just executes a shell script. 我创建了一个仅执行shell脚本的自由风格项目。 When I execute this script by hand, without Jenkins, it works just fine. 当我在没有詹金斯的情况下手动执行此脚本时,它就可以正常工作。 From Jenkins, however, it behaves quite differently. 但是,它与詹金斯的行为大相径庭。

# this will remove all subtrees
git log | grep git-subtree-dir | tr -d ' ' | cut -d ":" -f2 | sort | uniq | xargs -I {} bash -c 'if [ -d $(git rev-parse --show-toplevel)/{} ] ; then rm -rf {}; fi'

rm -rf .git

If this part is executed by Jenkins, in console output I see this kind of errors: 如果这部分是由詹金斯执行的,则在控制台输出中,我会看到这种错误:

rm: cannot remove '.git/objects/pack/pack-022eb85d38a41e66ad3f43a5f28809a5a3ee4a0f.pack': Device or resource busy
rm: cannot remove '.git/objects/pack/pack-05630eb059838f149ad30483bd48d37f9a629c70.pack': Device or resource busy
rm: cannot remove '.git/objects/pack/pack-26f510b5a2d15ba9372cf0a89628d743811e3bb2.pack': Device or resource busy
rm: cannot remove '.git/objects/pack/pack-33d276d82226c201eedd419e5fd24b6b906d4c03.pack': Device or resource busy

I modified this part of the script like this: 我这样修改了脚本的这一部分:

while true
do
    if rm -rf .git ; then
        break
    else
        continue
    fi
done

But this doesn't help. 但这无济于事。 In the task manager I see a git process that just doesn't terminate. 在任务管理器中,我看到一个git进程并没有终止。 I conjured said script by a lot of googling and I do not understand very good what's going on. 我通过大量的谷歌搜索联想了所说的脚本,但我不太了解发生了什么。

Jenkins runs on Windows Server 2012 behind IIS; Jenkins在IIS之后的Windows Server 2012上运行; shell scripts are executed by bash shipped with git for Windows. Shell脚本由git随Windows一起提供的bash执行。

1/ Ensure your path is correct, and no quote/double quote escaping occurs in the process of jenkins job starting. 1 /确保您的路径正确,并且在jenkins作业启动过程中没有出现引号/双引号转义。

2/ Your command line is a bit too handy to be correctly and safely evaluated. 2 /您的命令行有点方便,无法正确,安全地进行评估。 Put your commands in a regular script, starting with #!/bin/bash instead of thru the command line. 将您的命令放在常规脚本中,以#!/bin/bash开头,而不是通过命令行。

xargs -I {} bash -c 'if [ -d $(git rev-parse --show-toplevel)/{} ] ; then rm -rf {}; fi'

becames 成为

xargs -I {} /path/myscript.sh {}

with

#!/bin/bash

rev-parse="$(git rev-parse --show-toplevel)"
wait

if [ -d ${rev-parse}/${1} ] ; then 
   rm -rf ${1}
fi

Please note that your script is really unsafe, as you rm -rf a parameter without even evaluate it before… ! 请注意,您的脚本确实是不安全的,因为您对rm -rf一个参数进行了评估,甚至在…之前都不对其进行评估!

3/ You can add a wait between the git and the rm to wait for the end of the git process 3 /您可以在gitrm之间添加一个wait ,以等待git进程结束

4/ log your git command into a log file, with a redirection >> /tmp/git-jenkins-log 4 /将您的git命令记录到日志文件中,并带有重定向>> /tmp/git-jenkins-log

5/ put all of those commands in a script (see #2) 5 /将所有这些命令放在脚本中(请参阅#2)

Following is an infinite loop in case rm -rf fail 以下是在rm -rf失败的情况下的无限循环

while true
do
    if rm -rf .git ; then
        break
    else
        continue
    fi
done

indeed continue can be used in for or while loop to get the next entry but in this while loop it will run the same rm command forever. 实际上,可以在forwhile循环中使用continue获取下一个条目,但是在此while循环中,它将永远运行相同的rm命令。

Well, aparrently I was able to fix my issue by running the script from different user. 好吧,另外,我能够通过从其他用户运行脚本来解决问题。 By default on Windows Jenkins executes all jobs from the user SYSTEM. 在Windows上,默认情况下,Jenkins从用户SYSTEM执行所有作业。 I have no idea why it affects the behaviour of my script but running it with psexec from specially created user account worked. 我不知道为什么它会影响脚本的行为,但是可以通过专门创建的用户帐户使用psexec运行它。

In case anyoune is interested, I did something like this: 万一有兴趣的话,我可以这样做:

psexec -accepteula -h -user Jenkins -p _password_ "full/path/to/bash.exe" full/path/to/script.sh

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM