-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstatus.sh
executable file
·129 lines (113 loc) · 2.28 KB
/
status.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
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
#!/bin/bash
path=$(dirname "$(realpath $0)")
if [ ! -z $HYPRLAND_INSTANCE_SIGNATURE ]; then
echo "Mode : Wayland/hyprland"
name="hyprland_status"
elif [ $XDG_SESSION_TYPE = "wayland" ]; then
echo "Mode : Wayland";
name="wayland_status"
else
echo "Mode : Xorg";
name="status"
if ! pgrep -x "Xorg" >/dev/null;
then
echo "Xorg needs to be started first"
exit
fi
fi
start() {
cd "$path" && "$path"/$name & disown
exit 0;
}
is_running(){
pgrep -x $name > /dev/null
}
is_running_echo(){
is_running && echo "$name is running" || echo "$name is not running"
}
stop(){
killall $name && echo "Stopped instances"
}
daemon(){
echo "Starting daemon..."
while :
do
is_running && sleep 10 || start > /dev/null
done & disown
sleep 0.1
is_running && echo "Daemon started !" || echo "Daemon failed"
}
daemon_running(){
processes=($(pgrep -x "$(basename $0)"))
if [ ${#processes[@]} = 1 ];
then
false
else
true
fi
# return $(echo "${#processes[@]/$$}" | bc)
}
daemon_running_echo(){
daemon_running && echo "$name daemon is running" || is_running_echo
}
daemon_kill(){
# for all status.sh ($0) process except it's own pid
processes=($(pgrep -x "$(basename $0)"))
if [ ${#processes[@]} = 1 ];
then
echo "No daemon is running"
exit
fi
for p in "${processes[@]/$$}"
do
[ "$p" = "" ] && continue
kill "$p"
echo "Killing daemon $p"
done && printf "\r" || echo "No daemon terminated"
}
print_help(){
echo "Usage : $0 [status | start | stop | restart | daemon | kill]"
}
if [ "$1" = '' ];
then
daemon_running_echo
print_help
exit
fi
case "$1" in
"status")
daemon_running_echo
exit
;;
"start")
is_running && echo "./status is already running" || start
exit
;;
"restart")
is_running && stop
start
exit
;;
"stop")
stop
exit
;;
"daemon")
daemon_running && echo "Daemon already running" || daemon
exit
;;
"kill")
daemon_kill
exit
;;
*)
echo "Unknown command."
print_help
exit
;;
esac
exit
while true
do
"$path"/status
done