LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How do I run a script as a service (https://www.linuxquestions.org/questions/linux-newbie-8/how-do-i-run-a-script-as-a-service-754843/)

NickJH 09-13-2009 11:52 AM

How do I run a script as a service
 
Hi,
I have a bash script I wrote to monitor two VPN connections. I currently start it from rc.local, but to kill it I have to do a ps aux .... and a kill. To get round this I have hacked another init script, but it does not work as expected. My script is:
Code:

#!/bin/sh
#
# Startup script for ipsec-check. Hacked from pptpd
#
# description: This script starts ipsec-check
# processname: ipsec-check
# pidfile: /var/run/ipsec-check.pid

# Source function library.
. /etc/rc.d/init.d/functions

prog="ipsec-check"
RETVAL=0

# See how we were called.
case "$1" in
  start)
        echo -n $"Starting $prog: "
        daemon /etc/ipsec-check >/dev/null 2>&1 &
        RETVAL=$?
        echo
        [ $RETVAL -eq 0 ] && touch /var/lock/subsys/ipsec-check
        ;;
  stop)
        echo -n $"Stopping $prog: "
        killproc ipsec-check
        RETVAL=$?
        echo
        [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/ipsec-check
        ;;
  status)
        status ipsec-check
        RETVAL=$?
        ;;
  restart|reload)
        $0 stop
        $0 start
        RETVAL=$?
        ;;
  condrestart)
        if test "x`/sbin/pidof ipsec-check`" != x; then
                $0 stop
                $0 start
                RETVAL=$?
        fi
        ;;
  *)
        echo "Usage: $0 {start|stop|restart|reload|condrestart|status}"
        exit 1
esac

exit $RETVAL

If I use that daemon line I end up with 3 processes:
Code:

ps aux | grep ipsec.c
root    13148  0.0  0.0  4524  772 pts/0    S    17:41  0:00 /bin/sh /etc/init.d/ipsec-check start
root    13150  0.0  0.0  4520  1176 pts/0    S    17:41  0:00 /bin/bash -c ulimit -S -c 0 >/dev/null 2>&1 ; /etc/ipsec-check
root    13151  0.0  0.0  4520  1212 pts/0    S    17:41  0:00 /bin/bash /etc/ipsec-check

instead of just the one I want (13151 in this case).

How do I get it to run just one process?

catkin 09-13-2009 12:05 PM

Might be worth reading the start-stop-daemon man page.

NickJH 09-13-2009 12:32 PM

I've had a read of it. If I understand it the -b switch may be useful but it says use as a last resort.

I've tried the start-stop-daemon command instead of just daemon, but the system (ClarkConnect CE5.0) just says command not found. If I try the -b switch with the daemon command then run ps aux nothing shows so either it has run and terminated or not run at all.

I may be being dense here or I'm just too new.

catkin 09-13-2009 12:53 PM

Quote:

Originally Posted by NickJH (Post 3681187)
I've tried the start-stop-daemon command instead of just daemon, but the system (ClarkConnect CE5.0) just says command not found. If I try the -b switch with the daemon command then run ps aux nothing shows so either it has run and terminated or not run at all.

That's inconvenient. Thanks for giving your distro. What does the daemon function look like? Maybe the solution is in a modified version ...

NickJH 09-13-2009 01:55 PM

When you say "what does it look like", how to I tell?

NickJH 09-14-2009 01:56 PM

The answer seems to be to change the starting daemon line to:
Code:

daemon "/etc/ipsec-check &"
I guess the key is the quote marks enclosing the &. Presumably this puts ipsec-check into the background instead of daemon. Is this a correct interpretation?


All times are GMT -5. The time now is 06:14 AM.