我有一个 python 脚本,想每天凌晨 3:00 运行它,但我无法像在 Windows 上一样在 Linux 上安排任务。
我尝试在终端中执行以下命令
sudo apt-get install gnome-schedule
Run Code Online (Sandbox Code Playgroud)
他给我返回错误
E: Unable to find gnome-schedule package
Run Code Online (Sandbox Code Playgroud)
我不知道这个错误的原因是否有任何Linux程序来安排任务,有人可以推荐我等等...
我的ubuntu是19.10
:)
标准的 Unix/Linux/Ubuntu 调度方式是使用cron. 人们也可以使用systemctl,这更灵活,但更复杂。
该cron服务是标准安装的一部分,因此您可能已经拥有它。如果不这样做,请按照通常的方式安装:
sudo apt install cron
Run Code Online (Sandbox Code Playgroud)
要安排您的作业,该crontab命令将在默认编辑器中打开一个时间表:
crontab -e
Run Code Online (Sandbox Code Playgroud)
如果您尚未选择编辑器,系统会要求您选择一个。这项任务最简单的方法是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
Run Code Online (Sandbox Code Playgroud)
请注意上面文件中的示例。要每天凌晨 3 点运行 Python 脚本,您可以使用如下条目:
0 3 * * * python /path/to/my/script
Run Code Online (Sandbox Code Playgroud)
当您退出编辑器时,cron 作业将设置为在下一次符合条件的时间运行。我意识到这个解决方案非常面向命令行,但是没关系,因为您不会经常这样做。您还可以使用 cron 做许多其他奇特的事情,例如全天以特定的时间间隔运行作业。有关一些有用的示例,请查看:
man 5 crontab
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
14848 次 |
| 最近记录: |