记录笔记<java调用shell脚本,shell脚本调用python> 传参

记录笔记<java调用shell脚本,shell脚本调用python> 传参packagecom hansight atom custom abilityPower util importorg apache commons lang ArrayUtils importorg slf4j Logger importorg slf4j LoggerFactor importjava io InputStreamR importjava io LineNumberRe publicclassJ pr

package com.hansight.atom.custom.abilityPower.util; import org.apache.commons.lang.ArrayUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.InputStreamReader; import java.io.LineNumberReader; public class JaveShellUtil { 
    private static final Logger logger = LoggerFactory.getLogger(JaveShellUtil.class); public static int ExecCommand(String command) { 
    int retCode = 0; try { 
    ProcessBuilder builder1 = new ProcessBuilder("/bin/chmod", "755", AbilityPowerConfigReader.shellPath); Process process1 = builder1.start(); process1.waitFor(); Process process = Runtime.getRuntime().exec(new String[] { 
    "/bin/sh", "-c", command+" 1234,5678"+" taskid"}, null, null); retCode = process.waitFor(); ExecOutput(process); logger.info("能力上台调用告警补全shell脚本成功》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》"); } catch (Exception e) { 
    retCode = -1; e.printStackTrace(); logger.error("能力上台调用告警补全shell脚本报错:::",e); } return retCode; } public static boolean ExecOutput(Process process) throws Exception { 
    if (process == null) { 
    return false; } else { 
    InputStreamReader ir = new InputStreamReader(process.getInputStream()); LineNumberReader input = new LineNumberReader(ir); String line; String output = ""; while ((line = input.readLine()) != null) { 
    output += line + "\n"; } input.close(); ir.close(); if (output.length() > 0) { 
    System.out.println(output); } } return true; } } 

shell脚本

#!/usr/bin/env bash : ' Date: 2019-06-09 14:11:00 Description: 补全告警 ' indexs=$1 taskId=$2 source "$( cd $(dirname $0) pwd )/env.sh" PYTHON_COMMAND="${CONDA_ENV_HOME}/bin/python" ( cd ${ 
   PROGRAM_ENTRY_PATH} echo ${ 
   indexs} echo ${ 
   taskId} if [ `whoami` == "root" ]; then su soar -c "${PYTHON_COMMAND} main_domain_ability.py $indexs $taskId>> ${LOG_HOME}/alarm_completion_industry_ability.log" elif [ `whoami` == "soar" ]; then ${ 
   PYTHON_COMMAND} main_domain_ability.py $indexs $taskId>> ${ 
   LOG_HOME}/alarm_completion_industry_ability.log else echo `whoami` + "用户没有执行当前脚本权限,请切换至root或soar用户执行!" fi ) 

python脚本

#!/usr/bin/env python3 # -*- encoding:utf-8 -*- """ @Filename: main.py @Time: 5/11/19 12:12 AM @Description: 补全告警 """ import time import sys indexs = sys.argv[1] taskId = sys.argv[2] from zyxa.service.tip_completion_alarm_ability import TipCompletionAlarmByAbility from zyxa.utils.logger import Logger log = Logger("main_domain_ability").get_logger() if __name__ == "__main__": try: log.info("shell传过来的参数》》》》》》》》》》》》》》》》》》》》》》::"+indexs+"::::::::::::::::::::taskId::::::::::::::::::::::"+taskId) t1 = str(round(time.time() * 1000)) TipCompletionAlarmByAbility().query_alarm(indexs,taskId) t2 = str(round(time.time() * 1000)) except Exception as e: log.error("能力上台补全告警异常,异常信息如下:", e) 
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/226627.html原文链接:https://javaforall.net

(0)
上一篇 2026年3月16日 下午10:29
下一篇 2026年3月16日 下午10:30


相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号