0

I am calling some java binary in unix environment wrapped inside python script

When I call script from bash, output comes clean and also being stored in desired variable , However when i run the same script from Cron, Output stored(in a Variable) is incomplete

my code:

command = '/opt/HP/BSM/PMDB/bin/abcAdminUtil -abort -streamId ETL_' \
          'SystemManagement_PA@Fact_SCOPE_OVPAGlobal'
proc = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, 
                       stderr=subprocess.PIPE)
(output, err) = proc.communicate() # Storing Output in output variable

Value of output variable when running from shell:

Abort cmd output:PID:8717
Executing abort function
hibernateConfigurationFile = /OBRHA/HPE-OBR/PMDB/lib/hibernate-core-4.3.8.Final.jar
Starting to Abort Stream ETL_SystemManagement_PA@Fact_SCOPE_OVPAGlobal
Aborting StreamETL_SystemManagement_PA@Fact_SCOPE_OVPAGlobal

Value of output variable when running from cron:

PID:830

It seems output after creating new process is not being stored inside variable , i don't know why ?

Kintul
  • 1
  • 1
  • 2

2 Answers2

0

Kintul.

You question seems to be very similar to this one: Capture stdout stderr of python subprocess, when it runs from cron or rc.local

See if that helps you.

notrev
  • 665
  • 1
  • 5
  • 16
  • I have all parameters in place what is suggested in post above I am already using shell=True and p.communicate() , it seems to be something with new process being created by command – Kintul Apr 02 '19 at 07:15
0

This happened because Java utility was trowing exception which is not being cached by subprocess.Popen

However exception is catched by subprocess.check_output

Updated Code :

try:
    output = subprocess.check_output(command, shell=True, stderr=subprocess.STDOUT, stdin=subprocess.PIPE)
except subprocess.CalledProcessError as exc:
    print("Status : FAIL", exc.returncode, exc.output)
else:
    print("Output of Resume cmd: \n{}\n".format(output))
    file.write("Output of Resume cmd: \n{}\n".format(output) + "\n") 

Output of code:

('Status : FAIL', -11, 'PID:37319\n')
('Status : FAIL', -11, 'PID:37320\n')

Hence , command is throwing exception is being cached by subprocess.check_output but not by subprocess.Popen

Extract form official page of subprocess.check_output

If the return code was non-zero it raises a CalledProcessError. The CalledProcessError object will have the return code in the returncode attribute and any output in the output attribute.

Kintul
  • 1
  • 1
  • 2