ubuntu 中的任务计划程序

ubuntu 中的任务计划程序

我有一个 python 脚本,想每天凌晨 3:00 运行它,但我无法在 Linux 上像在 Windows 上一样安排任务。

我尝试在终端中执行以下命令

sudo apt-get install gnome-schedule

他返回给我错误

E: Unable to find gnome-schedule package

我不知道这个错误的原因,如果有任何 Linux 程序可以安排任务,有人可以推荐我等等...

我的 ubuntu 是 19.10

:)

答案1

标准的 Unix/Linux/Ubuntu 调度方式是使用cron。也可以使用systemctl,这种方式更加灵活,但也更加复杂。

cron服务是标准安装的一部分,因此您可能已经拥有它。如果没有,请按常规方式安装:

sudo apt install cron

要安排你的工作,该crontab命令将在你的默认编辑器中打开一个计划:

crontab -e

如果你还没有选择编辑器,系统会要求你选择一个。最简单的方法是nano。然后你将编辑如下文件:

# Edit this file to introduce tasks to be run by cron.
# 
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
# 
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').
# 
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
# 
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
# 
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
# 
# For more information see the manual pages of crontab(5) and cron(8)
# 
# m h  dom mon dow   command

请注意上述文件中的示例。要每天凌晨 3 点运行 Python 脚本,可以使用如下条目:

0 3 * * * python /path/to/my/script

退出编辑器时,cron 作业将设置为在下次符合条件的时间运行。我意识到这个解决方案非常面向命令行,但没关系,因为您不会经常这样做。您还可以使用 cron 做许多其他有趣的事情,例如全天以特定间隔运行作业。有关一些有用的示例,请查看:

man 5 crontab

相关内容