title: Updating CRON with bash script
body: <p>Can anyone see my syntax error here? Trying to edit/update a cron job, but the file is not being updated.</p>
  
  <pre><code>crontab -l | sed 's%*/5 * * * * cd /home/administrator/anm-1.5.0 &amp;&amp; ./anm.sh%*/10 * * * * cd /home/administrator/anm-1.5.0 &amp;&amp; ./anm.sh%' | crontab -
  </code></pre>
  
  <p><strong>* UPDATE *</strong></p>
  
  <p>So im still having trouble with this. Ultimately I am trying to pull a value from a config file $FREQ (minutes) to run the job. The script will first check to see if the value in the config is different than the value currently in the crontab. If the value is different, it will update the crontab with the new value. The crontab (both initial install and updates) also pulls the running directory and script name from variables. Example:</p>
  
  <pre><code>DIR=`pwd`
  SCRIPT=`basename $0`
  CRONTMP=`crontab -l | grep anm.sh`
  crontab -l | sed 's%'$CTMP'%*/'$FREQ' * * * * cd '$DIR' &amp;&amp; ./'$SCRIPT'%' | crontab -
  </code></pre>
  
  <p>Something along those lines. Obviously this is missing a few things, but that should give you the general idea.</p>
  
  <p>Thanks for the help!</p>
  
  <p><strong>* UPDATE *</strong></p>
  
  <p>Ok so things are moving along, but I am still having one tiny problem. I think I have most of the logic worked out. Here is the entire (pertinent) part of the script so you can get a feel for exactly what I am trying to accomplish.</p>
  
  <p>Remember: $SCRIPT and $DIR are defined outside the function and are just equal to the scripts name (anm.sh for example), and the current working directory. And I took your suggestion and updated all my code. I now use SCRIPT=$(basename $0). Thanks</p>
  
  <pre><code>function CRON {
    if [ "`crontab -l | grep $SCRIPT`" \&gt; " " ]; then
      CTMP=$(crontab -l | grep $SCRIPT)
      if [ "$CTMP" = "*/$FREQ * * * * cd $DIR &amp;&amp; ./$SCRIPT" ]; then
        echo "$GREEN No modifications detected $RESET"
      else
        crontab -l | "sed s%$CTMP%*/$FREQ * * * * cd $DIR &amp;&amp; ./$SCRIPT%" | crontab -
      fi
    else
      echo "$YELLOW CRON not detected - Installing defaults $RESET"
      (crontab -l ; echo "*/$FREQ * * * * cd $DIR &amp;&amp; ./$SCRIPT") | crontab -
    fi
  }
  </code></pre>
  
  <p>Essentially, when the function runs, it first checks to see if the cron job is even installed (perhaps this is the first time the script has run). If it doesn't detect anything, it appends the cron job to the crontab file. This works great so far. Next, if the function does detect that the cron job has been installed, it compares it vs the frequency (in minutes) set in the configuration file. If they are the same, no modification has been made to the config file and the script moves on. Lastly, if the values are indeed different, it then tries to update the corresponding line in the crontab file to reflect the changes made in the configuration file. This last part fails. Currently its just overwriting the crontab file completely to blank.</p>
  
  <p><strong>* UPDATE *</strong></p>
  
  <p>It looks like there is a major issue with the following line. This is not properly pulling the desired line out of the crontab and storing it to the variable CTMP:</p>
  
  <pre><code>CTMP=$(crontab -l | grep $SCRIPT)
  </code></pre>
  
  <p>when I echo out CTMP, I get a bunch of unexpected results. Apparently I am using grep incorrectly here.</p>
  
  <p>Ok this issue was resolved. The variable was being stored correctly, I was just echoing it out incorrectly.</p>
  
  <p><strong>* UPDATE 06/24/13 5:08am *</strong></p>
  
  <p>The last issue seems to be the sed line. Here is the error message I am researching now.</p>
  
  <pre><code>sed s%*/12 * * * * cd /home/administrator/anm-1.5.0 &amp;&amp; ./anm.sh%*/10 * * * * cd /home/administrator/anm-1.5.0 &amp;&amp; ./anm.sh%: No such file or directory
  </code></pre>
  
  <p>It looks like it's trying to replace the line, but failing.</p>
  
  <p><strong>* UPDATE 06/24/13 5:45am *</strong></p>
  
  <p>So the error message above was ofcourse caused by my own stupidity. I was including sed inside the quotes. I have since removed the command from the quotes, however the issue still persists. I have tried single quotes, double quotes, escaping * and . with no luck. The cron file is still not updating. Here is the current code:</p>
  
  <pre><code>function CRON {
    if [ "`crontab -l | grep $SCRIPT`" \&gt; " " ]; then
      CTMP="$(set -f; crontab -l | grep $SCRIPT)"
      if [ "$CTMP" = "*/$FREQ * * * * cd $DIR &amp;&amp; ./$SCRIPT" ]; then
        echo "$GREEN No modifications detected $RESET"
      else
        crontab -l | sed "s%$CTMP%*/$FREQ * * * * cd $DIR &amp;&amp; ./$SCRIPT%" | crontab -
      fi
    else
      echo "$YELLOW CRON not detected - Installing defaults $RESET"
      (crontab -l ; echo "*/$FREQ * * * * cd $DIR &amp;&amp; ./$SCRIPT") | crontab -
    fi
  }
  </code></pre>
  
  <p><strong>* UPDATE 06/24/13 6:05am *</strong></p>
  
  <p>Perhaps the issue is I'm not escaping everything. ie, the variables inside the sed expression once expanded have characters that need escaping? Could that be the issue? If so, im not exactly sure how to resolve this. Please help.</p>
  
  <p><strong>* SOLVED *</strong></p>
  
  <p>I did indeed have to escape the variables as well before passing them to sed. Here is the code:</p>
  
  <pre><code>CTMPESC=$(sed 's/[\*\.&amp;]/\\&amp;/g' &lt;&lt;&lt;"$CTMP")
  DIRESC=$(sed 's/[\*\.&amp;]/\\&amp;/g' &lt;&lt;&lt;"$DIR")
  SCRIPTESC=$(sed 's/[\*\.&amp;]/\\&amp;/g' &lt;&lt;&lt;"$SCRIPT")
  crontab -l | sed "s%$CTMPESC%\*/$FREQ \* \* \* \* cd $DIRESC \&amp;\&amp; \./$SCRIPTESC%" | crontab -
  </code></pre>