Can anyone see my syntax error here? Trying to edit / update cron job, but the file is not updated.
crontab -l | sed 's%*/5 * * * * cd /home/administrator/anm-1.5.0 && ./anm.sh%*/10 * * * * cd /home/administrator/anm-1.5.0 && ./anm.sh%' | crontab -
* UPDATE *
So I still have problems with this. Ultimately, I try to extract the value from the $ FREQ (minutes) configuration file to complete the task. The script first checks to see if the value in the configuration is different than the value currently in crontab. If the value is different, it will update crontab with the new value. Crontab (both the initial installation and updates) also pulls the running directory and script name from the variables. Example:
DIR=`pwd` SCRIPT=`basename $0` CRONTMP=`crontab -l | grep anm.sh` crontab -l | sed 's%'$CTMP'%*/'$FREQ' * * * * cd '$DIR' && ./'$SCRIPT'%' | crontab -
Something like that. Obviously this is not enough, but this should give you a general idea.
Thanks for the help!
* UPDATE *
So, everything is going forward, but I still have one tiny problem. I think I developed most of the logic. Here is the entire (relevant) part of the script so that you can understand what exactly I'm trying to execute.
Remember: $ SCRIPT and $ DIR are defined outside the function and are simply equal to the name of the scripts (for example, anm.sh) and the current working directory. And I accepted your offer and updated all my code. Now I use SCRIPT = $ (basename $ 0). Thanks
function CRON { if [ "`crontab -l | grep $SCRIPT`" \> " " ]; then CTMP=$(crontab -l | grep $SCRIPT) if [ "$CTMP" = "*/$FREQ * * * * cd $DIR && ./$SCRIPT" ]; then echo "$GREEN No modifications detected $RESET" else crontab -l | "sed s%$CTMP%*/$FREQ * * * * cd $DIR && ./$SCRIPT%" | crontab - fi else echo "$YELLOW CRON not detected - Installing defaults $RESET" (crontab -l ; echo "*/$FREQ * * * * cd $DIR && ./$SCRIPT") | crontab - fi }
Essentially, when a function runs, it first checks to see if the cron job is installed (perhaps this is the first time the script is run). If it does not detect anything, it adds the cron job to the crontab file. It's still great. Then, if the function detects that the cron job is installed, it compares it with the frequency (in minutes) set in the configuration file. If they are the same, no changes were made to the configuration file, and the script did not. Finally, if the values ββare really different, then he tries to update the corresponding line in the crontab file to reflect the changes made to the configuration file. This last part fails. Currently, it just overwrites the crontab file to empty.
* UPDATE *
There seems to be a serious problem with the next line. This incorrectly pulls the desired line from crontab and stores it in the CTMP variable:
CTMP=$(crontab -l | grep $SCRIPT)
when I repeat CTMP, I get a bunch of unexpected results. Apparently I'm using grep incorrectly here.
Well, this problem has been resolved. The variable was stored correctly, I just repeated it incorrectly.
* UPDATE 06/24/13 5:08 AM *
The last question seems to be a sed string. Here is the error message that I am currently studying.
sed s%*/12 * * * * cd /home/administrator/anm-1.5.0 && ./anm.sh%*/10 * * * * cd /home/administrator/anm-1.5.0 && ./anm.sh%: No such file or directory
It looks like it is trying to replace the string, but does not work.
* UPDATE 06/24/13 5:45 AM *
So the above error message was caused by my own stupidity. I have included sed in the quotes. Since then I removed the command from the quotation marks, however the problem still persists. I have tried single quotes, double quotes, escape codes * and. no luck. The cron file is still not updating. Here is the current code:
function CRON { if [ "`crontab -l | grep $SCRIPT`" \> " " ]; then CTMP="$(set -f; crontab -l | grep $SCRIPT)" if [ "$CTMP" = "*/$FREQ * * * * cd $DIR && ./$SCRIPT" ]; then echo "$GREEN No modifications detected $RESET" else crontab -l | sed "s%$CTMP%*/$FREQ * * * * cd $DIR && ./$SCRIPT%" | crontab - fi else echo "$YELLOW CRON not detected - Installing defaults $RESET" (crontab -l ; echo "*/$FREQ * * * * cd $DIR && ./$SCRIPT") | crontab - fi }
* UPDATE 06/24/13 6:05 AM *
Perhaps the problem is that I am not avoiding everything. those. Do the variables inside the sed expression have characters to escape? Could this be a problem? If so, then I donβt know exactly how to solve this. Please, help.
* SOLVED *
I really needed to avoid the variables and then pass them to sed. Here is the code:
CTMPESC=$(sed 's/[\*\.&]/\\&/g' <<<"$CTMP") DIRESC=$(sed 's/[\*\.&]/\\&/g' <<<"$DIR") SCRIPTESC=$(sed 's/[\*\.&]/\\&/g' <<<"$SCRIPT") crontab -l | sed "s%$CTMPESC%\*/$FREQ \* \* \* \* cd $DIRESC \&\& \./$SCRIPTESC%" | crontab -