cli-purge
cli-purge copied to clipboard
Output going to stderr
I'm doing a purge invalidate command in a python script using subprocess and getting the results with communicate().
The issue is that the output is being sent to stderr and I'm not sure the best way to tell when the purge has succeeded or failed. I found this comment on an akamai forum where another user was having the same problem, but it's been almost a year and no one ever answered.
Can anyone help with this? Thanks!
@anne-jones there are two ways to determine if the purge was successful:
- Exit Code: the exit code is non-zero if an error occurs, see: https://docs.python.org/3/library/subprocess.html#subprocess.Popen.returncode
- Read STDERR: Set
Popen.stdout
tosubprocess.PIPE
before callingcommunicate()
then it will returnstderr
output as the second value in the returned tuple. see https://docs.python.org/3/library/subprocess.html#subprocess.Popen.stderr
Hopefully this helps?
Thanks so much for your response!
-
I have a check on the returncode and I log an error if it's non-zero, but I didn't see that happen when the purge "failed," though I may have figured out why that is. When I ran the purge command from the command line it asked me if I wanted to update (y/n) which isn't something my script handles. Though I find it strange that nothing printed out if that's why the purge didn't work (see code below to see what I mean).
-
Here is example code of what I've been doing:
command = CONFIG_DATA["akamai_cli"] + " purge --edgerc " + CONFIG_DATA["edgerc"] + " invalidate --cpcode " + cpcode()
process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
output, error = process.communicate()
# akamai prints to error ?_? instead of output
print("Error: " + error.decode('UTF-8'))
print("Output:" + output.decode('UTF-8'))
if (process.returncode):
raise Exception("Error running akamai purge")
Here are the results of running the code:
As you can see the results are coming through the stderr pipe and I'm not getting anything through stdout.
@anne-jones this is the correct behavior — the CLI — like many other tools — only outputs data that is meant to be piped to other applications to STDOUT, and we use STDERR for all other (e.g. informational) output.