Dipak
Dipak

Reputation: 2308

Centos 7 custom bash script service does not work

I had created a deamon in centos 7 to send an email from email queue.

Email queue is implemented in Yii1. That works fine but when I tried to create and run daemon in server but it fails and shows an error :

echo "Error! Could not start MyStaging!"

I am following instructions which I found here.

On inspection I found that PID gets the value 1232 and pgrep -u $RUNAS -f $NAME > /dev/null command returns empty.

Below is my script:

#!/bin/bash
### BEGIN INIT INFO
# Provides: MyStaging
# Required-Start: $local_fs $network $named $time $syslog
# Required-Stop: $local_fs $network $named $time $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Description: This service having purpose to send email from email queue where relevant email push by particular module operation in queue.
### END INIT INFO

SCRIPT="/usr/bin/php5 /home/my/public_html/staging/protected/yiic mailqueue run"
RUNAS=root
NAME=MyStagingMailQueue

PIDFILE=/var/run/$NAME.pid
LOGFILE=/var/log/$NAME.log

start() {
 if [ -f $PIDFILE ] && kill -0 $(cat $PIDFILE); then
echo 'Service already running' >&2
   return 1
 fi
echo 'Starting service   ' >&2
 local CMD="$SCRIPT &> \"$LOGFILE\" & echo \$!"
# su -s /bin/sh $RUNAS -c "$CMD" > "$PIDFILE"
# Try with this command line instead of above if not workable
 su -c "$CMD" $RUNAS > "$PIDFILE"
sleep 2
 PID=$(cat $PIDFILE)
   if pgrep -u $RUNAS -f $NAME > /dev/null
   then
     echo "$NAME is now running, the PID is $PID"
   else
     echo "Error! Could not start $NAME!"
   fi
}

stop() {
 if [ ! -f "$PIDFILE" ] || ! kill -0 $(cat "$PIDFILE"); then
echo 'Service not running' >&2
   return 1
 fi
echo 'Stopping service   ' >&2
 kill -15 $(cat "$PIDFILE") && rm -f "$PIDFILE"
 echo 'Service stopped' >&2
}

status() {
       printf "%-50s" "Checking $NAME..."
   if [ -f $PIDFILE ]; then
       PID=$(cat $PIDFILE)
           if [ -z "$(ps axf | grep ${PID} | grep -v grep)" ]; then
               printf "%s\n" "The process appears to be dead but pidfile still exists"
           else
               echo "Running, the PID is $PID"
           fi
   else
       printf "%s\n" "Service not running"
   fi
}

case "$1" in
 start)
   start
   ;;
 stop)
   stop
   ;;
 status)
   status
   ;;
 restart)
   stop
   start
   ;;
 *)
   echo "Usage: $0 {start|stop|status|restart}"
esac

I don't know what is the issue. Please help me sort it out.

Upvotes: 1

Views: 2403

Answers (1)

sjsam
sjsam

Reputation: 21965

Since you're using CentOS 7, it's better to create a systemd service.

Step1:

Create a service file for your service say yiicmail.service. Do :

sudo touch /etc/systemd/system/yiicmail.service


Step2:

Open the the above file with your favourite editor and put the below content in it:

[Unit]
Description=yiic service
After=network.target

[Service]
Type=simple
User=root
ExecStart="/usr/bin/php5 /home/whizbite/public_html/staging/protected/yiic mailqueue run"
#If there are spaces, I would strings within quotes like above
Restart=on-abort


[Install]
WantedBy=multi-user.target


Step3:

Now it is the time to play with the service. To start it, do :

sudo systemctl start yiicmail # You don't have to type full name, that is, yiicmail.service

To check the status, do :

sudo systemctl status yiicmail

To stop it, do :

sudo systemctl stop yiicmail

To start the service at boot, do :

sudo systemctl enable yiicmail

To disable the service at boot, do:

sudo systemctl disable yiicmail

Hope this helps.

Upvotes: 1

Related Questions