]> git.decadent.org.uk Git - nfs-utils.git/blob - etc/nodist/nfs-functions
2002-04-07 H.J. Lu <hjl@lucon.org>
[nfs-utils.git] / etc / nodist / nfs-functions
1 #!/bin/bash
2 #
3 # An attempt at a simple, distribution-independent daemon management.
4 #
5 # Copyright (C) 2000-2001 Mission Critical Linux, Inc.
6 #
7
8 # daemon functions
9 getpid()
10 {
11     pid=''
12     if [ -f /var/run/${base}.pid ] 
13     then
14         pid=`head -1 /var/run/${base}.pid`
15     fi
16     if [ "$pid" = "" ]
17     then
18         pid=`pidof $1`
19     fi
20     if [ "$pid" = "" ]
21     then
22         pid=`ps ax | awk 'BEGIN { prog=ARGV[1]; ARGC=1 }
23                    { if ((prog == $5) || (("(" prog ")") == $5) ||
24                      (("[" prog "]") == $5) ||
25                    ((prog ":") == $5)) { print $1 ; exit 0 } }' $1`
26     fi
27
28     echo $pid
29 }
30
31 startdaemon()
32 {
33     base=`basename $1`
34
35     # check if it is already running
36     pid=`getpid $base`
37     [ -n "$pid" ] && ps h $pid >/dev/null 2>&1 && echo " already running." && return
38
39     # don't dump core
40     ulimit -c 0
41
42     $* && echo " done." || echo " failed."
43 }
44
45 stopdaemon()
46 {
47     base=`basename $1`
48     pid=`getpid $base`
49
50     if [ "$pid" != "" ]
51     then
52         if ps h $pid>/dev/null 2>&1
53         then
54             kill -TERM $pid
55             if ps h $pid>/dev/null 2>&1
56             then
57                 sleep 1
58                 if ps h $pid>/dev/null 2>&1
59                 then
60                     sleep 3
61                     if ps h $pid>/dev/null 2>&1
62                     then
63                         kill -KILL $pid
64                         sleep 2
65                     fi
66                 fi
67             fi
68         fi
69         ps h $pid >/dev/null 2>&1
70         RC=$?
71         [ $RC -eq 0 ] && echo " failed." || echo " done."
72         [ $RC -eq 0 ] || rm -f /var/run/$base.pid >/dev/null 2>&1
73     else
74         echo " not running."
75     fi
76 }
77
78 daemonstatus()
79 {
80     base=`basename $1`
81     pid=`getpid $base`
82     
83     if [ "$pid" != "" ]
84     then
85         if ps h $pid >/dev/null 2>&1
86         then
87             echo "$base (pid $pid) is running."
88             return 0
89         else
90             if [ -f /var/run/${base}.pid ]
91             then
92                 echo "$base dead but pid file exists."
93                 return 1
94             else
95                 echo "$base is stopped."
96                 return 2
97             fi
98         fi
99     else
100         echo "$base is stopped."
101         return 3
102     fi
103 }
104