与bash脚本更新科雷 [英] Updating CRON with bash script

查看:143
本文介绍了与bash脚本更新科雷的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人能看到我的语法错误吗?尝试编辑/更新cron作业,但该文件没有被更新。

 的crontab -l | SED的%* / 5 * * * * CD /home/administrator/anm-1.5.0&放大器;&安培; ./anm.sh%*/10 * * * *光盘/home/administrator/anm-1.5.0&放大器;&放大器; ./anm.sh%| crontab中 - 

*更新*

因此​​,进出口仍然有这个麻烦。最后,我想从一个配置文件$频(分钟)拉值运行作业。该脚本会先检查,看看是否在配置的值比目前在crontab的值不同。如果该值是不同的,它会更新为新值的crontab。在crontab(初始安装和更新)也从拉变量运行目录和脚本名称。例如:

  DIR =`pwd`
SCRIPT =`基名$ 0`
CRONTMP =`的crontab -l | grep的anm.sh`
crontab的-l | SED的%'$ CTMP%* /'$ FREQ* * * * CD'$ DIR'和;&安培; ./'$SCRIPT'%| crontab中 -

类似的规定。显然,这是缺少了一些东西,但应该给你的总体思路。

感谢您的帮助!

*更新*

确定这样的事情是沿着感动,但我仍然有一个很小的问题。我想我有大部分的逻辑制定。下面是脚本的整个(相关)的一部分,所以你可以得到一个感受正是我试图完成。

记住:$ SCRIPT和$ DIR在函数外部定义的,刚好等于脚本的名称(anm.sh为例),和当前工作目录。我把你的建议,并更新了所有我的code。我现在用SCRIPT = $($基名0)。谢谢

 功能科雷{
  如果[`的crontab -l | grep的$ SCRIPT`\\> ];然后
    CTMP = $(的crontab -l | grep的$ SCRIPT)
    如果[$ CTMP=* / $ FREQ * * * * CD $ DIR功放&;&安培; ./$SCRIPT];然后
      回声$绿色无修改检测$ RESET
    其他
      crontab的-l | sed的S%$%CTMP * / $ FREQ * * * * CD $ DIR功放&;&安培; ./$SCRIPT%| crontab中 -
    科幻
  其他
    回声$ YELLOW CRON未检测到 - 安装的默认值$ RESET
    (crontab的-l;回声* / $ FREQ * * * * CD $ DIR功放&;&安培; ./$SCRIPT)| crontab中 -
  科幻
}

从本质上讲,功能运行时,它首先检查是否即使安装了cron作业(也许这是第一次脚本运行)。如果没有检测到任何东西,它追加cron作业crontab文件。这个伟大的工程至今。接下来,如果函数检测到cron作业已安装完毕,进行比较VS在配置文件中设置的频率(分钟)。如果它们是相同的,无修改已取得的配置文件和脚本上移动。最后,如​​果值是确有不同,它将尝试更新的crontab文件的相应行,以反映在配置文件中所做的更改。这最后一部分将失败。目前,它只是覆盖crontab文件完全空白。

*更新*

它看起来像有一个与以下行的一个主要问题。这是不正确牵拉所需的行出的crontab并将其存储到变量CTMP:

  CTMP = $(的crontab -l | grep的$ SCRIPT)

当我回声出CTMP,我得到了一堆意想不到的效果。显然,我使用grep错误在这里。

好这个问题就解决了​​。该变量是存储正确,我只是呼应它不正确。

*更新13年6月24日上午05时08 *

最后一个问题似乎是sed的行。下面是我现在研究的错误消息。

  sed的S%* / 12 * * * * CD /home/administrator/anm-1.5.0&放大器;&安培; ./anm.sh%*/10 * * * *光盘/home/administrator/anm-1.5.0&放大器;&放大器; ./anm.sh%:没有这样的文件或目录

它看起来像它试图取代线,但失败了。

