如何在Linux上的特定时间运行脚本?

时间:2022-01-10 01:24:15

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 linux server , Debian wheezy. Thanks in advance

我有一个包含特定日期和时间的文本文件。我希望能够在该文件中指定的时间运行脚本。你会怎么做呢?创建另一个在后台运行的脚本(类似于deamon),如果当前时间与文件中的时间匹配,则每秒钟检查一次。有另一种方式吗?这台机器是一个linux服务器,Debian wheezy。谢谢提前

4 个解决方案

#1


112  

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.html http://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!

希望它可以帮助!

#2


10  

The at command exists specifically for this purpose (unlike cron which is intended for scheduling recurring tasks).

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

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

#3


6  

Cron is 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 date to compute the seconds-since-epoch of the target time as well as the present time, then use expr to find the difference, and sleep that many seconds.

但是对于一次性的解决方案,不需要root或任何东西,您可以使用date来计算目标时间的秒-since-epoch以及当前时间,然后使用expr来查找差异,然后休眠那么多秒。

#4


4  

Usually in Linux you use crontab for 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:

将于每周五下午一点半在这里执行脚本:

30 is minutes

30分钟

1 is hour

1小时

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

接下来的2 *是月日和月日(按这个顺序),5是工作日

#1


112  

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.html http://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!

希望它可以帮助!

#2


10  

The at command exists specifically for this purpose (unlike cron which is intended for scheduling recurring tasks).

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

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

#3


6  

Cron is 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 date to compute the seconds-since-epoch of the target time as well as the present time, then use expr to find the difference, and sleep that many seconds.

但是对于一次性的解决方案,不需要root或任何东西,您可以使用date来计算目标时间的秒-since-epoch以及当前时间,然后使用expr来查找差异,然后休眠那么多秒。

#4


4  

Usually in Linux you use crontab for 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:

将于每周五下午一点半在这里执行脚本:

30 is minutes

30分钟

1 is hour

1小时

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

接下来的2 *是月日和月日(按这个顺序),5是工作日