-3

I have a shell script which I used to run for a quick process check mechanism in my Linux environment. Below is just one piece of code which I am using.

How we can achieve the same in Python? I just searched and found to use the "subprocess" module for this purpose, will that be good enough?

Check_ntp () {
    echo "Checking the NTP Service Status on $(uname -n)"
    ps -e | grep ntp > /dev/null 2>&1
    if [ $? -eq 0 ] ; then
    echo "Service Status:  NTP Service is Running"
    else
    echo "Service Status:  NTP Service is Not Running"
    fi
    }

Update

I found the answer to my question myself:

#!/usr/bin/python

import subprocess
PSNTP = subprocess.call('ps -e| grep ntp > /dev/null 2>&1', shell=True)
if PSNTP == 0:
    print "Status:  NTP Service is Running"
else:
   print "Status:  NTP Service is not Runningg"

PSNSCD = subprocess.call('ps -e | grep nscd > /dev/null 2>&1', shell=True)
if PSNSCD == 0:
   print "Status:  NSCD Service is Running"
else:
   print "Status:  NSCD Service is not Running"
halfer
  • 19,824
  • 17
  • 99
  • 186
krock1516
  • 441
  • 10
  • 30
  • I don't know what any of that code is doing but I bet [`subprocess.check_output`](https://docs.python.org/2/library/subprocess.html#subprocess.check_output) would be useful here. – Kevin Apr 05 '17 at 18:19
  • @Kevin This is code is just doing the process check , there is a process called NTP in UNIX/Linux , this is just checking the validity of that process whether its ruining or not using the UNix/Linux "ps -e" command. – krock1516 Apr 05 '17 at 18:22
  • Don't put your answer in the question. The question is a question, not an answer. – Nic Apr 17 '17 at 02:22

1 Answers1

2

I couldn't figure out how do it with subprocess.check_output but using subprocess.Popen may do the job:

import subprocess

cmd1 = ['ps', '-e'] 
cmd2 = ['grep', 'ntp']

proc1 = subprocess.Popen(cmd1,stdout=subprocess.PIPE)
proc2 = subprocess.Popen(cmd2,stdin=proc1.stdout,
                         stdout=subprocess.PIPE,stderr=subprocess.PIPE)

proc1.stdout.close() # Allow proc1 to receive a SIGPIPE if proc2 exits.
out, err=proc2.communicate()

if out:
    print "Service Status:  NTP Service is Running"
else:
    print "Service Status:  NTP Service is Not Running"

# print('out: {0}'.format(out)) # optionally view output
# print('err: {0}'.format(err)) # and any errors
  • @downshit Thanks for your answer, though i got the way to do it at the same time i see your answer is really commendable as well. Can you please explain the `if out:` statement how its working. – krock1516 Apr 06 '17 at 03:20
  • @Karn, very welcome. `if out:` is a check to see if any response was received from `proc2.communicate()` method (the line before `if out:`) which is [Popen.communicate()](https://docs.python.org/2/library/subprocess.html#subprocess.Popen.communicate) which `communicate() returns a tuple (stdoutdata, stderrdata).". – chickity china chinese chicken Apr 06 '17 at 19:21
  • So `out` is a variable that got assigned to `index[0]` of that response tuple: `stdoutdata`, and likewise variable `err` got assigned to index[1]: `stderrdata`. Since we want to check if `stdoutdata` was received we do an `if` check, `if out:` which is the same as `if proc2.communicate()[0]:` the first index of the `subprocess.Popen().communicate()` response. – chickity china chinese chicken Apr 06 '17 at 19:21
  • By the way, I borrowed most of my answer from this answer: [How to run “ ps cax | grep something ” in Python?](http://stackoverflow.com/questions/6780035/how-to-run-ps-cax-grep-something-in-python) – chickity china chinese chicken Apr 06 '17 at 19:22
  • awesome , sounds good one. – krock1516 Apr 07 '17 at 10:59