You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Genexus Shell函数在Ubuntu20.04/Tomcat9/Java11环境下执行.sh脚本失败

排查步骤与解决方案

针对你遇到的Genexus Shell函数返回1、脚本未执行的问题,结合Tomcat9/Ubuntu20.04/Java11的环境特性,按以下步骤排查:

  • 强制指定脚本解释器并使用绝对命令
    Ubuntu20.04默认的/bin/sh是Dash,和Bash语法存在差异,且Tomcat服务的环境变量PATH可能不包含常用命令路径。

    1. 确保脚本第一行写死Bash路径:#!/bin/bash
    2. 脚本内所有命令用绝对路径,比如创建目录写成/usr/bin/mkdir /目标路径/DirTest
  • 确认执行上下文的工作目录
    Genexus调用Shell时的工作目录可能不是脚本所在目录,若脚本依赖相对路径会失败。

    • 可以在脚本开头添加切换目录的代码:
      cd "$(dirname "$0")" || exit 1
      
    • 或者在Genexus代码中先切换到脚本所在目录再执行:
      &currentDir = Directory.Current()
      Directory.Change('/home/adminap/tomcat9/webapps/proto01/')
      &execute = shell(&path,1)
      Directory.Change(&currentDir)
      
  • 捕获错误输出定位具体问题
    返回码1仅表示执行失败,但看不到具体原因。修改Genexus代码捕获错误输出:

    &path = '/home/adminap/tomcat9/webapps/proto01/test.sh'
    &output = ''
    &errorOutput = ''
    &execute = shell(&path, 1, &output, &errorOutput)
    // 将&errorOutput写入日志或输出到页面,查看具体错误
    
  • 检查Java安全策略限制
    Java11和Tomcat9的安全策略比旧版本更严格,可能限制了外部进程执行权限。
    编辑Tomcat的catalina.policy文件,添加以下权限规则:

    grant codeBase "file:/home/adminap/tomcat9/webapps/proto01/-" {
        permission java.io.FilePermission "/home/adminap/tomcat9/webapps/proto01/test.sh", "execute";
        permission java.lang.RuntimePermission "exec";
    };
    

    保存后重启Tomcat服务。

  • 排查AppArmor的限制
    Ubuntu20.04默认启用AppArmor,可能阻止Tomcat执行外部脚本。临时关闭AppArmor对Tomcat的限制测试:

    sudo aa-complain /etc/apparmor.d/usr.sbin.tomcat9
    

    如果测试通过,再调整AppArmor规则,永久允许Tomcat执行该脚本。

内容的提问来源于stack exchange,提问作者Jose Martinez

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.15 04:01:16