Wednesday, April 27, 2016

pids and cronjobs and scripts stomping on each other

 i am backing up a whole lot of data via a cronjob.   
 sometimes it takes a really long time. like so long   
 to bleeds over to the next backup cycle. this will   
 help me not run stuff in parallel. yuck. processes   
 stomping all over themselves is no fun.  
   
 this script sets the PID (process id) in a standard place.  
 if the PID is present, the script halts.  
 if the PID is not there, the script creates the PID file.  
 and continues along working.  
 but, if it cannot create, the script dies.  
   
 if the PID isn't present, the script creates the PID file.  
 and continues along working.  
 but, if it cannot create, the script dies.  
   
   
   
    
 PIDFILE=/var/run/script_name.pid  
   
 if [ -f $PIDFILE ]  
 then  
  PID=$(cat $PIDFILE)  
  ps -p $PID > /dev/null 2>&1  
  if [ $? -eq 0 ]  
  then  
   echo "process already running"   
   echo "process already running" | mail -s me@here.org   
   exit 1  
  else  
   echo $$ > $PIDFILE  
   if [ $? -ne 0 ]  
   then  
    echo "could not create PID file"  
    exit 1  
   fi  
  fi  
 else  
  echo $$ > $PIDFILE  
  if [ $? -ne 0 ]  
  then  
   echo "could not create PID file"  
   exit 1  
  fi  
 fi  
   
 work work work  
   
 # remove PID file  
 rm -f $PIDFILE  
     
   
Post a Comment