在 Mac 上使用 crontab 服务

2020-03-21  本文已影响0人  大成小栈

1. crontab介绍

crontab 是 linux 下的定时任务工具,类似于Windows系统中的任务计划程序。Mac下也可以使用crontab(mac上目前更推荐使用launchctl),通过crontab 命令可以在固定的间隔时间执行指定的系统命令或 shell 脚本,时间间隔的单位可以是分钟、小时、日、月、周及以上的任意组合,组合方式需要符合相应语法。该工具非常适合周期性的日志分析或数据备份等工作。

我们可以在一个crontab文件 下面是一条crontab的文件内容,该条定时任务的功能是每分钟都输出时间到指定文件:

# 分 时 日 月 星期 要执行的命令
* * * * * /bin/date >> /Users/dcw/Desktop/crontab

第1列:分钟1~59
第2列:小时1~23(0表示子夜)
第3列:日1~31
第4列:月1~12
第5列:星期0~7(0和7表示星期天)
第6列:要运行的命令

示例:

# 每1分钟执行一次Command
* * * * * Command

# 每小时的第3和第15分钟执行
3,15 * * * * Command

# 在上午8点到11点的第3和第15分钟执行
3,15 8-11 * * * Command

# 每隔两天的上午8点到11点的第3和第15分钟执行
3,15 8-11 */2  *  * Command

# 每周一上午8点到11点的第3和第15分钟执行
3,15 8-11 * * 1 Command

# 晚上11点到早上7点之间,每隔一小时重启smb
* 23-7/1 * * * /etc/init.d/smb restart
crontab [-u user] file 
crontab [-u user] [ -e | -l | -r ]

2. Mac使用crontab服务

与 Linux 不同的是,Mac的定时任务统统都由 launchctl 来管理。首先,看看 cron 任务有没有在里面:

$ sudo launchctl list | grep cron
-   0   com.vix.cron

暂时没有记录,查看一下启动项的配置:

$ locate com.vix.cron

WARNING: The locate database (/var/db/locate.database) does not exist.
To create the database, run the following command:

  sudo launchctl load -w /System/Library/LaunchDaemons/com.apple.locate.plist

Please be aware that the database can take some time to generate; once
the database has been created, this message will no longer appear.

database 不存在,按照提示步骤创建一个:

// 这个指令会花费一定时间
$ sudo launchctl load -w /System/Library/LaunchDaemons/com.apple.locate.plist
Password:

$ sudo launchctl load -w /System/Library/LaunchDaemons/com.apple.locate.plist
/System/Library/LaunchDaemons/com.apple.locate.plist: service already loaded

查看配置项:

$ cat /System/Library/LaunchDaemons/com.vix.cron.plist
<?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.vix.cron</string>
    <key>ProgramArguments</key>
    <array>
        <string>/usr/sbin/cron</string>
    </array>
    <key>KeepAlive</key>
    <dict>
        <key>PathState</key>
        <dict>
            <key>/etc/crontab</key>
            <true/>
        </dict>
    </dict>
    <key>QueueDirectories</key>
    <array>
        <string>/usr/lib/cron/tabs</string>
    </array>
    <key>EnableTransactions</key>
    <true/>
</dict>
</plist>

再次查找启动项配置时会显示已有文件:

$ locate com.vix.cron
/System/Library/LaunchDaemons/com.vix.cron.plist

注意,其中有个KeepAlive的条件是 /etc/crontab 是否存在:

    <key>KeepAlive</key>
    <dict>
        <key>PathState</key>
        <dict>
            <key>/etc/crontab</key>
            <true/>
        </dict>
    </dict>

再看是否是因为这个 /etc/crontab 不存在,导致 cron 里面的任务无法正常运行:

$  LaunchAgents  ll /etc/crontab
ls: /etc/crontab: No such file or directory

果然,这个文件不存在,创建一下:

$  sudo touch /etc/crontab

crontab -e创建一条定时任务,执行成功。

注意:
1.Linux和Mac下操作crontab都是一致的;
2.配置文件都在/etc/crontab下,如果没有就创建,sudo touch /etc/crontab;
3.测试发现直接使用crontab -e命令创建的定时任务是放在临时文件夹的(网上有人说重启会被删除,但是我重启了,并没有被删,还有待确认),并且与/etc/crontab文件无关联,怎样关联还有待研究;
4.sudo /etc/init.d/cron start 启动服务;
5.crontab -l 查看任务的运行情况。

参考文章:https://www.bbsmax.com/A/pRdBnE99dn/
感谢!

上一篇 下一篇

猜你喜欢

热点阅读