[英]How to check the status of bash shell script while executing from Python script?
I have a simple Python script which will execute a shell script using subprocess
module in Python. 我有一个简单的Python脚本,它将使用Python中的
subprocess
模块执行Shell脚本。
Below is my Python shell script which is calling testing.sh
shell script and it works fine. 下面是我的Python shell脚本,它正在调用
testing.sh
shell脚本,并且工作正常。
import os
import json
import subprocess
jsonData = '{"pp": [0,3,5,7,9]}'
jj = json.loads(jsonData)
os.putenv( 'jj3', ' '.join( str(v) for v in jj['pp'] ) )
print "start"
proc = subprocess.Popen('testing.sh', stdout=subprocess.PIPE, stderr=subprocess.PIPE)
(stdout, stderr) = proc.communicate()
if stderr:
print "Shell script gave some error"
print stderr
else:
print stdout
print "end" # Shell script ran fine.
And below is my testing.sh
shell script - 下面是我的
testing.sh
shell脚本-
#!/bin/bash
dir1=some_directory
dir2=some_directory
length1=some_number
length2=some_number
if [ "$dir1" = "$dir2" ] && [ "$length1" -gt 0 ] && [ "$length2" -gt 0 ]
then
for el in $jj3
do
scp david@machineB:/data/be_t1_snapshot/20131215/t1_"$el"_5.data /data01/primary/. || scp david@machineC:/data/be_t1_snapshot/20131215/t1_"$el"_5.data /data01/primary/.
done
fi
What my above shell script does is, it will copy the files from machineB
OR machineC
to machineA
. 我上面的shell脚本所做的是,它将文件从
machineB
或machineC
复制到machineA
。 If the files are not there in machineB
then it should be there in machineC
always. 如果文件不在
machineB
则应该始终在machineC
。 So it will try to copy the files from machineB
to machineA
but if the files are not there in machineB
then it will try to copy the files from machineC
to machineA
. 因此它将尝试将文件从
machineB
复制到machineA
但是如果文件不在machineB
则它将尝试将文件从machineC
复制到machineA
。
Now my above Python script (which I am running from machineA
) tries to execute my above shell script and see whether my script got executed successfully or not. 现在,我上面的Python脚本(我正在从
machineA
运行)试图执行我上面的shell脚本,并查看我的脚本是否成功执行。 As I am storing the stderr
of my shell script and stdout
as well. 当我存储shell脚本和
stdout
的stderr
时。
Problem Statement:- 问题陈述:-
With the above approach there is one problem that I am seeing. 通过上述方法,我看到了一个问题。 As I mentioned if the files are not there in
machineB
, then it will try to copy the files from machineC
to machineA
. 如前所述,如果
machineB
中没有文件,则它将尝试将文件从machineC
复制到machineA
。 So whenever I run my Python script which calls my shell script, what happens is that, some files are not there in machineB
so it throws some exception and then it tries copying the files from machineC
but when the call comes back to Python script, it always go inside if stderr:
block as there was an error while copying the files from machineB
(which is not what I want) and end
print statement doesn't gets printed out. 因此,每当我运行调用我的Shell脚本的Python脚本时,发生的是,
machineB
中不存在某些文件,因此它引发了一些异常,然后尝试从machineC
复制文件,但是当调用返回到Python脚本时,它将if stderr:
块总是进入内部if stderr:
因为从machineB
复制文件时出现了错误(这不是我想要的),并且end
print语句没有被打印出来。
So the question is if the files are not there in machineB
, it will try copying the files from machineC
and if the file is there is machineC
and got successfully copied to machineA
without any error, then I want to call that as a success
instead of failure
. 所以问题是,如果
machineB
中没有文件,它将尝试从machineC
复制文件,如果文件存在machineC
并成功复制到machineA
而没有任何错误,那么我想将其称为success
而不是failure
。 In current scenario what is happening, if the files are not there in machineB
and got successfully copied from machineC
then it still counts as a failure and end
print statement doesn't gets printed out. 在当前情况下发生了什么,如果文件是不是有
machineB
并得到了来自成功复制machineC
那么它仍然算作是失败, end
打印语句不被打印出来。
I also want to see whether my shell script has any problem while executing from Python. 我还想查看从Python执行时我的shell脚本是否有问题。 If yes, then I don't want to print
end
statement. 如果是,那么我不想打印
end
声明。
How should I overcome this problem? 我应该如何克服这个问题?
UPDATED:- 更新:-
What will happen with the below shell script? 下面的shell脚本会发生什么? As the first
for
loop will failed because david
is not a linux command, but my scp
command works fine. 因为第一个
for
循环将失败,因为david
不是linux命令,但是我的scp
命令可以正常工作。 So still I will see status code as not equal to 0
? 所以我仍然会看到状态码不等于
0
吗?
for i in $( david ); do
echo item: $i
done
dir1=some_directory
dir2=some_directory
length1=some_number
length2=some_number
if [ "$dir1" = "$dir2" ] && [ "$length1" -gt 0 ] && [ "$length2" -gt 0 ]
then
for el in $jj3
do
scp david@machineB:/data/be_t1_snapshot/20131215/t1_"$el"_5.data /data01/primary/. || scp david@machineC:/data/be_t1_snapshot/20131215/t1_"$el"_5.data /data01/primary/.
done
fi
A process which worked correctly can still write to stderr
, so it is wrong to check for problems using if stderr:
正常工作的进程仍然可以写入
stderr
,因此使用if stderr:
来检查问题if stderr:
Instead you should check the processes exit code. 相反,您应该检查进程退出代码。 If it is not equal to zero, there was an error, eg:
如果不等于零,则出现错误,例如:
[...]
if proc.returncode != 0:
print "Shell script gave some error"
[...]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.