0

I have a customised linux version, kernel version is 4.1.15-klk my platform architecture is armv7l GNU/Linux

I am trying to check if my process is running or not :

I tried this:

#!/bin/sh
service=myservice

if [ $(ps | grep -v grep | grep $service | wc -l) -gt 0 ]
then
 echo "$service is running!!!"
else
 echo "$service is not running!!!"
fi

but it is giving me "myservice is running!!!" whether it is running or not

I can't use pgrep since it is not available in my system

any ideas? thanks!

  • When the service is not running, what is the output of ps | grep -v grep | grep $service ? – John1024 Dec 22 '17 at 20:49
  • when the service is not running I got nothing for the output of ps | grep -v grep | grep $service and when it is running it display the service information in one line – sabrina2020 Dec 22 '17 at 20:59
  • Unless something else comes up, I still suspect that that is where your problem is. In your script, try if [ $(ps | grep -v grep | grep "$service" | tee ~/service.log | wc -l) -gt 0 ] and then inspect ~/service.log to verify that what the output really is. – John1024 Dec 22 '17 at 21:12
3

Based on this question, the short and scriptable way is:

systemctl is-active --quiet service

It will exit with code zero, if the service is running.

Example for print the sshd service is running or not:

systemctl is-active --quiet sshd && echo "sshd is running" || echo "sshd is NOT running"
  • 2
    This is much, much better than any other answer here. On any distro which uses systemd, this is THE right answer. – Wildcard Sep 11 at 9:18
0

On Ubuntu and CentOs systems, I'd use ps aux instead of ps alone.

  • ps aux is not supported in my system: ps aux ps: invalid option -- 'a' BusyBox v1.24.1 (2017-07-21 14:28:39 CEST) multi-call binary. Usage: ps – sabrina2020 Dec 22 '17 at 21:00
  • if you run ps is the service listed in your output? – John Dec 22 '17 at 21:04
  • 1
    yes the service is listed using only ps – sabrina2020 Dec 22 '17 at 21:05
  • Try just ps | grep $service – John Dec 22 '17 at 21:28
0

finally using this post, I was able to solve my problem:

#!/bin/sh
service=myservice

case  "$(pidof $service | wc -w)" in
0) echo "$service is not running!!!"
;;
1) echo "$service is  running!!!"
;;
*) echo "multiple instances running"
;;
esac
0

Use below script to check whether service running or not. I tested for the mysql service, making it up and down and in both the conditions it's working fine.

#!/bin/bash
i=`ps -eaf | grep -i mysql |sed '/^$/d' | wc -l`
echo $i
if [[ $i > 1 ]]
then
  echo "service is running"
else
  echo "service not running"
fi  
0

You can use the case function to better control it.

#!/bin/bash
i=$(ps -eaf | grep -i mysql | sed '/^$/d' | wc -l); echo $i
case $i in
    0) echo "The mysql service is 'not running'.";;
    1) echo "The mysql Service has 'failed'. You may need to restart the service";;
    2) echo "The mysql service is 'running'. No problems detected.";;
esac

The first example has an error in it. A space must be before & After '|', '||', '&' or '&&'

So it should read

i=$(ps -eaf | grep -i mysql | sed '/^$/d' | wc -l); echo $i

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.