0

I wonder if it is possible to rewrite this script below on one line? Probably with || ?

ps auxw | grep nagios-nrpe-server | grep -v grep > /dev/null

if [ $? != 0 ]
then
        /etc/init.d/nagios-nrpe-server start > /dev/null
fi

I tryed ps auxw | grep nagios-nrpe-server | grep -v grep > /dev/null || /etc/init.d/nagios-nrpe-server start but it restart service all the time, even if it is running..

Delirium
  • 207
  • 4
  • 11

2 Answers2

3

You need to switch your grep statements. Your second statement to filter out grep will always succeed, because it is always there.

So, first filter out grep and then check for the process:

ps auxw | grep -v grep | grep nrpe > /dev/null || /etc/init.d/nagios-nrpe-server start

According to the Debian package list the binary of the nagios-nrpe-server is just called nrpe, so I substituted that.

Gerald Schneider
  • 23,274
  • 8
  • 57
  • 89
  • but if I switch it, service is always started over and over again, same as in my try.. Should not be in that one liner some check if output is "0" ? – Delirium Jul 30 '20 at 08:05
  • `||` is the check if the return code is `0`. In my tests the line works as I wrote it. – Gerald Schneider Jul 30 '20 at 08:06
  • 2
    Are you sure the string `nagios-nrpe-server` shows up in `ps`? It's been a while since I used nagios, but IIRC the process had a different name. – Gerald Schneider Jul 30 '20 at 13:56
  • 2
    According to the [debian package list](https://packages.debian.org/buster/amd64/nagios-nrpe-server/filelist) the binary is just called `nrpe`. – Gerald Schneider Jul 30 '20 at 14:00
3

Using pgrep:

pgrep nrpe > /dev/null || /etc/init.d/nagios-nrpe-server start > /dev/null

or using a subshell:

(pgrep nrpe || /etc/init.d/nagios-nrpe-server start) > /dev/null

From the pgrep(1) manpage:

pgrep looks through the currently running processes and lists the process IDs which match the selection criteria to stdout.

If the process name is not nrpe, you need to replace the argument to pgrep with the actual process name.

fpmurphy
  • 841
  • 6
  • 13
  • same problem..even when is Nagios running output from this is [ ok ] Starting nagios-nrpe-server (via systemctl): nagios-nrpe-server.service. – Delirium Jul 30 '20 at 12:14
  • You need to redirect the init script to `/dev/null` also. One way is `pgrep nagios-nrpe-server > /dev/null || /etc/init.d/nagios-nrpe-server start > /dev/null`. Or use a subshell: `(pgrep nagios-nrpe-server || /etc/init.d/nagios-nrpe-server start) > /dev/null` – chicks Jul 30 '20 at 13:35
  • 1
    @chicks Yes, you are correct. That solves the OP's spurious output issue. Modified my answer. – fpmurphy Jul 30 '20 at 15:11
  • Piping the output of the init script to `null` only suppresses a symptom of the problem, it doesn't solve the problem. – Gerald Schneider Jul 30 '20 at 15:50
  • @GeraldSchneider What problem needs solving? – fpmurphy Jul 30 '20 at 17:18
  • @fpmurphy The problem that the line still starts the service even when it's already running. – Gerald Schneider Jul 31 '20 at 06:55
  • @GeraldSchneider. How does your solution, i.e. `ps auxw | grep -v grep | grep nrpe`, solve that issue? – fpmurphy Jul 31 '20 at 09:46
  • as is @GeraldSchneider saying, problem is that service is starting even if it is already running. Thats not quite okay I think.. – Delirium Jul 31 '20 at 11:31
  • @Delirium I agree. What is the output of `pgrep -l nrpe` when the server is running? – fpmurphy Jul 31 '20 at 15:08
  • @fpmurphy 15914 nrpe – Delirium Aug 03 '20 at 07:49
  • @Delirium. Sigh, guess your check, i.e. `ps auxw | grep nagios-nrpe-server | grep -v grep > /dev/null` never worked! Modified my answer to use `npre` instead of `nagios-nrpe-server`. – fpmurphy Aug 03 '20 at 16:36
  • Thanks...working now. – Delirium Aug 04 '20 at 12:21