-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexecpipe.sh
More file actions
executable file
·95 lines (81 loc) · 1.97 KB
/
execpipe.sh
File metadata and controls
executable file
·95 lines (81 loc) · 1.97 KB
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
#!/bin/bash
# Author: smirnov.arkady@gmail.com
# INSTALL
#
# Add to crontab:
# * * * * * /path/to/execpipe.sh status || /path/to/execpipe.sh start
#
# Sample:
# find . -name '*.mp3' ! -path '*previews*' -exec bash -c 'echo {} && echo {} >/tmp/execpipe_cmd' \;
#
PIPE=/tmp/execpipe_cmd
PIDFILE=/var/run/execpipe.pid
HANDLER=./test.sh
# delay between executions
EXECSLEEP=0.5
PIPECHMOD=0777
PIPECHOWN=root.root
check_status() {
if [ ! -f $PIDFILE ]; then
echo 'Could not open ('$PIDFILE')'
return 1
elif ! kill -0 `cat $PIDFILE` &>/dev/null; then
echo 'Calling app pid (='`cat $PIDFILE`') is not reposnding.'
return 1
fi
}
run_daemon() {
#if [[ ! -p $PIPE ]]; then
rm -f "$PIPE" "$PIPE.out" 2>/dev/null
if mkfifo "$PIPE" "$PIPE.out"; then
chmod $PIPECHMOD "$PIPE"
chmod 700 "$PIPE.out"
else
return 1
fi
#fi
while true; do
while IFS="" read -r -d $'\n' line; do
printf '%s\n' "${line}"
done <$PIPE >$PIPE.out &
bgproxy=$!
exec 3>$PIPE
exec 4<$PIPE.out
while IFS="" read -r -d $'\n' <&4 line; do
#echo `date +%s` $line >> /tmp/zzzdsfdf
[ -x "$HANDLER" ] && $HANDLER $line
sleep $EXECSLEEP
done &
bgreader=$!
trap "kill -TERM $bgproxy;kill -TERM $bgreader; echo 'bgproxy=$bgproxy bgreader=$bgreader'; rm -f '$PIPE'; exit" 0 1 2 3 13 15
wait "$bgproxy"
echo "restarting..."
done
}
# ---------------
case "$1" in
daemon)
run_daemon
;;
status)
check_status
;;
start)
if ! touch $PIDFILE >/dev/null 2>&1 && [ ! -w $PIDFILE ]; then
echo "PIDFILE (=$PIDFILE) not writeable" >&2
elif ! check_status >/dev/null; then
echo 'Daemonize'
nohup $0 daemon >/dev/null 2>&1 &
echo $! > $PIDFILE
else
echo 'Already running'
fi
;;
stop)
kill `cat $PIDFILE`
;;
*)
echo "Usage: $0 {start|stop|status}" >&2
exit 1
;;
esac