0%

问题

tomcat 运行时,报如下异常:

SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/C:/Users/Jueee/.m2/repository/org/slf4j/slf4j-log4j12/1.7.21/slf4j-log4j12-1.7.21.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/C:/Users/Jueee/.m2/repository/org/apache/logging/log4j/log4j-slf4j-impl/2.13.3/log4j-slf4j-impl-2.13.3.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Detected both log4j-over-slf4j.jar AND slf4j-log4j12.jar on the class path, preempting StackOverflowError. 
SLF4J: See also http://www.slf4j.org/codes.html#log4jDelegationLoop for more details.
26-May-2021 18:44:40.531 严重 [localhost-startStop-1] org.apache.catalina.core.StandardContext.startInternal One or more listeners failed to start. Full details will be found in the appropriate container log file
26-May-2021 18:44:40.531 严重 [localhost-startStop-1] org.apache.catalina.core.StandardContext.startInternal Context [/adminWeb] startup failed due to previous errors
26-May-2021 18:44:40.545 警告 [localhost-startStop-1] org.apache.catalina.loader.WebappClassLoaderBase.clearReferencesThreads The web application [adminWeb] appears to have started a thread named [Timer-0] but has failed to stop it. This is very likely to create a memory leak. Stack trace of thread:
 java.lang.Object.wait(Native Method)
 java.lang.Object.wait(Object.java:502)
 java.util.TimerThread.mainLoop(Timer.java:526)
 java.util.TimerThread.run(Timer.java:505)
阅读全文 »

使用 os.system ()

Python 示例:

import os
val = os.system('ls -al')
print val

返回的状态码

正常输出的情况下,sh 返回的状态码是 0

$ python test1.py
total 12
drwxr-xr-x 2 jue root 4096 May 25 11:18 .
drwx------ 4 jue root 4096 May 25 11:18 ..
-rw-r--r-- 1 jue root   46 May 25 11:18 test1.py
0

没有找到时,sh 返回的状态码是 1,而适用 Python 调用,返回的是:256

import os
val = os.system('ls -al|grep jue')
print val

调用 Python

$ python test1.py
256

使用 os.popen ()

使用 commands 模块

有三个方法可以使用:

  1. commands.getstatusoutput (cmd),其以字符串的形式返回的是输出结果和状态码,即(status,output)。
  2. commands.getoutput (cmd),返回 cmd 的输出结果。
  3. commands.getstatus (file),返回 ls -l file 的执行结果字符串,调用了 getoutput,不建议使用此方法

使用 subprocess 模块

subprocess 模块允许创建很多子进程,创建的时候能指定子进程和子进程的输入、输出、错误输出管道,执行后能获取输出结果和执行状态。

  1. subprocess.run ():python3.5 中新增的函数, 执行指定的命令, 等待命令执行完成后返回一个包含执行结果的 CompletedProcess 类的实例。
  2. subprocess.call ():执行指定的命令, 返回命令执行状态, 功能类似 os.system(cmd)。
  3. subprocess.check_call ():python2.5 中新增的函数,执行指定的命令,如果执行成功则返回状态码, 否则抛出异常。

普通调用

Python 脚本示例

# -*- coding: utf-8 -*-
import datetime

if __name__=='__main__':
    today = datetime.datetime.today()
    yesterday = today - datetime.timedelta(days=1)
    keyWord = yesterday.strftime( '%Y-%m-%d' )
    print(keyWord +" 测试!")

Shell 脚本示例

#! /bin/bash


thispath=$(cd "$(dirname "$0")"; pwd)

pyfile="$thispath/test.py"
result=`python $pyfile`
echo $result >> $thispath/out.log

调用结果

$ ./runAlarm.sh
2021-05-24 测试!

带参数调用

Python 脚本示例

# -*- coding: utf-8 -*-
import sys

if __name__=='__main__':
    print("param one:"+sys.argv[1])
    print("param two:"+sys.argv[2])

运行脚本:

$ python test0.py  a b
param one:a
param two:b

Shell 脚本示例

#! /bin/bash

thispath=$(cd "$(dirname "$0")"; pwd)

pyfile="$thispath/test0.py"
result=`python $pyfile ttt ccc`
echo $result

运行结果:

param one:ttt param two:ccc

向 Shell 脚本传递参数:

#! /bin/bash

thispath=$(cd "$(dirname "$0")"; pwd)

pyfile="$thispath/test0.py"
result=`python $pyfile $1 $2`
echo $result

运行结果:

$ bash test0.sh aaa bbb
param one:aaa param two:bbb

排除特定文件

<copy todir="${build.dir}" overwrite="true">
 <fileset dir="${src.dir}">
    <exclude name="**/*.java" /> <!-- 排除所有java文件 -->
    <exclude name="helloworld/" /> <!-- 排除根目录下整个helloworld文件夹 -->
    <exclude name="**/test/" /> <!-- 排除test子目录本身及其子目录下的所有文件夹 -->
  </fileset>
</copy>