watch
is not capable of doing this directly. Although you can highlight differences in the command output (via option -d
) or exit when the output changes (via option -g
) it is not possible to display the output of more than one run. But you can achieve this via other common tools.
One of many possible solutions:
last=""; while true; do cur="$(ps h -o nlwp -p <PID>)"; if ! [ "$cur" = "$last" ]; then last="$cur"; echo "$(date) $cur"; fi; sleep 1; done
Or more readable:
last=""
while true
do
cur="$(ps h -o nlwp -p <PID>)"
if ! [ "$cur" = "$last" ]
then
last="$cur"
echo "$(date) $cur"
fi
sleep 1
done
Explanation: The ps
option -o nlwp
directly prints the number of threads, so you don't have to call wc -l
. You can also use $(pidof programname)
instead of <PID>
in order to determine the process ID automatically. I also added the current date via $(date)
which seems useful to me. If you don't like it then just remove it.