0

I saw other posts the same problem, but I can't figure out since everyone does different things. I'm new to shell script as well, so my limited knowledge is getting more confused when I read those posts.

I have this scrip work perfectly well on another server, but somehow on the new server, I need to check different service names and the script returns error above. I hope you can help me point out what I'm doing wrong here.

The error is: line 9: test: too many arguments

Here is the script:

#!/usr/bin/bash

GREEN=0
YELLOW=1
RED=2

pid=`/bin/ps -eo fname,pid | /usr/bin/awk '{if ($1 == "sshd") print $2}'`

if test $pid
then
        message="sshd Server is running. PID: $pid"
        status=$GREEN
else
        message="sshd Server is stopped."
        status=$RED
fi
echo $message
exit $status
5
  • 1
    Echo the content of pid to console before you use it. What does it contain? And what exactly do you want to test it against? Edit your question and add the necessary details. But first take the tour and read How to Ask to learn how you can improve your question in general.
    – Murphy
    Mar 5, 2020 at 19:02
  • 1
    Add an echo $pidat line 8, It seems you have more than one PID inside your variable
    – Maze
    Mar 5, 2020 at 19:02
  • 2
    You probably want to use pgrep rather than a far too complicated pipeline with ps. Or, you may want to use a dedicated service command to check whether sshd is running or not (like rcctl check sshd on OpenBSD systems, possibly something involving systemd or systemctl or some such thing on Linux).
    – Kusalananda
    Mar 5, 2020 at 19:09
  • Calling anything test is a bad idea, as that is the name of a shell builtin/standard command, and that definition might be used instead of yours...
    – vonbrand
    Mar 5, 2020 at 20:31
  • @vonbrand What is called test the only use of test is the shell built in. Mar 5, 2020 at 20:37

2 Answers 2

1

Seems you have lot of ssh connections...

Try this,

#!/usr/bin/bash

GREEN=0
YELLOW=1
RED=2

pid=(`/bin/ps -eo fname,pid | /usr/bin/awk '{ if ($4 == "abc") print $2}'`)

if  [ ${#pid[@]} -gt 0 ]
then
        message="sshd Server is running. PID: ${pid[@]}"
        status=$GREEN
else
        message="sshd Server is stopped."
        status=$RED
fi
echo $message
exit $status

if you just want only the PID of ssh service, try the below

#!/usr/bin/bash

GREEN=0
YELLOW=1
RED=2

pid=`cat /var/run/sshd.pid`
if test $pid
then
        message="sshd Server is running. PID: $pid"
        status=$GREEN
else
        message="sshd Server is stopped."
        status=$RED
fi
echo $message
exit $status
2
  • Thank you so much Murphy and Kusalananda, the second modification much simpler and works like a charm. The PID of SSH script is much more efficient. Thank you for pointing out the problem so fast. Thank you, everyone, for your recommendation, I very appreciate your time and advised. Best Regards, PN
    – user398812
    Mar 5, 2020 at 19:39
  • 1
    @GAD3R Agreed !!! there are lot more to be improved.... like unnecessary variables... just want to help OP only to solve his error...
    – Siva
    Mar 5, 2020 at 19:46
-1

Use the quotes:

I.e.

if test "$pid"

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .