如何在 Linux 上的某个时间运行脚本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18945669/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-07 00:54:46  来源:igfitidea点击:

How to run a script at a certain time on Linux?

linuxbashscriptingdebian

提问by Aaron Ullal

I have a text file containing a specific date and time.I want to be able to run a script at the time specified in that file.How would you achieve that? Create another script that runs in background (sort of a deamon) and checks every second if the current time is matching the time in the file? Is there another way? The machine is a linuxserver , Debian wheezy. Thanks in advance

我有一个包含特定日期和时间文本文件。我希望能够在该文件中指定的时间运行脚本。你会如何做到这一点?创建另一个在后台运行的脚本(类似于守护进程)并每秒检查当前时间是否与文件中的时间匹配?还有其他方法吗?机器是linux服务器,Debian wheezy。提前致谢

采纳答案by Antoni

Look at the following:

请看以下内容:

echo "ls -l" | at 07:00

This code line executes "ls -l" at a specific time. This is an example of executing something (a command in my example) at a specific time. "at" is the command you were really looking for. You can read the specifications here:

此代码行在特定时间执行“ls -l”。这是在特定时间执行某事(在我的示例中为命令)的示例。“at”是您真正要查找的命令。您可以在此处阅读规格:

http://manpages.ubuntu.com/manpages/precise/en/man1/at.1posix.htmlhttp://manpages.ubuntu.com/manpages/xenial/man1/at.1posix.html

http://manpages.ubuntu.com/manpages/precise/en/man1/at.1posix.html http://manpages.ubuntu.com/manpages/xenial/man1/at.1posix.html

Hope it helps!

希望能帮助到你!

回答by julumme

Usually in Linux you use crontabfor this kind of scduled tasks. But you have to specify the time when you "setup the timer" - so if you want it to be configurable in the file itself, you will have to create some mechanism to do that.

通常在 Linux 中,您crontab用于此类有计划的任务。但是您必须指定“设置计时器”的时间 - 因此,如果您希望它在文件本身中可配置,则必须创建一些机制来做到这一点。

But in general, you would use for example:

但一般来说,你会使用例如:

30 1 * * 5 /path/to/script/script.sh

Would execute the script every Friday at 1:30 (AM) Here:

将在每个星期五的 1:30 (AM) 执行脚本在这里:

30is minutes

30是分钟

1is hour

1是小时

next 2 *'s are day of monthand month(in that order) and 5is weekday

接下来的 2 个 * 是day of monthmonth5按此顺序)并且是工作日

回答by tripleee

The atcommand exists specifically for this purpose (unlike cronwhich is intended for scheduling recurring tasks).

at命令专门为此目的而存在(与cron用于调度重复性任务的命令不同)。

at $(cat file) </path/to/script

回答by Anony

Cronis good for something that will run periodically, like every Saturday at 4am. There's also anacron, which works around power shutdowns, sleeps, and whatnot. As well as at.

Cron适用于定期运行的东西,比如每周六凌晨 4 点。还有anacron,它可以解决电源关闭、睡眠等问题。以及

But for a one-off solution, that doesn't require root or anything, you can just use dateto compute the seconds-since-epoch of the target time as well as the present time, then use exprto find the difference, and sleepthat many seconds.

但是对于一次性解决方案,不需要 root 或任何东西,您可以只使用date来计算目标时间和当前时间的秒数,然后使用expr找到差异,并且那么多秒。