#StackBounty: #bash #service #shell-scripting Running a binary as a service in RedHat/CentOS

Bounty: 50

I have created a script to execute a binary as a service. But the service does not start the service but when i stop the service it shows multiple pids.

I am using RHEL 7.Here is the complete details

NAME="CentOS Linux"
VERSION="7 (Core)"
ID="centos"
ID_LIKE="rhel fedora"
VERSION_ID="7"
PRETTY_NAME="CentOS Linux 7 (Core)"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:centos:centos:7"
HOME_URL="https://www.centos.org/"
BUG_REPORT_URL="https://bugs.centos.org/"

CENTOS_MANTISBT_PROJECT="CentOS-7"
CENTOS_MANTISBT_PROJECT_VERSION="7"
REDHAT_SUPPORT_PRODUCT="centos"
REDHAT_SUPPORT_PRODUCT_VERSION="7"

Usually we run the script as follows ./pipeline --config pipeline.conf

Here is the script

#!/bin/bash
# chkconfig: 345 99 01
# description: This service is meant to manage the pipeline service
DAEMON_PATH=/home/cisco/bigmuddy-network-telemetry-pipeline/bin
DAEMON_NAME=pipeline
PATH=$PATH:$DAEMON_PATH

case "$1" in
  start)
        #start daemon
        pid=$(ps ax|grep -i 'pipeline' | grep -v grep | awk '{print $1}')
        if [ -n "$pid" ]
         then
           echo "Pipeline is already RUNNING";
        else
          echo "Starting $DAEMON_NAME";
          $DAEMON_PATH/pipeline -daemon --config pipeline.conf
        fi
        ;;
   stop)
        #stop daemon
        pid=$(ps ax|grep -i 'pipeline' | grep -v grep | awk '{print $1}')
        echo "The pid is $pid"
        echo "Shutting down $DAEMON_NAME";
        kill -9 $pid
        ;;
   restart)
         $0 stop
         sleep 2
         $0 start
         ;;
   status)
         pid=$(ps ax | grep -i 'pipeline' | grep -v grep |awk '{print $1}')
         if [ -n "$pid" ]
           then
           echo "Pipeline is RUNNING PID: $pid"
         else
           echo "Pipeline is not RUNNING"
         fi
         ;;
   *)
        echo "usage: $0 (start|stop|restart|status)"
esac

exit 0

I also tried writing a unit service under the /etc/systemd/system/pipeline.service

But this did not work either

[Unit]
Description=Pipeline service

[Service]
Type=simple
User=cisco
ExecStart=/hfqp/bin/pipeline --config /hfqp/bin/pipeline.conf

and the did the following

systemctl daemon-reload
systemctl start pipeline.service

Edit:

I have also used the pid file as per the suggestions

#!/bin/bash
# chkconfig: 345 99 01
# description: This service is meant to manage the pipeline service
DAEMON_PATH=/home/cisco/bigmuddy-network-telemetry-pipeline/bin
DAEMON_NAME=pipeline
PATH=$PATH:$DAEMON_PATH

case "$1" in
  start)
        #start daemon
    pid_file=/var/run/pipeline.pid
    if [ -f "$pid_file"]
      then
      echo "Pipeline is RUNNING"
        else
          echo "Starting $DAEMON_NAME";
          nohup $DAEMON_PATH/pipeline --config $DAEMON_PATH/pipeline.conf &
      echo $! >$pid_file
        fi
        ;;
   stop)
        #stop daemon
    if [ -f "$pid_file"]
          then
          kill -9 `cat $pid_file`
          echo "Shutting down $DAEMON_NAME";
        fi
        ;;
   restart)
         $0 stop
         sleep 2
         $0 start
         ;;
   status)
         if [ -f "$pid_file" ]
           then
           pid=`cat $pid_file`
           echo "Pipeline is RUNNING PID: $pid"
         else
           echo "Pipeline is not RUNNING"
         fi
         ;;
   *)
        echo "usage: $0 (start|stop|restart|status)"
esac

exit 0

Even this did not work.

I also tried creating a shell script and created unit service under systemd

[Unit]
Description=Pipeline service

[Service]
Type=simple
User=cisco
ExecStart=/home/cisco/bigmuddy-network-telemetry-pipeline/bin/executePipeline.sh

[Install]
WantedBy=multi-user.target

Even this did not work

here is the error log

● pipeline.service - Pipeline service
   Loaded: loaded (/etc/systemd/system/pipeline.service; disabled; vendor preset: disabled)
   Active: failed (Result: exit-code) since Tue 2019-04-16 06:29:34 GMT; 19s ago
  Process: 10195 ExecStart=/home/cisco/bigmuddy-network-telemetry-pipeline/bin/executePipeline.sh --daemon (code=exited, status=1/FAILURE)
 Main PID: 9962 (code=exited, status=1/FAILURE)

Apr 16 06:29:07 matrix-pipeline-b-01 systemd[1]: Starting Pipeline service...
Apr 16 06:29:32 matrix-pipeline-b-01 sudo[10196]: pam_unix(sudo:auth): conversation failed
Apr 16 06:29:32 matrix-pipeline-b-01 executePipeline.sh[10195]: sudo: no tty present and no askpass program specified
Apr 16 06:29:32 matrix-pipeline-b-01 sudo[10196]: pam_unix(sudo:auth): auth could not identify password for [cisco]
Apr 16 06:29:34 matrix-pipeline-b-01 systemd[1]: pipeline.service: control process exited, code=exited status=1
Apr 16 06:29:34 matrix-pipeline-b-01 systemd[1]: Failed to start Pipeline service.
Apr 16 06:29:34 matrix-pipeline-b-01 systemd[1]: Unit pipeline.service entered failed state.
Apr 16 06:29:34 matrix-pipeline-b-01 systemd[1]: pipeline.service failed.

Any help is appreciated


Get this bounty!!!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.