python process termination

  • 0
  • 1
  • Question
  • Updated 1 year ago
  • Answered

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?

Photo of bw447

bw447

  • 906 Points 500 badge 2x thumb

Posted 1 year ago

  • 0
  • 1
Photo of Dave Hammers

Dave Hammers, Dir SW Engineering

  • 3,502 Points 3k badge 2x thumb
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. 
Photo of bw447

bw447

  • 906 Points 500 badge 2x thumb
@Dave, Thanks for the explanation!
Photo of bw447

bw447

  • 906 Points 500 badge 2x thumb

@Dave,  Currently we are running 16.1.3.6

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.

Thanks!

Photo of Dave Hammers

Dave Hammers, Dir SW Engineering

  • 3,502 Points 3k badge 2x thumb
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 1
In the class, change cli2xml.py to cli2json.py for JSON output.to change from XML to JSON
(Edited)
Photo of bw447

bw447

  • 906 Points 500 badge 2x thumb

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? 

Photo of Dave Hammers

Dave Hammers, Dir SW Engineering

  • 3,502 Points 3k badge 2x thumb
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 <cmd> < - this would be your new 3 line script
run script cli2json.py <cmd> <-this is the script already shipping with EXOS
(run script cli2json.py -d <cmd> creates a more human readable output)
Photo of bw447

bw447

  • 906 Points 500 badge 2x thumb
Got it!  Thank you sir!