0

I tried to to modify an existing script to work..the start option works fine. The "stop" option works ( it does kill the process successfully but gave some strange errors. Please see the output at the bottom. Can anyone please help?

#!/bin/sh  
# ckconfig: 35 99 1  
### BEGIN INIT INFO  
# Provides: kibana
# Required-Start: $syslog $network $named $remote_fs $time networker
# Required-Stop: $syslog
# Should-Start: $ALL
# Should-Stop: $ALL
# Default-Start: 3 5
# Default-Stop: 0 6
# Description: Start the kibana application
### END INIT INFO

SCRIPT_NAME="$0"  
SERVICE_NAME="kibana"  

usage()  
{  
    echo "USAGE: kibana start|stop" 1>&2  
    exit 1  
}  
start()
{
        export kibana_exec="nohup /data/kibana-main/bin/kibana > /data/logs/kibana/kibana.log 2>&1 "
        export kibana_parms="&"
        if [ ! -f $kibana_exec ]; then
                echo "kibana ERROR"
                RETVAL=1
                return 1
        fi
        /bin/su - Delk_admin_dev -c "$kibana_exec $kibana_parms 2>&1"
        RETVAL=$?
        return $RETVAL
}

stop()
{
        export PIDFILE="`(ps -eaf | grep kibana | awk '{print $2}')`"
        export pid=$PIDFILE
        if [ ! -f $kibana_exec ]; then
                echo "kibana ERROR"
                RETVAL=1
                return 1
        fi
        /bin/su - Delk_admin_dev -c "kill -15 $pid"
        RETVAL=$?
        return $RETVAL
}


case "$1" in
        start)
                start
                ;;
        stop)
                stop
                ;;
        *)
                /bin/echo $"Usage: kibana {start|stop}"
                ;;
esac

exit $?

[Delk_admin_dev@pvmdlr001 ~]$ ./newbash stop
./newbash: line 39: [: too many arguments
Password:
-bash: line 1: 8197: command not found
Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255

2 Answers2

2
export kibana_exec="nohup /data/kibana-main/bin/kibana > /data/logs/kibana/kibana.log 2>&1 "
if [ ! -f $kibana_exec ]; then

This looks like something that would give the [: too many arguments error: $kibana_exec expands to multiple words (five, in the value given above), that don't form a sensible test, so [ throws an error. It doesn't make much sense even with [ ! -f "$kibana_exec" ] since the value in the variable doesn't look like a filename either. Also, if you meant to use command substitution to run the program and capture what it printed, you'd use kibana_exec=$(command ...), but here that won't work either, since you're redirecting all output to a logfile.

However, I can't match the line number (39) in the error message to the script. As far as I can tell, line 39 is the export pid=$PIDFILE, which doesn't look like it should give an error.

I'm not exactly sure what it is you're trying to do here, should kibana_exec hold just the name of the executable (/data/kibana-main/bin/kibana)? And if so, perhaps you should use [ -x "$kibana_exec" ].


In the stop() function, kibana_exec looks to be unset, so [ ! -f $kibana_exec ] will be just [ ! -f ] which tests to see if -f is empty. It isn't, so the conditional part never runs.

export PIDFILE="`(ps -eaf | grep kibana | awk '{print $2}')`"
export pid=$PIDFILE

Here, the grep probably matches multiple lines in the ps output, namely kibana and the grep itself, so the final output has two PIDs, in two lines. These get set to pid, and when you run su -c "... $pid", the shell started by su sees two lines, with the second only having the second PID. It tries to run that as a command, doesn't find it, and you get something like 8197: command not found. The line 1 referred to in the error message is obviously not the first line of the full script, so that also works as a hint that it's from another shell spawned within the script.

Something like pid=$(pgrep kibana) might be better (pgrep won't match itself), or pid=$(pgrep -d' ' kibana) to get the PIDs space separated, just in case there's more than one (so su -c "kill $pid" will kill them all).

ilkkachu
  • 138,973
  • ------------I cleaned up the script a little bit. No longer see the "too many arguments.." but still see "command not found..". You are right that the grep ends up matching multiple lines in the ps output...I still can't figure out how to just get one pid..the first pid is the process id that I want to kill...Thanks so much for your feedback anyway! – hungl99 May 30 '17 at 18:33
  • @hungl99, pgrep might be better than ps | grep. edited. – ilkkachu May 30 '17 at 18:59
0

To get rid of the [: too many arguments error, I'd suggest putting any variable on that line in double quotes, forcing it to be constrained to one string.

E.g. Line 39 is:

export pid=$PIDFILE

try changing it to:

export pid="$PIDFILE"
  • Thanks for the feedback! I cleaned up the script and no longer seeing "..too many arguments.." error. – hungl99 May 30 '17 at 18:35
  • start()
    {
    export kibana_exec="/data/kibana-main/bin/kibana"
    export kibana_parms="> /data/logs/kibana/kibana.log 2>&1 &"
    if [ ! -f $kibana_exec ]; then
    echo "kibana ERROR"
    RETVAL=1
    return 1
    fi
    /bin/su - Delk_admin_dev -c "$kibana_exec $kibana_parms 2>&1"
    RETVAL=$?
    return $RETVAL
    }
    – hungl99 May 30 '17 at 18:35
  • stop() { export PIDFILE="(ps -eaf | grep kibana | awk '{print $2}')" if [ ! -f $kibana_exec ]; then echo "kibana ERROR" RETVAL=1 return 1
    fi
    /bin/su - Delk_admin_dev -c "kill -15 $PIDFILE"
    RETVAL=$?
    return $RETVAL
    }
    – hungl99 May 30 '17 at 18:36