For the install process (or really any changes you want to monitor that an executable produces), I have made a tool that monitors definable human readable files in a specific directory. You supply:
1 The folder to monitor, it will wait for it to exist to start if need be.
2 The pattern of files (eg *.conf) to read diffs from, if left blank will search all human readable files.
3 Where you want the diff files to live, default is /home/diffs.
You will have to run this, then execute your script in another terminal. I tried to launch scripts from it, and everything got gummed up.
It then spits out diff files whenever files change or are added, what script is running at that time, and what the difference is in which human readable file. I will tweak it more when I get some other things done. It needs to be recursive on demand. Need to be able to specify more folders.
diffmon1.6
Requires dialog and symlinks (local ubu repos)
#!/bin/bash
### This script is free.
#Needs to be the only instance running. Makes sure that happens but is name specific.
trap 'rm -r /tmp/difftmp' EXIT
procs=$(echo | ps -eo pid,user,args | grep 'diffmon.sh' | wc -l)
if (($procs > 3)); then echo "Multiple instances running. Terminating... please reload"; sleep 1; rm -r /tmp/difftmp; killall -v 'diffmon.sh'; fi
### Sets suffix numberer, installs dialog and cleans up after old operations
if [ -z $runonce ]; then
suffix=1
type -P dialog &>/dev/null || apt-get install dialog -y
type -P symlinks &>/dev/null || apt-get install symlinks -y
if [ -d /tmp/difftmp ]; then rm -r /tmp/difftmp; fi
if [ -e /tmp/ast1.txt ]; then rm /tmp/ast1.txt; fi
if [ -e /tmp/ast2.txt ]; then rm /tmp/ast2.txt; fi
runonce=something
getout=live
fi
### Sets the directory to monitor
thedir () {
searchdir1=$(dialog --title "Directory to Monitor" --stdout --inputbox "Please choose a folder to monitor
(eg /etc/asterisk ) cannot be blank" 14 48)
if [ $? = 1 ]; then exit 0; fi
}
if [ -z $searchdir1 ]; then thedir; fi
### Figures out if the directory to monitor exists already and makes sure you didn't typo
if [ ! -d $searchdir1 ]; then dialog --title "Confirm" --yesno "The directory $searchdir1 does not exist. Are you expecting it to be created by the process?" 10 48; fi
### Sets the file pattern to monitor
#filemon=$(dialog --title "File Pattern" --stdout --inputbox "Please indicate a file pattern to monitor (eg *.conf, or for all readable files leave blank)" 14 48)
### Sets the output folder for diff files/deleted files
difhome=$(dialog --title "Output" --stdout --inputbox "Please specify a folder to put diff files in. If it does not exist it will be created. If left blank will default to /home/diffs" 14 48)
if [ -z $difhome ];then difhome=/home/diffs; fi
### Main loop
rundiff () {
getin=die
symlinks -dr $searchdir1
### Waits for directory to exist and creates the temporary environment.
while [ ! -d $searchdir1 ]; do sleep 1; done
if [ ! -d $difhome ]; then mkdir $difhome; chmod 777 $difhome; fi
if [ ! -d /tmp/difftmp ]; then mkdir /tmp/difftmp; chmod 777 /tmp/difftmp; rsync -rtv --links $searchdir1/ /tmp/difftmp; fi
while true; do
if [ ! "$getout"="$getin" ]; then echo "-1"; break; fi
### Figures out if files are trying to be deleted by something, and moves them for review. If they come into existence then that is noted, but not read in diff.
home=$(ls -R $searchdir1 | wc -l)
target=$(ls -R /tmp/difftmp | wc -l)
if [ "$home" -lt "$target" ]; then
if [ ! -d $difhome/deleted ]; then mkdir -p $difhome/deleted; fi
mover=$(diff -r $searchdir1 /tmp/difftmp | grep Only | sed -e 's/Only in /rsync -avb --remove-source-files /g;s/\: /\/*/g')
movdir=$(echo "$mover $difhome/deleted")
# remover=$(diff -r $searchdir1 /tmp/difftmp | grep Only| sed -e 's/Only in /rm -r /g;s/\: /\/*/g')
$movdir
# cleanrm=true;
fi
### Finds diffs and spits them into format
if (diff -r $searchdir1 /tmp/difftmp); then sleep 1; else
if [ -z $filemon ]; then filemon=$(find $searchdir1 -name '*' -prune -readable -type f | sed 's_.*/__'); fi
if [ ! -e /tmp/ast1.txt ]; then
echo | ls -l $searchdir1 > /tmp/ast1.txt;
input_variable=$"diffmon"${suffix}
iv2=$input_variable;
echo "" > $difhome/$iv2
echo "" >> $difhome/$iv2
echo " ************** $iv2 ***************" >> $difhome/$iv2
echo "" >> $difhome/$iv2
echo "Files added > or removed <" >> $difhome/$iv2
(echo | diff /tmp/ast1.txt /tmp/ast2.txt >> $difhome/$iv2);
iv1=$(ls $searchdir1/$filemon);
echo "" >> $difhome/$iv2
echo "****** Scripts running during change " >> $difhome/$iv2
(echo | ps -eo pid,user,args | grep 'bin/bash' >> $difhome/$iv2);
for iv1diff in $iv1; do
if ! diff -r $searchdir1/$iv1diff /tmp/difftmp/$iv1diff; then
echo "" >> $difhome/$iv2
echo "********* Changes in $searchdir1/$iv1diff" >> $difhome/$iv2
(diff -r $searchdir1/$iv1diff /tmp/difftmp/$iv1diff >> $difhome/$iv2)
fi
done
echo "Diff $difhome/$iv2 written";
rsync -azdr $searchdir1/ /tmp/difftmp
if [ -e /tmp/ast2.txt ]; then rm /tmp/ast2.txt; fi
let suffix=$suffix+1
else
### Redundant function allowing two files to always be contrasted and never confused
echo | ls -l $searchdir1 > /tmp/ast2.txt;
input_variable=$"diffmon"${suffix}
iv2=$input_variable;
echo "" > $difhome/$iv2
echo "" >> $difhome/$iv2
echo " ************** $iv2 ***************" >> $difhome/$iv2
echo "" >> $difhome/$iv2
echo "Files added > or removed <" >> $difhome/$iv2
(echo | diff /tmp/ast1.txt /tmp/ast2.txt >> $difhome/$iv2);
iv1=$(ls $searchdir1/$filemon);
echo "" >> $difhome/$iv2
echo "****** Scripts running during change " >> $difhome/$iv2
(echo | ps -eo pid,user,args | grep 'bin/bash' >> $difhome/$iv2);
for iv1diff in $iv1; do
if ! diff -r $searchdir1/$iv1diff /tmp/difftmp/$iv1diff; then
echo "" >> $difhome/$iv2
echo "********* Changes in $searchdir1/$iv1diff" >> $difhome/$iv2
(diff -r $searchdir1/$iv1diff /tmp/difftmp/$iv1diff >> $difhome/$iv2)
fi
done
echo "Diff $difhome/$iv2 written";
rsync -azdr $searchdir1/ /tmp/difftmp
if [ -e /tmp/ast1.txt ]; then rm /tmp/ast1.txt; fi
let suffix=$suffix+1
fi
### Backup insist one contrast file exist at all times.
if [ ! -e /tmp/ast1.txt ] && [ ! -e /tmp/ast2.txt ]; then echo | ls -l $searchdir1 > /tmp/ast1.txt; fi
fi
sleep 1
done
}
### Confirms input information is correct before launching
dialog --title "Confirm" --yesno "Choose ok to begin, or cancel to exit and start over" 10 48;
if [ $? = 0 ]; then clear
rundiff
fi
else
exit 0
I would appreciate feedback