-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmpv-active-sockets
executable file
·38 lines (33 loc) · 1.01 KB
/
mpv-active-sockets
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
#!/usr/bin/env bash
# Removes any inactive mpv sockets, lists active mpv sockets
# Exits unsuccessfully if no sockets are active
declare default_tmp_dir default_socket_dir
default_tmp_dir="${TMPDIR:-/tmp}"
default_socket_dir="${default_tmp_dir}/mpvsockets"
readonly SOCKET_DIR="${MPV_SOCKET_DIR:-${default_socket_dir}}"
# remove any mpv sockets which fail to respond to a basic command
is_active_socket() {
# send a command to determine if the socket
# is still active.
# If its dead, removes the socket and returns 1
if socat - "$1" <<<'{ "command": ["get_property", "path"] }' >/dev/null 2>&1; then
return 0
else
rm "$1" >&2
return 1
fi
}
# find all active sockets, remove any that aren't active
declare -a active_sockets
active_sockets=()
while read -r -d $'\0' socket; do
if is_active_socket "${socket}"; then
active_sockets+=("${socket}")
fi
done < <(find "${SOCKET_DIR}" -type s -print0)
((${#active_sockets[@]} == 0)) && exit 1
{
for socket in "${active_sockets[@]}"; do
echo "${socket}"
done
} | sort -n