【问题标题】:how to prevent duplicate entry for creating cron jobs?如何防止重复输入以创建 cron 作业?
【发布时间】:2015-04-19 14:05:09
【问题描述】:

这里是成功创建 cron 作业的以下代码,但我需要防止重复输入,所以下次我尝试为同一文件创建 cron 作业。我需要覆盖现有的 cron 作业

 #!/bin/bash
    crontab -l > cd3.new
    file1="/home/admin/Desktop/n_com.sh -EI"
    file2="/home/admin/Desktop/com.sh -NI"
    file3="/home/admin/Desktop/fcom.sh -EF"
    file4="/home/admin/Desktop/fcm.sh -NF"
    echo "$1 $2 $3 $4 $5 $file1" >> cd3.new
    echo "$6 $7 $8 $9 ${10} $file2" >> cd3.new
    echo "${11} ${12} ${13} ${14} ${15} $file3" >> cd3.new
    echo "${16} ${17} ${18} ${19} ${20} $file4" >> cd3.new
    cat cd3.new
    crontab cd3.new

【问题讨论】:

  • 然后你可以将当前和旧的存储在不同的文件中,并与diff一起玩。
  • @fedorqui 我需要覆盖

标签: linux shell unix cron cron-task


【解决方案1】:

我喜欢为 cron 的每个“周期”设置目录并使用 run-parts 执行它们的想法。更新只需要 rsync-ing 新的 cron 目录,而且不会弄乱 crontab。

然后我这样设置目录和 crontab:

# Run hourly, daily, weekly, monthly and yearly tasks for axon
00  0 * * * run-parts /etc/cron/midnight >/dev/null 2>&1
01  * * * * run-parts /etc/cron/hourly   >/dev/null 2>&1
02  1 * * * run-parts /etc/cron/daily    >/dev/null 2>&1
55 23 * * * run-parts /etc/cron/dailyend >/dev/null 2>&1
45  2 * * 6 run-parts /etc/cron/weekly   >/dev/null 2>&1
02  0 1 * * run-parts /etc/cron/monthly  >/dev/null 2>&1
03  0 1 1 * run-parts /etc/cron/yearly   >/dev/null 2>&1

【讨论】:

    猜你喜欢
    • 2014-04-05
    • 2012-10-19
    • 2017-05-12
    • 1970-01-01
    • 2019-03-16
    • 2020-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多