#!/bin/sh
# check and clean left over sync job pids of running syncs in /var/tmp/rsync.lock.set* /tmp/rsync.lock.set*
# fixes problem with syncs failing to start due to left over old pid
# very specific script two pids are created in /tmp and /var/tmp one by one program other by other
# this one checks if one is missing of both and if not deletes it
# check and clean left over sync job pids of running syncs in /var/tmp/rsync.lock.set* /tmprsync.lock.set*
# fixes problem with syncs failing to start due to left over old pid

# >/dev/null 2>&1
out_log=/root/rsync_timeout.log;
 for file in /var/tmp/rsync.lock.set*; do 
 name=${file##*/};
if [[ -f "/tmp/$name" ]] && [[ ! -f "/var/tmp/$name" ]] && [[ $(ps axu|grep -i $name | grep -v grep | wc -l) -eq 0 ]]; then 
echo "rsync process $name and pid $name missing in /tmp/$name but existing in /var/tmp/$name"; 
echo "$(date) Found PROBLEM !! Running: rm -f /var/tmp/$name . Cleaning obsolete left pid $name" | tee -a $out_log; 
rm -f /tmp/$name;  fi;  if [[  -f "/var/tmp/$name" ]] && [[ ! -f "/tmp/$name" ]] && [[ $(ps axu | grep -i $name | grep -v grep | wc -l) -eq 0 ]]; then 
echo "$name exists in both directories"; 
echo "rsync process $name and pid missing in /var/tmp/$name but existing in /tmp/$name"; 
echo "$(date) Found PROBLEM /var/tmp/rsync.lock.set* !! Running: rm -f /var/tmp/$name . Cleaning obsolete left pid $name" | tee -a $out_log; echo rm -f /var/tmp/$name; fi; 
done 

 for file1 in /tmp/rsync.lock.set*; do
 name1=${file1##*/}; 
if [[ -f "/tmp/$name1" ]] && [[ ! -f "/var/tmp/$name1" ]] && [[ $(ps axu|grep -i $name | grep -v grep | wc -l) -eq 0 ]]; then 
echo "rsync process $name1 and pid $name1 missing in /tmp/$name1 but existing in /var/tmp/$name1"; 
echo "$(date) Found PROBLEM /tmp/rsync.lock.set* !! Running: rm -f /var/tmp/$name1 . Cleaning obsolete left pid $name1" | tee -a $out_log;
rm -f /tmp/$name1; 
fi; if [[ ! -f "/tmp/$name1" ]] && [[ -f "/var/tmp/$name1" ]] && [[ $(ps axu|grep -i $name | grep -v grep | wc -l) -eq 0 ]]; then
echo "rsync process $name1 and pid $name1 missing in /var/tmp/$name1 but existing in /var/tmp/$name1";
echo "$(date) Found PROBLEM /tmp/rsync.lock.set.* !! Running: rm -f /var/tmp/$name1 . Cleaning obsolete left pid $name1" | tee -a $out_log; echo rm -f /var/tmp/$name1; fi;
done 
