Linux 如何设置 cron 在特定时间只运行一次文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5473780/
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-04 00:35:47  来源:igfitidea点击:

How do I set up cron to run a file just once at a specific time?

linuxunixcron

提问by Gajus

How do I set up cronto run a file just once at a specific time? One of the alternatives is atbut it is not accessible to all users on standard hosting plans. Therefore I was wondering whether there is way to do it using cron?

如何设置cron在特定时间只运行一次文件?替代方案之一是,at但并非所有标准托管计划的用户都可以访问它。因此我想知道是否有办法使用cron?

采纳答案by gparis

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 yearcolumn 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 的标准/默认实现不支持年份列。请参阅下面的 TomOnTime 答案,了解在未来的特定时间在 cron 的标准实现中运行脚本的正确方法。

回答by Brian Showalter

You could put a crontab file in /etc/cron.dwhich 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.

您可以放置​​一个 crontab 文件,/etc/cron.d该文件将运行一个脚本来运行您的命令,然后删除 .crontab 文件中的 crontab 文件/etc/cron.d。当然,这意味着您的脚本需要以 root 身份运行。

回答by dj_segfault

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");

回答by TomOnTime

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 作业包含确保它只运行一次的代码。

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!' [email protected]

回答by TomOnTime

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 -rremoves 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”行的那个!

回答by alexandre1985

atis the correct way.

at是正确的方法。

If you don't have the atcommand in the machine and you also don't have install privilegies on it, you can put something like this on cron(maybe with the crontabcommand):

如果您at在机器中没有该命令并且您也没有安装权限,您可以将这样的内容放在上面cron(也许使用crontab命令):

* * * 5 * /path/to/comand_to_execute; /usr/bin/crontab -l | /usr/bin/grep -iv command_to_execute | /usr/bin/crontab - 

it will execute your command one time and remove it from cronafter that.

它将执行一次您的命令,然后将其删除cron