简体   繁体   English

使用bash脚本更新CRON

[英]Updating CRON with bash script

Can anyone see my syntax error here? 谁能在这里看到我的语法错误? Trying to edit/update a cron job, but the file is not being updated. 尝试编辑/更新cron作业,但文件未更新。

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. 最终我试图从配置文件$ FREQ(分钟)中提取一个值来运行该作业。 The script will first check to see if the value in the config is different than the value currently in the crontab. 该脚本将首先检查配置中的值是否与crontab中当前的值不同。 If the value is different, it will update the crontab with the new value. 如果值不同,它将使用新值更新crontab。 The crontab (both initial install and updates) also pulls the running directory and script name from variables. crontab(初始安装和更新)还从变量中提取运行目录和脚本名称。 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. 请记住:$ SCRIPT和$ DIR是在函数外定义的,只等于脚本名称(例如anm.sh)和当前工作目录。 And I took your suggestion and updated all my code. 我接受了你的建议并更新了我的所有代码。 I now use SCRIPT=$(basename $0). 我现在使用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). 本质上,当函数运行时,它首先检查是否安装了cron作业(这可能是脚本第一次运行)。 If it doesn't detect anything, it appends the cron job to the crontab file. 如果它没有检测到任何内容,它会将cron作业附加到crontab文件。 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. 接下来,如果函数确实检测到已安装了cron作业,则会将其与配置文件中设置的频率(以分钟为单位)进行比较。 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. 最后,如果值确实不同,则它会尝试更新crontab文件中的相应行以反映在配置文件中所做的更改。 This last part fails. 最后一部分失败了。 Currently its just overwriting the crontab file completely to blank. 目前它只是将crontab文件完全覆盖为空白。

* 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: 这不能正确地从crontab中拉出所需的行并将其存储到变量CTMP中:

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

when I echo out CTMP, I get a bunch of unexpected results. 当我回应CTMP时,我得到了一堆意想不到的结果。 Apparently I am using grep incorrectly here. 显然我在这里使用grep不正确。

Ok this issue was resolved. 好的,这个问题已经解决。 The variable was being stored correctly, I was just echoing it out incorrectly. 变量存储正确,我只是错误地回应它。

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

The last issue seems to be the sed line. 最后一个问题似乎是sed线。 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 * *更新06/24/13 5:45 am *

So the error message above was ofcourse caused by my own stupidity. 所以上面的错误信息是由我自己的愚蠢造成的。 I was including sed inside the quotes. 我把sed包括在引号内。 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. cron文件仍未更新。 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 * *更新06/24/13 6:05 am *

Perhaps the issue is I'm not escaping everything. 也许问题是我没有逃避一切。 ie, the variables inside the sed expression once expanded have characters that need escaping? 也就是说,sed表达式中的变量一旦展开,就会有需要转义的字符? 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. 在将它们传递给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 或者,您可以输入与您的工作相匹配的regex ,只需更改时间即可

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

* SOLVED * * 解决了 *

I did indeed have to escape the variables as well before passing them to sed. 在将它们传递给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 -

I would offer this shorter version: 我会提供这个较短的版本:

crontab -l | sed '/cd /s#\/5#\/10#' | crontab -

which should change 5 to 10 where the line has cd . 该行应该改变5到10,其中行有cd Or use anm.sh in place of cd as an address to be more specific in case there is more than one cd line: 或者使用anm.sh代替cd作为地址,以便在有多个cd行时更具体:

crontab -l | sed '/anm\.sh/s#\/5#\/10#' | crontab -

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM