-1

I am trying to set init.d script for some service - let it be uptime that is located in /usr/bin/uptime. Here is my script:

#!/bin/bash
# description: read service
#Source function library
. /etc/rc.d/init.d/functions


RETVAL=0
UPTIME=/usr/bin/uptime
PIDFILE=/var/run/read.pid

start() {
echo -n $"Starting $UPTIME service: "
/usr/bin/uptime &
echo $! > $PIDFILE; 
RETVAL=$?
[ $RETVAL -eq 0 ] && touch /var/lock/subsys/uptime
echo
return $RETVAL
}

stop() {
echo -n $"Shutdown $UPTIME service: "
killproc /usr/bin/uptime
rm -f $PIDFILE
RETVAL=$?
[ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/uptime
echo
return $RETVAL
}

restart() {
echo -n $"Restarting $UPTIME service: "
killproc /usr/bin/uptime
/usr/bin/uptime &
}

status() {
if [ `pidof /var/run/read.pid` ];then
        echo "Running"
    else
        echo "Not running"
    fi
 }


case "$1" in
start)
    start
    ;;
stop)
    stop
    ;;
status)
    status /usr/bin/uptime
    ;;
restart|reload)
    stop
    start
    ;;
*)
    echo $"Usage: $0 {start|stop|restart|reload|status}"
    exit 1
esac
exit $?

After starting my script PIDFILE=/var/run/read.pid had successfully created. But when I used status for my service - it was not running. It meant that pidof /var/run/read.piddidn't find pid of my service. ps -ef didn't show anything too. What am I going to do to make my read service runs in background properly and my status command /etc/init.d/read.sh status shows running due to my script.

fuser
  • 113
  • 6

1 Answers1

0

I don't think you are using pidof correctly, pidof takes a process name and returns it's PID e.g.

pidof httpd
19420 7273 7272 7271 7270 7269 7268 7267 7266

You are passing a filename to pidof not a process name. I guess what you want to do is check that the process with the pid stored in the pid file is still running.

The functions file you were having difficulty including earlier appears to have a function called status that looks to do pretty much what you want

status -p pidfile processname

e.g.

status -p /var/run/read.pid someprocess
someprocess (pid  7272) is running...

The $? variable is set appropriately too. Note also that the processname you provide can be anything it's not checked.

user9517
  • 115,471
  • 20
  • 215
  • 297
  • I understand. But I still don't understand how to change my status function in a proper way? – fuser Nov 22 '15 at 20:06
  • Or I can use `status -p /var/run/read.pid someprocess` in it? Something like: `status() { if [ `status -p /var/run/read.pid someprocess` ]; then echo "running"}` ??? – fuser Nov 22 '15 at 20:09
  • When I use `status() { if [ `status -p /var/run/read.pid someprocess` ]; then echo "running"}` my shell just hang up o_O – fuser Nov 22 '15 at 20:16
  • It is clear that you are fairly new to this, I have some advice for you. Learn to read documentation and try to understand it. Even if you read the man page for pidof you really didn't understand it at all. Be inquisitive and learn to think outside the box. I didn't know that the `status` function existed till I went looking for it. In particular I went and looked at how other service scripts in /etc/init.d carried out the process and worked it out from there. Perhaps you should do the same, it will answer the additional questions you have asked. – user9517 Nov 22 '15 at 20:34
  • Also, be aware, Server Fault is not your personal assistant. We expect some effort on your part (which you are _not_ displaying). If you are not careful you will become a help vampire. – user9517 Nov 22 '15 at 20:35