forked from byxorna/scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsample_init_script.sh
96 lines (91 loc) · 1.78 KB
/
sample_init_script.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/bin/bash
# sample init script, for templating
# written Aug 4 2009
#
# /etc/rc.d/init.d/
# Source function library.
SCRIPTNAME='test_wireless.pl'
. /lib/lsb/init-functions
PIDFILE="/var/run/$SCRIPTNAME"
LOGFILE="/var/log/test_wireless"
start() {
if [ -f "$PIDFILE" ] ; then
PID=`/bin/cat $PIDFILE`
if [[ $PID && `/bin/ps -ef | /bin/grep $PID | /bin/grep -v grep` ]] ; then
echo "$SCRIPTNAME is already running: $PID"
exit 2;
else
start
fi
else
echo "Starting $SCRIPTNAME"
# create the PIDFILE now
if [[ -e "$PIDFILE" && -f "$PIDFILE" && -w "$PIDFILE" && -r "$PIDFILE" ]]; then
true
else
touch "$PIDFILE"
chmod 644 "$PIDFILE"
fi
# daemonize the script
/home/noc/$SCRIPTNAME &
testpid=$$
echo $$
# check if the script started sucessfully
if [ $? -eq 0 ] ; then
PID=`/bin/ps -ef | /bin/grep "$SCRIPTNAME" | /bin/grep -v grep | /usr/bin/awk '{print $2}'`
echo $PID > $PIDFILE
else
echo "something bad happened starting $SCRIPTNAME"
rm -f "$PIDFILE"
return 3
fi
fi
return
}
status() {
if [ -f "$PIDFILE" ] ; then
PID=`/bin/cat $PIDFILE`
if [ -z $PID ] ; then
echo "$SCRIPTNAME not running"
rm -f "$PIDFILE"
return
else
echo "$SCRIPTNAME is running with PID $PID"
return
fi
else
echo "$SCRIPTNAME is not running"
return
fi
}
stop() {
echo "Shutting down $SCRIPTNAME"
if [[ -f "$PIDFILE" && -r "$PIDFILE" ]] ; then
PID=`/bin/cat "$PIDFILE"`
kill $PID
rm -f "$PIDFILE"
else
echo "$SCRIPTNAME wasnt running!"
fi
return
}
case "$1" in
start)
start
;;
stop)
stop
;;
status)
status
;;
restart)
stop
start
;;
*)
echo "Usage: {start|stop|status|restart"
exit 1
;;
esac
exit $?