Header Only - DO NOT REMOVE - Extreme Networks

python process termination

Userlevel 2
I have a question on what happens when a process is terminated.

I create a process: create process Proc python-module [python file] start on-demand

I then start it: start process Proc slot 1

When I terminate it: terminate process Proc gracefully slot 1

Is there an exception that I can catch in my python script so I can do some kind of cleanup or extra logging before the process completely dies?

7 replies

Userlevel 5
Unfortunately, no.
The 'create process' in EXOS runs the python code in its own thread which means python threading rules apply.

A possible future enhancement would be to signal the thread in some fashion that a SystemExit exception occurred, but that doesn't help you in currently shipping EXOS. 
Userlevel 2
@Dave, Thanks for the explanation!
Userlevel 2
@Dave, Currently we are running

I prefer to get the output from a command as xml when running a script:

exsh.clicmd(cmd, xml=True)

Are there plans to be able to get xml output from a process? From what I see in the Python doc it's only possible to receive output as regular text.

Userlevel 5
There is a way to do this but it's a bit involved. We do something similar to return JSON from CLI commands.when writing EXOS python apps.

Create a script cli2xml.py with the following 3 lines:
import sys
cmd = ' '.join(sys.argv[1:])
print exsh.clicmd(cmd, xml=True)[/code]You can test this by the CLI command: run script cli2xml.py show port 1 no-refresh
[/code]It's also handy when you want to see what the XML output of a command looks like before writing any code.

In your python application, invoke the EXOS command shell to run cli2xml.py with your command.
Below is an example:
# This is an example of a class that would interface to cli2xml.py in the Python application # environment
import subprocess
class ExosCliToXml(object):
def __init__(self):
self.dummy = None
def exos_cli2xml(cmd):
shell_cmd = "/exos/bin/exsh -n 0 -b -c 'run script cli2xml.py {0}'".format(cmd)
p = subprocess.Popen([shell_cmd], shell=True,
(stdoutdata, stderrdata) = p.communicate()
return (stdoutdata, stderrdata)

# test stub
cli = ExosCliToXml()
while True:
cmd = raw_input('enter command > ')
if cmd in ['quit', 'exit']:
(stdoutdata, stderrdata) = cli.exos_cli2xml(cmd) # print the XML output from the command
print stdoutdata[/code]
If you wanted to use JSON, there is already a script shipping with EXOS: cli2json.py
E.g. run script cli2json.py show port 1[/code]In the class, change cli2xml.py to cli2json.py for JSON output.to change from XML to JSON
Userlevel 2
Hello Dave,

Thanks for the info. Below I'm getting the following Permission denied when I run your test script up above. I'm seeing the Permission denied output because I added 'print stderrdata' after your last print stdoutdata statement up above.

* Slot-1 Kenn-B.8 # run script CliToXml.py
enter command > show version

/bin/sh: /exos/bin/exsh: Permission denied

enter command > quit

I tried it with the JSON output by changing the script name as you said at then end of your comment and I received the same Persmission denied error.

I'm running this script as the local admin account.

Any thoughts?
Userlevel 5
Sorry, I wasn't clear. This has to be run from the EXOS process environment i.e. from the python that you give to 'create process'.

My test stub was a bad example since I bypassed the restrictions to validate the class.

If you include the class in your applications and call cli.exos_cli2xml(cmd) within the application, it should work for you.

The parts that are available from the normal CLI are:
run script cli2xml.py < - this would be your new 3 line script
run script cli2json.py <-this is the script already shipping with EXOS
(run script cli2json.py -d creates a more human readable output)
Userlevel 2
Got it! Thank you sir!