LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-13-2009, 11:52 AM   #1
NickJH
LQ Newbie
 
Registered: Mar 2009
Distribution: ClarkConnect CE5.0
Posts: 19

Rep: Reputation: 0
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?
 
Old 09-13-2009, 12:05 PM   #2
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Might be worth reading the start-stop-daemon man page.
 
Old 09-13-2009, 12:32 PM   #3
NickJH
LQ Newbie
 
Registered: Mar 2009
Distribution: ClarkConnect CE5.0
Posts: 19

Original Poster
Rep: Reputation: 0
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.
 
Old 09-13-2009, 12:53 PM   #4
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by NickJH View Post
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 ...
 
Old 09-13-2009, 01:55 PM   #5
NickJH
LQ Newbie
 
Registered: Mar 2009
Distribution: ClarkConnect CE5.0
Posts: 19

Original Poster
Rep: Reputation: 0
When you say "what does it look like", how to I tell?
 
Old 09-14-2009, 01:56 PM   #6
NickJH
LQ Newbie
 
Registered: Mar 2009
Distribution: ClarkConnect CE5.0
Posts: 19

Original Poster
Rep: Reputation: 0
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?
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to run a script that will automatically start FTP service after installation? reshmpv Linux - Newbie 2 08-18-2008 07:56 AM
need a script to run oracle service shipon_97 Linux - Enterprise 5 03-02-2007 07:43 AM
How do I run a PHP script as a service? robbiemorgan Linux - Newbie 3 04-13-2005 02:03 PM
Priority: script to run on boot vs. "service iptables save" iheardrain Linux - General 5 09-30-2004 09:53 PM
on Network Up Script run? On Battery power run script? v2-ncl Linux - General 0 12-08-2003 09:34 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:31 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration