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