我如何设置一个任务,每隔多久运行一次? [英] How do I set a task to run every so often?

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

问题描述

如何让脚本每次运行,例如30分钟?我假设有不同的方式为不同的操作系统。我使用的是OS X。

解决方案

只需使用 launchd 。它是一个非常强大的启动系统,同时它是标准的启动系统的Mac OS X(当前OS X版本甚至不会启动它)。对于那些不熟悉 launchd (或者一般来说OS X)的人来说,它就像是 init cron ,SysVinit( init.d ) , inetd upstart systemd 。借用所有这些项目的概念,还提供你在其他地方找不到的东西。



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



系统任务转到

  / Library / LaunchDaemons / 

如果他们将运行,无论是否有任何用户登录到系统或不。他们将以root权限启动。



如果他们只有在任何用户登录时才会运行,他们会转到

  / Library / LaunchAgents / 



如果只有在登录后才能运行,他们会转到

 〜/ Library / LaunchAgents / 

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



请注意,还存在 / System / Library / LaunchDaemons / System / Library / LaunchAgents / System 是由OS X管理的。你不应该在那里放置任何文件,你不应该改变任何文件,除非你真的知道你在做什么。在系统文件夹中的混乱可能会使您的系统不可用(使其进入一个状态,甚至会拒绝再次启动)。这些是Apple放置在启动期间启动和运行系统的 launchd 任务的目录,根据需要自动启动服务,执行系统维护任务等。 p>

每个 launchd 任务都有一个plist格式的文件。它应该具有反向域名符号。例如。您可以为自己的任务命名

  com.example.my-fancy-task.plist 

这个plist可以有各种选项和设置。每次手写都不是最理想的,您可能需要使用免费工具 Lingon 来创建您的任务。这个工具以前是免费的,现在它在应用商店中花费5美元,非应用商店版本为10美元(非应用商店版本更强大,如果你已经计划付费,认真地,获得非应用商店版)。如果任何人知道一个类似的工具是免费软件或开源,给我一条线在评论,我宁愿推荐一个(不想在这里做广告商业软件)。



例如,它可能像这样

 <?xml version =1.0encoding = UTF-8?> 
<!DOCTYPE plist PUBLIC - // Apple Computer // DTD PLIST 1.0 // ENhttp://www.apple.com/DTDs/PropertyList-1.0.dtd\">
< plist version =1.0>
< dict>
< key>标签< / 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年运行shell脚本/usr/local/bin/my-script.sh秒(每30分钟)。你也可以在某些日期/时间运行任务(基本上launchd可以做一切cron可以做的),或者甚至禁用OnDemand,使launchd保持进程永久运行(如果它退出或崩溃,launchd会立即重新启动它) 。你甚至可以限制一个进程可以使用多少资源(如前所述,Lingon在一个漂亮的UI界面显示所有这些设置)。



更新: strong> 即使 OnDemand 仍然受支持,它已被弃用。新设置名为 KeepAlive ,这更有意义。它可以有一个布尔值,在这种情况下,它与 OnDemand (设置为 false 如果 OnDemand true ,反之亦然)。伟大的新功能是,它也可以有一个字典值,而不是一个布尔值。如果它有一个字典值,你有几个额外的选项,给你更精细的粒度控制在什么情况下任务将保持活着。例如。只有只有当磁盘上的某个文件/目录存在时,只有当另一个任务还活着时,或者只有当网络当前处于启动状态时,程序才会保持活动状态。 em>



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

  launchctl< command> < parameter> 

命令可以加载或卸载,以加载plist或重新卸载,文件的路径。或者命令可以是启动或停止,只是启动或停止这样的任务,在这种情况下参数是标签(com.example.my-fancy-task)。



请参阅Apple的文档 plist格式 launchctl 命令行工具(注意,您可以选择顶部的OS X版本,因为不同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.

解决方案

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?"

System tasks go to

/Library/LaunchDaemons/

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/

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.

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.

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

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>

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).

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 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.

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天全站免登陆