如何每小时运行一个 bash 脚本以及如何在云桌面上杀死它?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45453673/
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
How to run a bash script every hour and how to kill it on cloud desktop?
提问by leonardo rey
I want to run a bash script every hour in my cloud desktop which can be done by following commands:
我想在我的云桌面中每小时运行一个 bash 脚本,这可以通过以下命令来完成:
while true; do ./parseScript.sh; sleep 3600; done
while true; do ./parseScript.sh; sleep 3600; done
minute hour day month day-of-week command-line-to-execute
But the problem is my cloud window will expire so I won't be able to kill it in future? Can anyone guide me on this, How to do that?
但问题是我的云窗口会过期,所以我将来无法杀死它?任何人都可以指导我,如何做到这一点?
回答by Cédric Julien
On every modern linux distribution you have the cron
utility. This tool allow you to schedule tasks regularly (or not).
在每个现代 linux 发行版上,您都有该cron
实用程序。此工具允许您定期(或不定期)安排任务。
To schedule your task, you have to launch crontab
and then, inside the file presented (which is the list of your scheduled tasks), put something like this :
要安排您的任务,您必须启动crontab
,然后在显示的文件(这是您的计划任务列表)中输入如下内容:
0 * * * * /absolute/path/to/your/parseScript.sh
This will launch every hour at 0
minute (so, at 0:00, 1:00, 2:00....) your script (give the absolute path of your script)
这将每小时每0
分钟启动一次(因此,在 0:00、1:00、2:00....)您的脚本(给出脚本的绝对路径)
With very last versions of cron, you can even use something easier :
使用最新版本的 cron,您甚至可以使用更简单的东西:
@hourly /absolute/path/to/your/parseScript.sh
Because new shortcuts have been implemented (@hourly, @daily, @weekly, @monthly...)
因为已经实施了新的快捷方式(@hourly、@daily、@weekly、@monthly...)
回答by leonardo rey
this solution:
这个解决方案:
$watch --interval=3600 command
No sudoer user, no config, ideal for just use from time to time as a dirty solution.
没有 sudoer 用户,没有配置,非常适合偶尔用作肮脏的解决方案。
Also, you can cancel that execution whenever you want
此外,您可以随时取消该执行