I have a routine that frequently executes a shell command and parses the output from it. For this I am using the subprocess.Popen
to execute the shell command. Tested with the shell commands ls
, systemctl status xxxx.service
etc.
Platform - Linux 3.14.4-yocto-standard x86_64
status_call = subprocess.Popen(shlex.split(<shell_command>),
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
I used the communicate()
to read the output and error from the above child process. I am considering error scenario if the length of output is 0 or length of error is not zero and I am printing the error and the return code.
import subprocess
import shlex
def loop_process():
count = 0
iter = 0
print 'Test Case: Starting the script'
while True:
status_call = subprocess.Popen(shlex.split('ls'),
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
output, error = status_call.communicate()
if len(error) != 0 or len(output) == 0:
print 'Test Case : Unable to make the systemctl status call\n'
print 'error is : ' + str(error) + 'with length of output as ' + str(len(output))
print 'return_code is ' + str(status_call.returncode)
count = 0
iter = 0
else:
count = count + 1
if count > 1000:
iter = iter + 1
print iter
count = 0
if __name__ == '__main__':
loop_process()
The above logic is failing at random times and I am not getting any error message or the return code from the child process is zero as shown below
I am seeing the issue in here roughly for every 200,000 times. I am running it on embedded equipment. I am not seeing this issue when I run on QEMU
114
115
Unable to make the systemctl status call
error is : with length of output as 0
return_code is 0
1
2
Other occurrence
387
388
389
Test Case : Unable to make the systemctl status call
error is : with length of output as 0
return_code is 0
1
2
Can anyone guide me how to resolve such issues? What is the direction I need to concentrate on finding the root cause and possibly a fix? I am strongly suspecting some problem with the PIPE.