如何将任务设置为每隔一段时间运行一次? [英] How do I set a task to run every so often?

查看:20
本文介绍了如何将任务设置为每隔一段时间运行一次?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何让脚本每隔 30 分钟运行一次?我假设不同的操作系统有不同的方法.我使用的是 OS X.

How do I have a script run every, say 30 minutes? I assume there are different ways for different OSs. I'm using OS X.

推荐答案

只需使用 launchd.它是一个非常强大的启动器系统,同时它也是 Mac OS X 的标准启动器系统(没有它,当前的 OS X 版本甚至无法启动).对于那些不熟悉 launchd(或一般的 OS X)的人来说,它就像是 initcronat、SysVinit (init.d)、inetdupstartsystemd.借鉴所有这些项目的概念,同时提供您在别处可能找不到的东西.

Just use launchd. It is a very powerful launcher system and meanwhile it is the standard launcher system for Mac OS X (current OS X version wouldn't even boot without it). For those who are not familiar with launchd (or with OS X in general), it is like a crossbreed between init, cron, at, SysVinit (init.d), inetd, upstart and systemd. Borrowing concepts of all these projects, yet also offering things you may not find elsewhere.

每个服务/任务都是一个文件.文件的位置取决于以下问题:该服务应该何时运行?"和服务需要哪些权限?"

Every service/task is a file. The location of the file depends on the questions: "When is this service supposed to run?" and "Which privileges will the service need?"

系统任务转到

/Library/LaunchDaemons/

如果它们应该运行,无论是否有用户登录到系统.它们将以root"权限启动.

if they shall run, no matter if any user is logged in to the system or not. They will be started with "root" privileges.

如果他们只在任何用户登录时才运行,他们去

If they shall only run if any user is logged in, they go to

/Library/LaunchAgents/

并且将以刚刚登录的用户的权限执行.

and will be executed with the privileges of the user that just logged in.

如果它们仅在登录后才运行,它们将转到

If they shall run only if you are logged in, they go to

~/Library/LaunchAgents/

其中 ~ 是您的 HOME 目录.这些任务将以您的权限运行,就像您自己通过命令行或双击 Finder 中的文件启动它们一样.

where ~ is your HOME directory. These task will run with your privileges, just as if you had started them yourself by command line or by double clicking a file in Finder.

请注意,还有/System/Library/LaunchDaemons/System/Library/LaunchAgents,但和往常一样,/System 由 OS X 管理.您不得在那里放置任何文件,也不得在那里更改任何文件,除非您真的知道自己在做什么.在 Systems 文件夹中乱搞可能会使您的系统无法使用(使其进入甚至拒绝再次启动的状态).这些是 Apple 放置 launchd 任务的目录,这些任务用于在引导期间启动和运行系统、根据需要自动启动服务、执行系统维护任务等.

Note that there also exists /System/Library/LaunchDaemons and /System/Library/LaunchAgents, but as usual, everything under /System is managed by OS X. You shall not place any files there, you shall not change any files there, unless you really know what you are doing. Messing around in the Systems folder can make your system unusable (get it into a state where it will even refuse to boot up again). These are the directories where Apple places the launchd tasks that get your system up and running during boot, automatically start services as required, perform system maintenance tasks, and so on.

每个 launchd 任务都有一个 plist 格式的文件.它应该有反向域名符号.例如.你可以给你的任务命名

Every launchd task there is a file in plist format. It should have reverse domain name notation. E.g. you can name your task

com.example.my-fancy-task.plist

这个 plist 可以有各种选项和设置.一只手写一个是次优的,您可能想要获得免费工具 Lingon 来创建您的任务.这个工具曾经是免费的,现在在应用商店需要 5 美元,非应用商店版本需要 10 美元(非应用商店版本功能更强大,如果您已经计划付费,请认真地获取非应用商店版本)版本).如果有人知道免费软件或开源软件的类似工具,请在评论中给我留言,我宁愿推荐那个(不想在这里为商业软件做广告).

