]> git.pld-linux.org Git - projects/rc-scripts.git/blob - rc.d/init.d/functions
Synced with rc-scripts-0.0.2 from old repo.
[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.4 $ $Date: 1999/06/14 20:12:05 $
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 -ne "`tput setaf 6`[`tput setaf 5` BUSY `tput setaf 6`]`tput setaf 7`"
22 }
23
24 function ok {
25   echo -ne "`tput setaf 6`[`tput setaf 2` DONE `tput setaf 6`]`tput setaf 7`"
26 }
27
28 function fail {
29   echo -ne "`tput setaf 6`[`tput setaf 1` FAIL `tput setaf 6`]`tput setaf 7`"
30 }
31
32 function died {
33   echo -ne "`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; 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
102                         usleep 100000
103                         dead=`ps -o pid= $pid`
104                         if [ "$dead" != "" ]; then
105                                 sleep 3
106                                 kill -KILL $pid &> $TMPFILE
107                         fi
108                 # use specified level only
109                 else
110                         kill $killlevel $pid &> $TMPFILE
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         echo `ps -C $1 -o pid=`
158         return 0
159 }
160
161 status() {
162         # Test syntax.
163         if [ $# = 0 ] ; then
164                 echo "Usage: status {program}"
165                 return 1
166         fi
167
168         # First try "pidof"
169         pid=`pidof $1`
170         if [ "$pid" != "" ] ; then
171                 echo "$1 (pid $pid) is running..."
172                 return 0
173         else
174                 pid=`echo \`ps -C $1 -o pid= \``
175                 if [ "$pid" != "" ] ; then
176                         echo  "$1 (pid $pid) is running..."
177                         return 0
178                 fi
179         fi
180
181         # Next try "/var/run/*.pid" files
182         if [ -f /var/run/$1.pid ] ; then
183                 pid=`head -1 /var/run/$1.pid`
184                 if [ "$pid" != "" ] ; then
185                         echo "$1 dead but pid file exists"
186                         return 1
187                 fi
188         fi
189         # See if /var/lock/subsys/$1 exists
190         if [ -f /var/lock/subsys/$1 ]; then
191                 echo "$1 dead but subsys locked"
192                 return 2
193         fi
194         echo "$1 is stopped"
195         return 3
196 }
This page took 0.039385 seconds and 4 git commands to generate.