【问题标题】:Is there a way to run shell command only once a year?有没有办法每年只运行一次 shell 命令?
【发布时间】:2022-11-23 20:14:58
【问题描述】:

我在 Jenkins 作业中有一个 shell 脚本,我只需要脚本中的一个命令每年只运行一次。就像是:

if [today is 1st of Nov in any Year]; then
 do this command....
fi

可能吗?

【问题讨论】:

  • 我建议看一下 cron (man cron) 及其 crontab

标签: bash shell jenkins


【解决方案1】:

date 实用程序结合字符串测试应该可以解决问题:

case $(date +%m%d) in
  (1101) run this command
esac

有关 date(1) 接受的格式,请参阅 strftime(3) 手册页。

【讨论】:

    【解决方案2】:

    由于您使用的是 Jenkins 并且这是一项每年运行一次的作业,因此您可以将脚本移至 Jenkins 作业中并删除 if 条件。然后,在“构建触发器”部分下,您可以选择定期构建的选项。这里支持 Cron 表达式,或者您可以简单地使用 @yearly。这样做会显示如下内容:

    Would last have run at Sunday, September 11, 2022 10:21:52 PM CEST; would next run at Monday, September 11, 2023 10:21:52 PM CEST.
    

    这应该是你正在寻找的。

    【讨论】:

      猜你喜欢
      • 2021-07-02
      • 2012-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-03
      • 2020-05-04
      相关资源
      最近更新 更多