cancel
Showing results for 
Search instead for 
Did you mean: 

python process termination

python process termination

bw447
New Contributor III
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 7

bw447
New Contributor III
Got it! Thank you sir!

Dave_Hammers
Extreme Employee
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)

bw447
New Contributor III
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?

Dave_Hammers
Extreme Employee
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)You can test this by the CLI command: run script cli2xml.py show port 1 no-refresh
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
@staticmethod
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,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
(stdoutdata, stderrdata) = p.communicate()
return (stdoutdata, stderrdata)

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