Updating CRON with bash script

。_饼干妹妹 提交于 2019-12-04 06:03:13

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 -

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

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. Or use anm.sh in place of cd as an address to be more specific in case there is more than one cd line:

crontab -l | sed '/anm\.sh/s#\/5#\/10#' | crontab -
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!