Kaynağa Gözat

脚本,输入输出

xueli.xue 8 yıl önce
ebeveyn
işleme
b30fceb46d

+ 5 - 3
xxl-job-admin/src/main/webapp/WEB-INF/template/jobinfo/jobinfo.index.ftl

@@ -192,16 +192,18 @@ public class DemoGlueJobHandler extends IJobHandler {
 </textarea>
 <textarea class="glueSource_shell" style="display:none;" >
 #!/bin/bash
-
 echo "xxl-job: hello shell"
 
-for item in 1 2 3
+echo "脚本位置:$0"
+echo "参数数量:$#"
+for param in $*
 do
-    echo "shell : $item"
+    echo "参数 : $param"
     sleep 1s
 done
 
 echo "Good bye!"
+exit 0
 </textarea>
 <textarea class="glueSource_python" style="display:none;" >
 #!/usr/bin/python

+ 1 - 1
xxl-job-admin/src/main/webapp/static/js/jobinfo.index.1.js

@@ -304,7 +304,7 @@ $(function() {
 	});
 
 
-    // GLUE模式开启
+    // 运行模式
     $(".glueType").change(function(){
 		// executorHandler
         var $executorHandler = $(this).parents("form").find("input[name='executorHandler']");

+ 3 - 2
xxl-job-core/src/main/java/com/xxl/job/core/handler/impl/ScriptJobHandler.java

@@ -48,8 +48,9 @@ public class ScriptJobHandler extends IJobHandler {
         String logFileName = XxlJobFileAppender.filePath.concat(XxlJobFileAppender.contextHolder.get());
 
         // invoke
-        ScriptUtil.execToFile(cmd, scriptFileName, logFileName);
-        return ReturnT.SUCCESS;
+        int exitValue = ScriptUtil.execToFile(cmd, scriptFileName, logFileName, params);
+        ReturnT<String> result = (exitValue==0)?ReturnT.SUCCESS:new ReturnT<String>(ReturnT.FAIL_CODE, "script exit value("+exitValue+") is failed");
+        return result;
     }
 
 }

+ 9 - 2
xxl-job-core/src/main/java/com/xxl/job/core/util/ScriptUtil.java

@@ -65,8 +65,11 @@ public class ScriptUtil {
      * @param command
      * @param scriptFile
      * @param logFile
+     * @param params
+     * @return
+     * @throws IOException
      */
-    public static void execToFile(String command, String scriptFile, String logFile) throws IOException {
+    public static int execToFile(String command, String scriptFile, String logFile, String... params) throws IOException {
         // 标准输出:print (null if watchdog timeout)
         // 错误输出:logging + 异常 (still exists if watchdog timeout)
         // 标准输入
@@ -76,12 +79,16 @@ public class ScriptUtil {
         // command
         CommandLine commandline = new CommandLine(command);
         commandline.addArgument(scriptFile);
+        if (params!=null && params.length>0) {
+            commandline.addArguments(params);
+        }
 
         // exec
         DefaultExecutor exec = new DefaultExecutor();
         exec.setExitValues(null);
         exec.setStreamHandler(streamHandler);
-        int exitValue = exec.execute(commandline);
+        int exitValue = exec.execute(commandline);  // exit code: 0=success, 1/-1=fail
+        return exitValue;
     }
 
 }