3

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

5 Answers5

10

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 printing whether the sshd service is running or not:

systemctl is-active --quiet sshd && echo "sshd is running" || echo "sshd is NOT running"
Feriman
  • 969
  • 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 '19 at 09:18
  • 1
    Thanks for the answer! How can I use it with an if? I have done something like STATUS=$(systemctl is-active service) and then if [ "$STATUS" = "active" ] or if [ "$STATUS" = "inactive" ] ? – Ferran Maylinch Nov 02 '21 at 08:27
  • 1
    Answering to my question. I found it here: https://unix.stackexchange.com/questions/22726/how-to-conditionally-do-something-if-a-command-succeeded-or-failed. For example: if systemctl is-active --quiet service; then ... or if ! systemctl is-active --quiet service; then .... – Ferran Maylinch Nov 02 '21 at 08:47
  • 1
    @FerranMaylinch you can also access the previous commands exit code via $? – sudo soul Apr 11 '22 at 04:47
  • This does not appear to work for type=exec. I just tried this on podman and the problem is the service deactivates after running. – Grant Curell Feb 08 '23 at 17:00
3

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  
Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255
1

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

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

John
  • 1,210
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
jesse_b
  • 37,005
Rosey
  • 1