*更新13年6月24日上午5:45 *

所以,上述错误信息是ofcourse我自己的愚蠢造成的。我的sed包括在引号内。因为我已经删除了引号的命令,但问题仍然存在。我曾尝试单引号,双引号,转义*和。没有运气。这个cron文件仍没有更新。这是当前code:

 功能科雷{
  如果[`的crontab -l | grep的$ SCRIPT`\\> ];然后
    CTMP =$(设置-f; crontab中-l | grep的$ SCRIPT)
    如果[$ CTMP=* / $ FREQ * * * * CD $ DIR功放&;&安培; ./$SCRIPT];然后
      回声$绿色无修改检测$ RESET
    其他
      crontab的-l | SEDS%$%CTMP * / $ FREQ * * * * CD $ DIR功放&;&安培; ./$SCRIPT%| crontab中 -
    科幻
  其他
    回声$ YELLOW CRON未检测到 - 安装的默认值$ RESET
    (crontab的-l;回声* / $ FREQ * * * * CD $ DIR功放&;&安培; ./$SCRIPT)| crontab中 -
  科幻
}

*更新13年6月24日6:05 *

也许问题我不会逃避一切。即SED前pression里面的变数,一旦扩大有需要转义字符?莫非是这个问题?如果是这样,林不完全知道如何解决这个问题。请帮助。

*解决*

我的确有过他们的sed之前转义变量为好。这里是code:

  CTMPESC = $(SED的/[\\*\\.&安培] / \\\\&安培/克'<<<$ CTMP)
DIRESC = $(SED的/[\\*\\.&安培] / \\\\&安培/克'<<<$ DIR)
SCRIPTESC = $(SED的/[\\*\\.&安培] / \\\\&安培/克'<<<$脚本)
crontab的-l | SEDS%$ CTMPESC%\\ * / $ FREQ \\ * \\ * \\ * \\ * CD $ DIRESC \\&放大器; \\&安培; \\ ./$ SCRIPTESC%| crontab中 -


解决方案

尝试逃离 * 中您的替代部分。

 的crontab -l | SED的%\\ * / 5 \\ * \\ * \\ * \\ * CD /home/administrator/anm-1\\.5\\.0&放大器;&安培; \\ ./ ANM \\ .SH%* / 10 * * * *光盘/home/administrator/anm-1.5.0&放大器;&放大器; ./anm.sh%| crontab中 - 

或者,你可以把在正则表达式符合您的工作,只是改变的时机

 的crontab -l | SED/anm\\.sh/s,^\\*/5,*/10,'| crontab中 - 

Can anyone see my syntax error here? Trying to edit/update a cron job, but the file is not being 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 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:

DIR=`pwd`
SCRIPT=`basename $0`
CRONTMP=`crontab -l | grep anm.sh`
crontab -l | sed 's%'$CTMP'%*/'$FREQ' * * * * cd '$DIR' && ./'$SCRIPT'%' | crontab -

Something along those lines. Obviously this is missing a few things, but that should give you the general idea.

Thanks for the help!

* UPDATE *

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.

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

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 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.

* UPDATE *

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:

CTMP=$(crontab -l | grep $SCRIPT)

when I echo out CTMP, I get a bunch of unexpected results. Apparently I am using grep incorrectly here.

Ok this issue was resolved. The variable was being stored correctly, I was just echoing it out incorrectly.

* UPDATE 06/24/13 5:08am *

The last issue seems to be the sed line. Here is the error message I am researching now.

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's trying to replace the line, but failing.

* UPDATE 06/24/13 5:45am *

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:

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:05am *

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.

* SOLVED *

I did indeed have to escape the variables as well before passing 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 -

解决方案

Try escaping the * and . in your substitution portion.

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 -

or, you can put in a regex that matches your job and just change the timing

crontab -l | sed '/anm\.sh/s,^\*/5,*/10,' | crontab -

这篇关于与bash脚本更新科雷的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