shell执行的时候报错,请问大佬们有么有解决的?我的xxl-job是docker容器,进去看了文件是存在的!

gjmwrych  于 2021-11-27  发布在  Java
关注(0)|答案(10)|浏览(698)

2020-12-09 19:36:24 [com.xxl.job.core.thread.JobThread#run]-[124]-[Thread-32]
----------- xxl-job job execute start -----------
----------- Param:
2020-12-09 19:36:24 [com.xxl.job.core.handler.impl.ScriptJobHandler#execute]-[81]-[Thread-32] ----------- script file:/data/applogs/xxl-job/jobhandler/gluesource/4_1607511923000.sh -----------
2020-12-09 19:36:24 [com.xxl.job.core.util.ScriptUtil#execToFile]-[112]-[Thread-32] java.io.IOException: Cannot run program "bash": error=2, No such file or directory
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1048)
at java.lang.Runtime.exec(Runtime.java:620)
at java.lang.Runtime.exec(Runtime.java:485)
at com.xxl.job.core.util.ScriptUtil.execToFile(ScriptUtil.java:76)
at com.xxl.job.core.handler.impl.ScriptJobHandler.execute(ScriptJobHandler.java:82)
at com.xxl.job.core.thread.JobThread.run(JobThread.java:152)
Caused by: java.io.IOException: error=2, No such file or directory
at java.lang.UNIXProcess.forkAndExec(Native Method)
at java.lang.UNIXProcess.(UNIXProcess.java:247)
at java.lang.ProcessImpl.start(ProcessImpl.java:134)
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1029)
... 5 more

2020-12-09 19:36:24 [com.xxl.job.core.thread.JobThread#run]-[164]-[Thread-32]
----------- xxl-job job execute end(finish) -----------
----------- ReturnT:ReturnT [code=500, msg=script exit value(-1) is failed, content=null]
2020-12-09 19:36:24 [com.xxl.job.core.thread.TriggerCallbackThread#callbackLog]-[190]-[xxl-job, executor TriggerCallbackThread]
----------- xxl-job job callback finish.

vjhs03f7

vjhs03f71#

把你脚本的


# ! /bin/bash

改为


# ! /bin/sh

相关语法也做相应调整,猜测你的容器是基于alpine,默认没有bash

bprjcwpo

bprjcwpo2#

1. 我的脚本修改为: 之前是【#! /bin/bash】,执行后依然包错,下面详细说明下请款

#! /bin/sh
echo "xxl-job: hello shell"
exit 0

2. 容器中确实有这个文件

cat /data/applogs/xxl-job/jobhandler/gluesource/4_1607523500000.sh
#! /bin/sh
echo "xxl-job: hello shell"
exit 0/bin #

3. 手动去执行可以sh /data/applogs/xxl-job/jobhandler/gluesource/4_1607523500000.sh

sh /data/applogs/xxl-job/jobhandler/gluesource/4_1607523500000.sh
xxl-job: hello shell

4. 日志依然报错

2020-12-09 21:15:19 [com.xxl.job.core.thread.JobThread#run]-[124]-[Thread-14]
----------- xxl-job job execute start -----------
----------- Param:
2020-12-09 21:15:19 [com.xxl.job.core.handler.impl.ScriptJobHandler#execute]-[81]-[Thread-14] ----------- script file:/data/applogs/xxl-job/jobhandler/gluesource/7_1607519622000.sh -----------
xxl-job: hello shell
脚本位置:/data/applogs/xxl-job/jobhandler/gluesource/7_1607519622000.sh
任务参数:
分片序号 = 0
分片总数 = 1
111
mkdir: 无法创建目录"test": 文件已存在
222
Good bye!
2020-12-09 21:15:19 [com.xxl.job.core.thread.JobThread#run]-[164]-[Thread-14]
----------- xxl-job job execute end(finish) -----------
----------- ReturnT:ReturnT [code=200, msg=null, content=null]
2020-12-09 21:15:19 [com.xxl.job.core.thread.TriggerCallbackThread#callbackLog]-[190]-[xxl-job, executor TriggerCallbackThread]
----------- xxl-job job callback finish.

[Load Log Finish]

5. 我重新把xxl-job单独部署一下,没有在容器中,直接java -jar xxl-job-executor-sample-springboot-2.1.2.jar 启动就可以正常使用了。部署在centos7上。

不管使用#! /bin/sh还是#! /bin/bash均可以正常执行,下面是正确执行的日志。

2020-12-09 22:25:57 [com.xxl.job.core.thread.JobThread#run]-[124]-[Thread-9]
----------- xxl-job job execute start -----------
----------- Param:
2020-12-09 22:25:57 [com.xxl.job.core.handler.impl.ScriptJobHandler#execute]-[81]-[Thread-9] ----------- script file:/data/applogs/xxl-job/jobhandler/gluesource/7_1607523844000.sh -----------
xxl-job: hello shell
脚本位置:/data/applogs/xxl-job/jobhandler/gluesource/7_1607523844000.sh
任务参数:
分片序号 = 0
分片总数 = 1
111
222
/usr/local/xxl
Good bye!
2020-12-09 22:25:58 [com.xxl.job.core.thread.JobThread#run]-[164]-[Thread-9]
----------- xxl-job job execute end(finish) -----------
----------- ReturnT:ReturnT [code=200, msg=null, content=null]
2020-12-09 22:25:58 [com.xxl.job.core.thread.TriggerCallbackThread#callbackLog]-[190]-[xxl-job, executor TriggerCallbackThread]
----------- xxl-job job callback finish.

[Load Log Finish]
6. 那么我使用容器技术,是使用k8s技术,部署在pod中的。怎么才能正常访问,虽然单独部署也可以解决,但是有割裂感!还是想在k8s的容器内部解决这个问题,不知道同行有什么其他的启发或者思路!万分感谢了。

hlswsv35

hlswsv353#

这不是已经执行到脚本了么,这是其他错误,文件已存在;光看报错不分析错误的吗?

brvekthn

brvekthn4#

不好意思,昨天截取的错误日志,我放上去的第四条中的日志不是访问k8s的pod报的,是我复制错了,这个错是我单独部署的xxl-job执行器报的,原因是我跑了两次,第一次通了,第二次提示文件已存在。(这个事告一段落,说我复制错了,但是k8s依然不通)
下面说说k8s,他错依然存在,造成误解,不好意思。

我把我的请款再详细说下:

1. 页面配置GLUE如下:

#! /bin/sh
echo "xxl-job: hello shell"
sh /test.sh
exit 0

2.执行一次,日志如下:

2020-12-10 19:56:37 [com.xxl.job.core.thread.JobThread#run]-[124]-[Thread-38]
----------- xxl-job job execute start -----------
----------- Param:
2020-12-10 19:56:37 [com.xxl.job.core.handler.impl.ScriptJobHandler#execute]-[81]-[Thread-38] ----------- script file:/data/applogs/xxl-job/jobhandler/gluesource/4_1607601297000.sh -----------
2020-12-10 19:56:37 [com.xxl.job.core.util.ScriptUtil#execToFile]-[112]-[Thread-38] java.io.IOException: Cannot run program "bash": error=2, No such file or directory
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1048)
at java.lang.Runtime.exec(Runtime.java:620)
at java.lang.Runtime.exec(Runtime.java:485)
at com.xxl.job.core.util.ScriptUtil.execToFile(ScriptUtil.java:76)
at com.xxl.job.core.handler.impl.ScriptJobHandler.execute(ScriptJobHandler.java:82)
at com.xxl.job.core.thread.JobThread.run(JobThread.java:152)
Caused by: java.io.IOException: error=2, No such file or directory
at java.lang.UNIXProcess.forkAndExec(Native Method)
at java.lang.UNIXProcess.(UNIXProcess.java:247)
at java.lang.ProcessImpl.start(ProcessImpl.java:134)
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1029)
... 5 more

2020-12-10 19:56:37 [com.xxl.job.core.thread.JobThread#run]-[164]-[Thread-38]
----------- xxl-job job execute end(finish) -----------
----------- ReturnT:ReturnT [code=500, msg=script exit value(-1) is failed, content=null]
2020-12-10 19:56:37 [com.xxl.job.core.thread.TriggerCallbackThread#callbackLog]-[190]-[xxl-job, executor TriggerCallbackThread]
----------- xxl-job job callback finish.

[Load Log Finish]

3. 去k8s的pod中看/data/applogs/xxl-job/jobhandler/gluesource/4_1607601297000.sh内容如下:确实存在

/ # cat /data/applogs/xxl-job/jobhandler/gluesource/4_1607601297000.sh
#! /bin/sh
echo "xxl-job: hello shell"
sh /test.sh

4. /test.sh内容如下,没有执行到这里

/ # cat /test.sh
echo "111"
mkdir test
echo "222"

yqyhoc1h

yqyhoc1h5#

刚才查看了源码,shell脚本是以bash运行的

bash xxx.sh

应该是最初设计没有过多考虑在容器执行脚本的场景
如果你只在容器执行,那可以自行修改下xxl-job-core中的代码试试,我个人没做测试

com.xxl.job.core.glue.GlueTypeEnum

GLUE_SHELL("GLUE(Shell)", true, "bash", ".sh"),

修改为

GLUE_SHELL("GLUE(Shell)", true, "sh", ".sh"),

另外我还有个想法,在不改动代码的情况下,修改执行器构建容器的Dockerfile,增加一句

RUN ln -s /bin/sh /bin/bash

仅仅是个人想法,不知道可不可行,你如果愿意可以试试

nbysray5

nbysray56#

遇到了一样的问题,大佬有解决办法了吗?

mec1mxoz

mec1mxoz7#

应该是你容器没有shell,或者将com.xxl.job.core.glue.GlueTypeEnum 中GLUE_SHELL("GLUE(Shell)", true, "bash", ".sh"), 改为GLUE_SHELL("GLUE(Shell)", true, "/bin/sh", ".sh"),

k7fdbhmy

k7fdbhmy8#

同遇到该问题,请问解决了吗

ee7vknir

ee7vknir9#

亲测这种方法可用

ifsvaxew

ifsvaxew10#

类似的问题,不过我没有使用doker,我扩展了 groovy 脚本,
使用 Jenkins 发布执行器启动后, Runtime 执行 groovy 命令 就会提示: Cannot run program “groovy”
当我登录服务器手动重启执行器,再次执行,就时正常的

我猜测是自动化部署的时候,没有加载到环境变量
有两种猜想方案:

  1. 部署脚本中调用 source ~/.bashrc
  2. 修改源码,将GlueType 的命令改为 软链

相关问题