]> git.pld-linux.org Git - projects/rc-scripts.git/blob - rc.d/init.d/functions
59c4671e829e22b42022a2491bd27d3f8c1cfff8
[projects/rc-scripts.git] / rc.d / init.d / functions
1 # functions     This file contains functions to be used by most or all
2 #               shell scripts in the /etc/init.d directory.
3 #
4 # Version:      @(#) /etc/init.d/functions $Revision: 1.5 $ $Date: 1999/06/14 20:44:32 $
5 #
6 # Author:       Miquel van Smoorenburg, <miquels@drinkel.nl.mugnet.org>
7 # Hacked by:    Greg Galloway and Marc Ewing
8 # Modified for PLD by Marek Obuchowicz <elephant@shadow.eu.org>
9 #
10
11 # First set up a default search path.
12 PATH="/sbin:/usr/sbin:/bin:/usr/bin"; export PATH
13
14 # Some functions to handle PLD-style messages
15 function show {
16   text="$*".............................................
17   echo -ne `echo $text | cut -c1-45`
18 }
19
20 function busy {
21   echo -n "`tput setaf 6`[`tput setaf 5` BUSY `tput setaf 6`]`tput setaf 7`"
22 }
23
24 function ok {
25   echo  "`tput setaf 6`[`tput setaf 2` DONE `tput setaf 6`]`tput setaf 7`"
26 }
27
28 function fail {
29   echo  "`tput setaf 6`[`tput setaf 1` FAIL `tput setaf 6`]`tput setaf 7`"
30 }
31
32 function died {
33   echo  "`tput setaf 6`[`tput setaf 1` DIED `tput setaf 6`]`tput setaf 7`"
34 }
35
36 function deltext {
37   echo -ne '\b\b\b\b\b\b\b\b'
38 }
39
40 # A function to start a program.
41 daemon() {
42         TMPFILE=`mktemp /tmp/init-XXXXXX`
43         
44         nicelevel=0
45         # Test syntax.
46         case $1 in
47             '')    echo '$0: Usage: daemon [+/-nicelevel] {program}'
48                    return 1;;
49             -*|+*) nicelevel=$1
50                    shift;;
51         esac
52
53         # make sure it doesn't core dump anywhere; while this could mask
54         # problems with the daemon, it also closes some security problems
55         ulimit -c 0
56
57         # And start it up.
58         busy
59         if nice -n $nicelevel "$@" >$TMPFILE 2>&1; then
60           deltext
61           ok
62         else
63           deltext
64           fail
65           cat $TMPFILE
66         fi
67         rm -f $TMPFILE
68 }
69
70 # A function to stop a program.
71 killproc() {
72         # Test syntax.
73         if [ $# = 0 ]; then
74                 echo "Usage: killproc {program} [signal]"
75                 return 1
76         fi
77
78         busy
79         
80         TMPFILE=`mktemp /tmp/init-XXXXXX`
81         
82         notset=0
83         # check for second arg to be kill level
84         if [ "$2" != "" ] ; then
85                 killlevel=$2
86         else
87                 notset=1
88                 killlevel="-9"
89         fi
90
91         # Save basename.
92         base=`basename $1`
93
94         # Find pid.
95         pid=`pidofproc $base`
96
97         # Kill it.
98         if [ "$pid" != "" ] ; then
99                 if [ "$notset" = 1 ] ; then
100                         # TERM first, then KILL if not dead
101                         kill -TERM $pid >$TMPFILE 2>&1
102                         usleep 100000
103                         dead=`ps -o pid= $pid`
104                         if [ "$dead" != "" ]; then
105                                 sleep 3
106                                 kill -KILL $pid >$TMPFILE 2>&1
107                         fi
108                 # use specified level only
109                 else
110                         kill $killlevel $pid >$TMPFILE 2>&1
111                 fi
112         else
113           rm -f /var/run/$base.pid
114           deltext
115           fail
116           return
117         fi
118
119         # Remove pid file if any.
120         rm -f /var/run/$base.pid
121         
122         if [ -z "`cat $TMPFILE`" ]; then
123                 deltext
124                 ok
125         else
126                 deltext
127                 died    
128         fi
129         rm -f $TMPFILE
130 }
131
132 # A function to find the pid of a program.
133 pidofproc() {
134         # Test syntax.
135         if [ $# = 0 ] ; then
136                 echo "Usage: pidofproc {program}"
137                 return 1
138         fi
139
140         # First try "/var/run/*.pid" files
141         if [ -f /var/run/$1.pid ] ; then
142                 pid=`head -1 /var/run/$1.pid`
143                 if [ "$pid" != "" ] ; then
144                         echo $pid
145                         return 0
146                 fi
147         fi
148
149         # Next try "pidof"
150         #pid=`pidof $1`
151         if [ "$pid" != "" ] ; then
152                 echo $pid
153                 return 0
154         fi
155
156         # Finally try to extract it from ps
157         pids=`ps -C $1 -o pid=`
158
159         # Return the first PID, which is not our
160         for pid in $pids ; do
161                 if [ $pid != $$ ] ; then
162                         echo $pid
163                         return 0
164                 fi
165         done  
166         
167         return 1
168 }
169
170 status() {
171         # Test syntax.
172         if [ $# = 0 ] ; then
173                 echo "Usage: status {program}"
174                 return 1
175         fi
176
177         # First try "pidof"
178         pid=`pidof $1`
179         if [ "$pid" != "" ] ; then
180                 echo "$1 (pid $pid) is running..."
181                 return 0
182         else
183                 pid=`echo \`ps -C $1 -o pid= \``
184                 if [ "$pid" != "" ] ; then
185                         echo  "$1 (pid $pid) is running..."
186                         return 0
187                 fi
188         fi
189
190         # Next try "/var/run/*.pid" files
191         if [ -f /var/run/$1.pid ] ; then
192                 pid=`head -1 /var/run/$1.pid`
193                 if [ "$pid" != "" ] ; then
194                         echo "$1 dead but pid file exists"
195                         return 1
196                 fi
197         fi
198         # See if /var/lock/subsys/$1 exists
199         if [ -f /var/lock/subsys/$1 ]; then
200                 echo "$1 dead but subsys locked"
201                 return 2
202         fi
203         echo "$1 is stopped"
204         return 3
205 }
This page took 0.050996 seconds and 2 git commands to generate.