在指定的星期几随时运行 cron 任务

在指定的星期几随时运行 cron 任务

我在使用 cron 时遇到了麻烦。我需要在桌面上运行任务,但我无法保证我的电脑会在指定的时间内开机。

例如我需要弱执行任务

如果我指定

* * * * 6 user my_very_useful_task

我非常有用的任务将在周日每分钟执行一次,但我只需要一次

如果我指定

0 9 * * 6 user my_very_useful_task

我非常有用的任务将在上午 9:00 执行,但我不能保证计算机会在此时打开

答案1

使用阿纳克隆。 来自男人阿纳克隆页:

  Anacron can be used to execute commands periodically, with a  frequency
   specified in days.  Unlike cron(8), it does not assume that the machine
   is running continuously.  Hence, it can be used on machines that aren't
   running 24 hours a day, to control daily, weekly, and monthly jobs that
   are usually controlled by cron.

你可以找到一个简明的介绍这里无论如何,请按如下方式修改 /etc/anacrontab 文件:创建一行,格式如下

 period   delay   job-identifier   command

Period 是您希望作业运行的频率,可接受的值为 1(每日)、7(每周)、N(= 每 N 天);delay 是启动后作业在执行前必须等待的时间(以分钟为单位);job-identifier 是目录 /var/spool/anacron 中的一个文件,其中将保留命令的最后一次执行记录。最后,command 是您想要执行的命令。

/etc/anacron 中的一行示例为

  1 10 test.daily /home/my_name/bin/my_command

这将每天执行命令 /home/my_name/bin/my_command(选项 1),并在 /var/spool/anacron/test.daily 中留下执行记录,并且,如果在执行时 PC 处于关闭状态,它将在下次启动后等待 10 分钟再执行该命令。

相关内容