0

0 6 * * 0 /root/SST/myscript.sh行全体を更新された文字列に置き換えるにはどうすればよいですか?

スクリプトは次を使用して実行され、update.sh 7それによって0 6 * * 0 /root/SST/myscript.sh置き換えられます0 7 * * 0 /root/SST/myscript.sh

hourcron エントリは動的になります (変更される可能性があります) 0 * * * 0 /root/SST/myscript.sh

[root@local ~]# crontab -l    
0 1 * * 0 /root/SST/test.sh
0 6 * * 0 /root/SST/myscript.sh
0 10 * * 0 /root/SST/test.sh

シェルスクリプトの内容update.sh:

#!/bin/bash

tmpfile=$(crontab -l)

if [[ "$tmpfile" == *myscript.sh* ]]
then
    #update myscript.sh within crontab contents

    echo "$updatedfileContents";
fi
4

2 に答える 2

0
crontab -l |
sed '/myscript.sh/ s/^\([^ ][^ ]*\) [^ ][^ ]* /\1 '"$1" '/'

これにより、更新されたコンテンツが表示されます。パターンは、行頭の空白以外のシーケンスに一致して記憶し、その後に空白、1 つ以上の空白以外のシーケンス、別の空白が続き、それを記憶されたパターン、スペース、値で置き換えます。と$1空白。を使用するupdate.sh 7,8,9,10,11と、0 7,8,9,10,11crontab に入ります。

そのコマンドの出力を変数にキャプチャし、それを (慎重に; 二重引用符を使用して) エコーしcrontab、実際のエントリを変更することができます。

あなたができることが考えられます:

crontab -l |
sed '/myscript.sh/ s/^\([^ ][^ ]*\) [^ ][^ ]* /\1 '"$1" '/' |
(sleep 1; crontab)

これにより、新しい値によって上書きされる前に現在の値を取得する機会がsleep与えられます — おそらく! crontab -lcrontab を失わないように、VCS (バージョン管理システム) の下に保管することを検討することはおそらく価値がありますsleep

于 2013-07-26T01:16:18.513 に答える
0

私は最終的にこの答えに行きました./update.sh 8

#!/bin/bash

updatedCrontab=""
tmpfile=$(crontab -l)

while read -r line; do
    if [[ "$line" == *myscript.sh* ]]
    then
            updatedCrontab+="0 $1 * * 0 myscript.sh\n"
    else
            updatedCrontab+="$line\n"
    fi
done <<< "$tmpfile"

echo -e "$updatedCrontab" | crontab

結果:

[root@local ~]# crontab -l
0 1 * * 0 /root/SST/test.sh
0 8 * * 0 /root/SST/myscript.sh
0 10 * * 0 /root/SST/test.sh
于 2013-07-29T15:54:55.457 に答える