Skip to content

Instantly share code, notes, and snippets.

@b0o
Created January 23, 2020 05:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save b0o/0202c4e6e63b3e254e1355bdec7e1313 to your computer and use it in GitHub Desktop.
Save b0o/0202c4e6e63b3e254e1355bdec7e1313 to your computer and use it in GitHub Desktop.
#!/bin/bash
declare sway_lsof_f="/dev/null"
declare -i sleep_duration=1
declare -i sway_pid
sway_pid="$(pgrep -x sway)" || {
echo "sway process not found" >&2
return 1
}
sway_ps() {
ps --no-heading -ocommand "$sway_pid"
}
sway_cmd="$(sway_ps)"
sway_lsof() {
# ensure sway is still running
if [[ "$sway_cmd" != "$(sway_ps)" ]]; then
echo "sway command '$sway_cmd' disappeared" >&2
return 1
fi
local f l
f="/tmp/sway_lsof-$(date +%s)"
l="$(lsof -F 0tfn -p "$sway_pid" | tr '\0' '\t' | column -tN5 | sort -k 2)"
if [[ -e $sway_lsof_f ]]; then
diff "$sway_lsof_f" <(cat <<< "$l") && return 0
fi
echo "$l" > "$f"
echo "total open files: $(wc -l "$f")" >&2
if [[ -f "$sway_lsof_f" ]]; then
rm "$sway_lsof_f"
fi
sway_lsof_f="$f"
}
trap 'rm $sway_lsof_f 2>/dev/null' EXIT
while sway_lsof; do
sleep $sleep_duration
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment