kill: 5656 : arguments must be process or job IDs

0

Having redis-server ON (launched with redis-server &).

Running this command in a terminal works perfectly:

kill -s SIGTERM "`pgrep redis-server`"

But in a script it outputs the following message and does not kill the process:

myscript.sh: line 17: kill: 1448
1452: arguments must be process or job IDs

(if I do: pgrep redis-server in this example it will outputs me 1448)

My complete source script:

#!/bin/bash

if [ -a "redis-server_must_be_ON" ]
then
  if [ "`redis-cli PING`" != "PONG" ]
  then
    redis-server &
    if [ "`redis-cli PING`" != "PONG" ]
    then
      echo "redis-server still not running while it should have been set on." >> /dev/stderr
      exit 1
    fi
  fi
else
  if [ "`redis-cli PING`" == "PONG" ]
  then
    kill -s SIGTERM "`pgrep redis-server`"
    if [ "`redis-cli PING`" == "PONG" ]
    then
      echo "redis-server still running while it should have been set off." >> /dev/stderr
      exit 1
    fi
  fi
fi

(Here if I replace the pgrep redis-server with some sort of mascarade like pgrep bash1.sh it also works fine).

Is my script correct, what do I miss ?

Hellfar

Posted 2017-01-18T13:41:24.840

Reputation: 103

For completeness, does ps aux | grep redis-server return 1448 as the PID of the process? – djsmiley2k TMW – 2017-01-18T14:09:13.523

2Aren't you reinventing the wheel? Most systems have service redis stop, systemctl stop redis. – user1686 – 2017-01-18T14:11:23.380

username 1448 0.0 0.1 34880 4320 pts/1 Sl 14:17 0:01 redis-server *:6379 yep :/ – Hellfar – 2017-01-18T14:14:57.630

@grawity, your commands returns me Unit redis.service not found.. I also tried to use the service like that: /etc/init.d/redis-server start, but with no incidences (no errors, but no process either) both with start and stop. However, redis-server & let me use it and do redis request finely. – Hellfar – 2017-01-18T14:22:40.607

Ok so redis-cli shutdown works. But, this is still weird that the same kill command does not work in a script. – Hellfar – 2017-01-18T14:38:58.037

No answers