如何安排取决于网络可用性的作业?

bal*_*lki 6 networking cron

我已经克隆了某个项目的 Git 存储库。我想git pull每周自动化和编译一次项目。我使用的笔记本电脑不会 24x7。现在,我不能使用,cron因为我应该在那个确切的时刻保持系统运行。我也不能使用anacron,因为它可能会在我连接到网络之前启动。anacron只有当我连接到互联网时,是否有一些选项才能运行这个特定的工作?或者我应该为此使用其他一些不同的工具吗?

Gil*_*il' 2

连接到网络时运行作业。大多数发行版都有一个可以插入的脚本基础架构,但您需要 root 权限。如果您使用 NetworkManager 或 Wicd 连接,它们也有自己的钩子基础设施。添加一个仅在网络可用时运行的 cron 作业(并且可选地,仅在长时间未运行该作业时运行),以防网络长时间保持连接。

您没有指定您的发行版,因此我将给出一个基于 Debian 的发行版的示例。中的脚本在/etc/network/if-up.d界面调出后执行;请参阅interfaces(5)手册页以获取更多信息。/etc/network/if-up.d/zzzz_balki_git_pull编写如下脚本:

#!/bin/sh
su balki -c /home/balki/bin/pull_repository_if_old
Run Code Online (Sandbox Code Playgroud)

执行pull_repository_if_old类似以下操作(git pull如果网络可用,则运行,除非时间戳小于 7 天):

#!/bin/sh
set -e
if [ -z "$(find /path/to/.repository.pull.timestamp -mtime -7)" ] &&
   ping -c 1 -w 1 repository.example.com >/dev/null 2>/dev/null
then
  cd /path/to/repository
  git pull
  touch /path/to/.repository.pull.timestamp
fi
EOF
Run Code Online (Sandbox Code Playgroud)

您帐户上的 crontab 条目:

28 4 * * * /home/balki/bin/pull_repository_if_old
Run Code Online (Sandbox Code Playgroud)