如何设置cron在未来的特定时间只运行一个文件一次?

时间:2022-01-07 02:33:56

How to setup cron to run a file just once at a specific time in future? One of the alternatives is http://en.wikipedia.org/wiki/At_%28Unix%29 but it is by far not accessible to all users on standard hosting plans. Therefore I was wondering whether there is way to do it using cron?

如何设置cron在将来某个特定的时间运行一个文件?另一种选择是http://en.wikipedia.org/wiki/At_%28Unix%29,但是在标准的托管计划中,并不是所有用户都可以访问它。所以我在想是否有办法用cron来做这件事?

5 个解决方案

#1


30  

Try this out to execute a command on 30th March 2011 at midnight:

请尝试在2011年3月30日午夜执行命令:

0 0 30 3 ? 2011  /command

WARNING: As noted in comments, the year column is not supported in standard/default implementations of cron. Please refer to TomOnTime answer below, for a proper way to run a script at a specific time in the future in standard implementations of cron.

警告:如注释所述,cron的标准/默认实现不支持year列。在cron的标准实现中,请参考下面的TomOnTime答案,以获得在未来特定时间运行脚本的正确方法。

#2


34  

You really want to use at. It is exactly made for this purpose.

你真的想用at。它就是为这个目的而做的。

echo /usr/bin/the_command options | at now + 1 day

However if you don't have at, or your hosting company doesn't provide access to it, you can have a cron job include code that makes sure it only runs once.

但是,如果您没有at,或者您的托管公司没有提供对它的访问权限,您可以使用cron job include代码,确保它只运行一次。

Set up a cron entry with a very specific time:

设置一个非常特定的时间的cron入口:

0 0 2 12 * /home/adm/bin/the_command options

Next /home/adm/bin/the_command needs to either make sure it only runs once.

接下来/home/adm/bin/the_command需要确保只运行一次。

#! /bin/bash

COMMAND=/home/adm/bin/the_command
DONEYET="${COMMAND}.alreadyrun"

export PATH=/usr/bin:$PATH

if [[ -f $DONEYET ]]; then
  exit 1
fi
touch "$DONEYET"

# Put the command you want to run exactly once here:
echo 'You will only get this once!' | mail -s 'Greetings!' me@example.com

#3


11  

You really want to use at. It is exactly made for this purpose.

你真的想用at。它就是为这个目的而做的。

echo /usr/bin/the_command options | at now + 1 day

However if you don't have at, or your hosting company doesn't provide access to it, you could make a self-deleting cron entry.

但是,如果您没有at,或者您的托管公司没有提供对它的访问权限,您可以创建一个自删除cron条目。

Sadly, this will remove all your cron entries. However, if you only have one, this is fine.

遗憾的是,这将删除所有cron条目。然而,如果你只有一个,这是可以的。

0 0 2 12 * crontab -r ; /home/adm/bin/the_command options

The command crontab -r removes your crontab entry. Luckily the rest of the command line will still execute.

命令crontab -r删除crontab条目。幸运的是,命令行其余部分仍将执行。

WARNING: This is dangerous! It removes ALL cron entries. If you have many, this will remove them all, not just the one that has the "crontab -r" line!

警告:这是危险的!它删除所有cron条目。如果您有许多,这将删除所有,而不仅仅是有“crontab -r”行!

#4


2  

You could put a crontab file in /etc/cron.d which would run a script that would run your command and then delete the crontab file in /etc/cron.d. Of course, that means your script would need to run as root.

您可以在/etc/ crontab文件中放置crontab文件。它会运行一个脚本,运行您的命令,然后删除/etc/cron.d中的crontab文件当然,这意味着您的脚本需要作为root用户运行。

#5


0  

Your comment suggests you're trying to call this from a programming language. If that's the case, can your program fork a child process that calls sleep then does the work?

你的评论表明你试图从一种编程语言中调用它。如果是这样的话,你的程序可以为调用sleep的子进程提供分支吗?