This plist can have various options and settings. Writing one per hand is suboptimal, you may want to get the free tool Lingon to create your tasks. This tool used to be free, now it costs $5 in the app store and $10 as the non app store version (the non app store version is much more powerful and if you already plan on paying for it, seriously, get the non app store version). If anyone knows a comparable tool that is freeware or open source, drop me a line in the comments and I will rather recommend that one (don't want to advertise here for commercial software).

举个例子,它看起来像这样

Just as an example, it could look like this

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>com.example.my-fancy-task</string>
    <key>OnDemand</key>
    <true/>
    <key>ProgramArguments</key>
    <array>
        <string>/bin/sh</string>
        <string>/usr/local/bin/my-script.sh</string>
    </array>
    <key>StartInterval</key>
    <integer>1800</integer>
</dict>
</plist>

此代理将每 1800 秒(每 30 分钟)运行一次 shell 脚本/usr/local/bin/my-script.sh.您还可以在特定日期/时间运行任务(基本上 launchd 可以执行 cron 可以执行的所有操作),或者您甚至可以禁用OnDemand",从而使 launchd 保持进程永久运行(如果它退出或崩溃,launchd 将立即重新启动它).您甚至可以限制进程可以使用的资源量(如前所述,Lingon 在漂亮的 UI 界面中显示了所有这些设置).

This agent will run the shell script /usr/local/bin/my-script.sh every 1800 seconds (every 30 minutes). You can also have task run on certain dates/times (basically launchd can do everything cron can do) or you can even disable "OnDemand" causing launchd to keep the process permanently running (if it quits or crashes, launchd will immediately restart it). You can even limit how much resources a process may use (as said before, Lingon shows all these settings in a nice UI interface).

更新: 尽管 OnDemand 仍受支持,但已弃用.新设置名为 KeepAlive,这更有意义.它可以有一个布尔值,在这种情况下它与 OnDemand 完全相反(将其设置为 false 就好像 OnDemand>true 反之亦然).一个很棒的新功能是,它也可以有一个字典值而不是一个布尔值.如果它有一个字典值,你有几个额外的选项可以让你更精细地控制任务在哪种情况下应该保持活动状态.例如.只要程序以零退出代码终止,只要磁盘上存在某个文件/目录,仅当另一个任务也处于活动状态时,或者仅当网络当前处于启动状态时,它就会保持活动状态.

Update: Even though OnDemand is still supported, it is deprecated. The new setting is named KeepAlive, which makes much more sense. It can have a boolean value, in which case it is the exact opposite of OnDemand (setting it to false behaves as if OnDemand is true and the other way round). The great new feature is, that it can also have a dictionary value instead of a boolean one. If it has a dictionary value, you have a couple of extra options that give you more fine grain control under which circumstances the task shall be kept alive. E.g. it is only kept alive as long as the program terminated with an exit code of zero, only as long as a certain file/directory on disk exists, only if another task is also alive, or only if the network is currently up.

您也可以通过命令行手动启用/禁用任务:

Also you can manually enable/disable tasks via command line:

launchctl <command> <parameter>

command 可以是 load 或 unload,用于加载 plist 或再次卸载它,在这种情况下,参数是文件的路径.或者命令可以是开始或停止,只是开始或停止这样的任务,在这种情况下,参数是标签(com.example.my-fancy-task).还存在其他命令和选项.

command can be load or unload, to load a plist or unload it again, in which case parameter is the path to the file. Or command can be start or stop, to just start or stop such a task, in which case parameter is the label (com.example.my-fancy-task). Other commands and options exist as well.

请参阅 Apple 的文档 plist 格式launchctl 命令行工具(请注意,您可以在顶部选择 OS X 版本,因为格式/选项确实有所不同在不同的 OS X 版本之间)

See Apple's documentation of the plist format and of the launchctl command line tool (note that you can select the OS X version on top, since the format/options do vary between different OS X releases)

这篇关于如何将任务设置为每隔一段时间运行一次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