Created
January 23, 2020 05:09
-
-
Save b0o/0202c4e6e63b3e254e1355bdec7e1313 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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