What about having your program calculate the number of seconds until the desired runtime, and have it call shell_exec("sleep ${secondsToWait) ; myCommandToRun");

让您的程序计算到所需运行时的秒数,并让它调用shell_exec(“sleep ${secondsToWait);myCommandToRun”);

#1


30  

Try this out to execute a command on 30th March 2011 at midnight:

请尝试在2011年3月30日午夜执行命令:

0 0 30 3 ? 2011  /command

WARNING: As noted in comments, the year column is not supported in standard/default implementations of cron. Please refer to TomOnTime answer below, for a proper way to run a script at a specific time in the future in standard implementations of cron.

警告:如注释所述,cron的标准/默认实现不支持year列。在cron的标准实现中,请参考下面的TomOnTime答案,以获得在未来特定时间运行脚本的正确方法。

#2


34  

You really want to use at. It is exactly made for this purpose.

你真的想用at。它就是为这个目的而做的。

echo /usr/bin/the_command options | at now + 1 day

However if you don't have at, or your hosting company doesn't provide access to it, you can have a cron job include code that makes sure it only runs once.

但是,如果您没有at,或者您的托管公司没有提供对它的访问权限,您可以使用cron job include代码,确保它只运行一次。

Set up a cron entry with a very specific time:

设置一个非常特定的时间的cron入口:

0 0 2 12 * /home/adm/bin/the_command options

Next /home/adm/bin/the_command needs to either make sure it only runs once.

接下来/home/adm/bin/the_command需要确保只运行一次。

#! /bin/bash

COMMAND=/home/adm/bin/the_command
DONEYET="${COMMAND}.alreadyrun"

export PATH=/usr/bin:$PATH

if [[ -f $DONEYET ]]; then
  exit 1
fi
touch "$DONEYET"

# Put the command you want to run exactly once here:
echo 'You will only get this once!' | mail -s 'Greetings!' me@example.com

#3


11  

You really want to use at. It is exactly made for this purpose.

你真的想用at。它就是为这个目的而做的。

echo /usr/bin/the_command options | at now + 1 day

However if you don't have at, or your hosting company doesn't provide access to it, you could make a self-deleting cron entry.

但是,如果您没有at,或者您的托管公司没有提供对它的访问权限,您可以创建一个自删除cron条目。

Sadly, this will remove all your cron entries. However, if you only have one, this is fine.

遗憾的是,这将删除所有cron条目。然而,如果你只有一个,这是可以的。

0 0 2 12 * crontab -r ; /home/adm/bin/the_command options

The command crontab -r removes your crontab entry. Luckily the rest of the command line will still execute.

命令crontab -r删除crontab条目。幸运的是,命令行其余部分仍将执行。

WARNING: This is dangerous! It removes ALL cron entries. If you have many, this will remove them all, not just the one that has the "crontab -r" line!

警告:这是危险的!它删除所有cron条目。如果您有许多,这将删除所有,而不仅仅是有“crontab -r”行!

#4


2  

You could put a crontab file in /etc/cron.d which would run a script that would run your command and then delete the crontab file in /etc/cron.d. Of course, that means your script would need to run as root.

您可以在/etc/ crontab文件中放置crontab文件。它会运行一个脚本,运行您的命令,然后删除/etc/cron.d中的crontab文件当然,这意味着您的脚本需要作为root用户运行。

#5


0  

Your comment suggests you're trying to call this from a programming language. If that's the case, can your program fork a child process that calls sleep then does the work?

你的评论表明你试图从一种编程语言中调用它。如果是这样的话,你的程序可以为调用sleep的子进程提供分支吗?

What about having your program calculate the number of seconds until the desired runtime, and have it call shell_exec("sleep ${secondsToWait) ; myCommandToRun");

让您的程序计算到所需运行时的秒数,并让它调用shell_exec(“sleep ${secondsToWait);myCommandToRun”);