Para fazer isso, você precisará armazenar a saída de ps aux
em uma variável e fazer um pós-processamento com ferramentas como awk
, sed
e similares.
Aqui está um exemplo de tal implementação:
#!/bin/bash
PS_OUT="$(ps aux)"
while true
do
NEW_OUT="$(ps aux)"
# quick explanation :
# - NR==FNR : in the case of the 1st file
# -> we create the dictionary : a[line] = 1
# - NR!=FNR : in the case of the 2nd file
# -> we add 2 to the value of the dictionary a[line]
awk 'NR==FNR{a[$0]++} NR!=FNR{a[$0]=a[$0]+2}
END{
# for every existing key in our dictionary ...
for (l in a)
{
# if a[line] is equal to 1, it was only encountered in the first file
if (a[l]==1) print "process removed : "l
# if a[line] is equal to 2, it was only encountered in the second file
if (a[l]==2) print "process added : "l
# if it was encountered in both files, it should be equal to 3
}
# we substitute the content of our variables so that awk considers them as input files
}' <(echo "$PS_OUT") <(echo "$NEW_OUT")
PS_OUT="$NEW_OUT"
sleep 5
done