forked from adafruit/adafruit-pi-cam
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathphotobooth
executable file
·159 lines (146 loc) · 2.93 KB
/
photobooth
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#!/bin/bash
# /etc/init.d/photobooth
### BEGIN INIT INFO
# Provides: photobooth
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Starts the raspberry pi photobooth
# Description: Starts the raspberrypi photobooth
### END INIT INFO
# Loading path for CMDS
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
#Loading profile to have, for example, JAVA_HOME set
. /etc/profile
#The service name
NAME=photobooth
#THE COMMAND TO LAUNCH
DAEMON=/opt/photobooth/startphotobooth
#File where log will be placed (/dev/null if empty)
LOGFILE=/var/log/$NAME
#File where pid will be store, leave as is if you don't know what you do
PIDFILE=/var/run/$NAME.pid
#User who will be owner of the process, root if empty
USER=
processRunning() {
kill -0 $1 &> /dev/null
if [ "$?" == "0" ]
then
echo 0
else
if [ -f $PIDFILE ]
then
rm $PIDFILE
fi
echo 1
fi
}
daemonRunning() {
if [ -f $PIDFILE ]
then
echo $(processRunning $(cat $PIDFILE))
else
echo 1
fi
}
waitForCompletion() {
echo "Service $NAME is being stopped"
while [ $(daemonRunning) == 0 ]
do
sleep 1
done
}
start() {
CMD="nohup "
if [ $(daemonRunning) == 1 ]
then
if [ -x $DAEMON ]
then
CMD="$CMD $DAEMON"
if [ "$LOGFILE" != "" ]
then
if [ ! -f $LOGFILE ]
then
touch $LOGFILE
if [ "$USER" != "" ]
then
chown $USER:$(id -gn $USER) $LOGFILE
fi
fi
CMD="$CMD 1>> $LOGFILE 2>>$LOGFILE"
else
CMD="$CMD &> /dev/null"
fi
CMD="$CMD &"
CMD="$CMD echo \$!"
if [ "$USER" != "" ]
then
CURRPID=$(su $USER -c "$CMD")
else
CURRPID=$(su -c "$CMD")
fi
echo $CURRPID > $PIDFILE
echo -e "Service $NAME \e[0;32mSTARTED\e[0m"
else
echo "The daemon file $DAEMON is not executable"
fi
else
echo -e "Service $NAME already running, pid: \e[1;34m$(cat $PIDFILE)\e[0m"
fi
}
# Restart the service FOO
stop() {
CURRPID=$(cat $PIDFILE)
if [ $(daemonRunning) == 0 ]
then
kill $CURRPID
waitForCompletion
echo -e "Service $NAME : \e[0;31mSTOPPED\e[0m"
else
echo "Service $NAME is not currently running."
fi
}
forcestop() {
CURRPID=$(cat $PIDFILE)
if [ $(daemonRunning) == 0 ]
then
kill -9 $CURRPID
waitForCompletion
echo -e "Service $NAME : \e[0;31mSTOPPED\e[0m"
else
echo "Service $NAME is not currently running."
fi
}
### main logic ###
case "$1" in
start)
start
;;
stop)
stop
;;
status)
if [ $(daemonRunning) == "0" ]
then
echo "Service $NAME running under pid $(cat $PIDFILE)"
else
echo "Service is stopped"
fi
;;
reload)
stop
start
;;
force-reload)
forcestop
start
;;
force-stop)
forcestop
;;
*)
echo $"Usage: $0 {start|stop|reload|force-reload|force-stop|status}"
exit 1
esac
exit 0