Timed task tool in Mac: launchctl

launchctl is a unified service management framework that can start, stop and manage daemons, applications, processes and scripts, etc.
launchctl specifies execution cycles and tasks through configuration files.

Of course, the mac can also use the crontab command to add scheduled tasks like the linux system .

The following will teach you step by step how to create scheduled tasks on mac. (Task goal: regularly execute the python program of /Users/demo/helloworld.py at ten o'clock every night)

1. Create the run.sh script

Enter the directory where the helloworld.pyprogram is located to
cd /User/demo
create a run.sh script and
vi run.sh
add helloworld.pythe command to execute

#!/bin/sh

# 记录一下开始时间
echo `date` >> /Users/demo/log &&
# 进入helloworld.py程序所在目录
cd /Users/demo &&
# 执行python脚本(注意前面要指定python运行环境/usr/bin/python,根据自己的情况改变)
/usr/bin/python helloworld.py
# 运行完成
echo 'finish' >> /Users/demo/log

:wqsave and exit

Note that the script needs to be changed to executable permissions
chmod 777 run.sh

2. Write plist file

launchctl will launch tasks based on the information in the plist file.
plist scripts are generally stored in the following directories:

  • /Library/LaunchDaemons-->As long as the system is started, it will be executed even if the user does not log in to the system

  • /Library/LaunchAgents--> will be executed when the user logs in to the system

More plist storage directories:

~/Library/LaunchAgents User-defined task items
/Library/LaunchAgents Administrator-defined task items for users
/Library/LaunchDaemons Administrator-defined daemon task items
/System/Library/LaunchAgents Mac OS X user-defined Task Items Defined
/System/Library/LaunchDaemons Daemon Task Items defined by Mac OS X

Enter ~/Library/LaunchAgents, create a plist filecom.demo.plist

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
  <!-- Label唯一的标识 -->
  <key>Label</key>
  <string>com.demo.plist</string>
  <!-- 指定要运行的脚本 -->
  <key>ProgramArguments</key>
  <array>
    <string>/Users/demo/run.sh</string>
  </array>
  <!-- 指定要运行的时间 -->
  <key>StartCalendarInterval</key>
  <dict>
        <key>Minute</key>
        <integer>00</integer>
        <key>Hour</key>
        <integer>22</integer>
  </dict>
<!-- 标准输出文件 -->
<key>StandardOutPath</key>
<string>/Users/demo/run.log</string>
<!-- 标准错误输出文件,错误日志 -->
<key>StandardErrorPath</key>
<string>/Users/demo/run.err</string>
</dict>
</plist>

3. Load command

launchctl load -w com.demo.plist
The task is loaded successfully.

More commands:

# 加载任务, -w选项会将plist文件中无效的key覆盖掉,建议加上
$ launchctl load -w com.demo.plist

# 删除任务
$ launchctl unload -w com.demo.plist

# 查看任务列表, 使用 grep '任务部分名字' 过滤
$ launchctl list | grep 'com.demo'

# 开始任务
$ launchctl start  com.demo.plist

# 结束任务
$ launchctl stop   com.demo.plist

If the task is modified, it must be unloaded first, and then reload
start to test the task. This is executed immediately. Regardless of whether the time is up
, the task must be loaded before start and unload are executed. Otherwise, the task
can be stopped by reporting an error stop .

Extras

plist supports two ways to configure execution time:
  • StartInterval: Specify how long the script should be executed every interval (unit: seconds);
  • StartCalendarInterval: You can specify how many minutes, hours, days, days of the week, and months to execute the script, similar to the settings in crontab, including the following keys:
Minute <integer>
The minute on which this job will be run.
Hour <integer>
The hour on which this job will be run.
Day <integer>
The day on which this job will be run.
Weekday <integer>
The weekday on which this job will be run (0 and 7 are Sunday).
Month <integer>
The month on which this job will be run.
Description of the parameters in the plist part:
  1. Label: The corresponding need to ensure global uniqueness;
  2. Program: the program to run;
  3. ProgramArguments: command statement
  4. StartCalendarInterval: running time, use dict for a single time point, use array <dict> for multiple time points
  5. StartInterval: Time interval, one of which is used with StartCalendarInterval, in seconds
  6. StandardInPath, StandardOutPath, StandardErrorPath: Standard input and output error files. It is recommended not to use .log as the suffix, and the information inside will not be opened.
  7. When starting a task regularly, if the network is involved, but the computer is in a sleep state, it cannot be executed. At this time, you can start the screen regularly.

For more parameters, see: mac official documentation

refer to:

Launchctl for Mac to perform timed tasks



Author: Fanzhu
Link : https://www.jianshu.com/p/4addd9b455f2
Source: Jianshu The
copyright belongs to the author. For commercial reprints, please contact the author for authorization, and for non-commercial reprints, please indicate the source.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325649854&siteId=291194637