如何从Bash向WLST-Jython脚本正确传递多服务名列表参数
解决方案
问题根源
你当前的报错由4个核心问题导致:
- Bash传递带空格的逗号分隔参数时未加引号,被shell拆分为多个独立参数,导致
sys.argv[4]仅能读取到第一个服务名 - 直接对字符串类型的服务名参数遍历,循环逻辑按单个字符迭代,才会出现打印服务名为
s的报错 - 主逻辑未调用
connectAdmin()方法,未连接到域就操作MBean,触发「No domain or domain template has been read」报错 - 主模块判断语法错误,且缺少WLST编辑域必需的编辑开启、保存激活逻辑
修正步骤
1. Bash侧传参修正
传递逗号分隔的多服务名参数时,必须用单引号整体包裹,避免shell拆分参数:
# 正确传参格式 ./serverArgUpdate.sh myAdminServer 40100 'server_MS1, server_MS2' -Dweblogic.logs2=debug
2. WLST脚本侧修正
以下是修正后的完整可运行代码:
import sys ### Domain Properties #### adminURL = sys.argv[1] domainUserName = sys.argv[2] domainPassword = sys.argv[3] # 按逗号分割参数字符串,去除每个服务名前后空格后转成元组 svrName = tuple(s.strip() for s in sys.argv[4].split(',')) print("The server names are ", svrName) setarg = sys.argv[5] ############### Connecting to AdminServer ################################# def connectAdmin() : try: connect(userConfigFile=domainUserName,userKeyFile=domainPassword,url=adminURL) except: banner('Unable to find admin server..') exit() def banner(line): print('='*80) print(line) print('='*80) ######################################################################## def updateSvrArg(): for i in svrName: cd('/Servers/'+i+'/ServerStart/'+i) # 这里补充你原来的参数设置逻辑 set('Arguments', setarg) ############### Main Script ##################################### if __name__ == "__main__": try: # 先连接域 connectAdmin() # 开启编辑模式 edit() startEdit() updateSvrArg() # 保存并激活配置修改 save() activate(block='true') disconnect() except: dumpStack() banner("There was an error with the script: Check the properties file or check if Someone has the lock/edit on the console") cancelEdit(defaultAnswer='y') sys.exit(1)
内容的提问来源于stack exchange,提问作者weblogicGuy
相关产品推荐
相关产品推荐

