简体   繁体   English

如何每天在Mac Os X中运行Shell脚本?

[英]How to run shell script daily in Mac Os X?

I am using Mac Os x. 我正在使用Mac OSX。 I have an shell script and i want to run this automatically daily for given time. 我有一个Shell脚本,我想在给定的时间内每天自动运行此脚本。 Is there anyway to do this without using third party tool. 无论如何,无需使用第三方工具即可执行此操作。

Yes of course, you can use crontab! 是的,当然可以使用crontab!

Firstly open the terminal, then launch crontab with: 首先打开终端,然后使用以下命令启动crontab:

crontab -e

In some cases you need to specify the editor (es.nano) like this: 在某些情况下,您需要像这样指定编辑器(es.nano):

env EDITOR=nano crontab -e

Now you can add your daily script at 3am like this: 现在,您可以像这样在凌晨3点添加每日脚本:

0  3  *  *  *  sh /path/to/your/file

The format is: 格式为:

min  hour  day_of_month  month  day_of_week  your_command

After save the cron, you can check the crontab list whit: 保存完cron之后,您可以检查crontab列表的内容:

crontab -l

And if you want remove it with: 如果要删除它,请执行以下操作:

crontab -r

You can do this via LaunchDaemons 您可以通过LaunchDaemons执行此操作

Just create a file with below syntax and put on /Library/LaunchDaemons/ and save it in .plist 只需使用以下语法创建文件,然后放在/ Library / LaunchDaemons /上,然后将其保存在.plist中

now script will run every 13:30 Hours - you can change time what ever you want 现在脚本将每13:30小时运行一次-您可以随时更改时间

<?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>
    <key>Label</key>
    <string>COM.COMPANY.LOGGER</string>
    <key>ProgramArguments</key>
    <array>
        <string>YOUR-SCRIPT-LOCATION</string>
    </array>
    <key>StartCalendarInterval</key>
    <dict>
          <key>Hour</key>
          <integer>13</integer>
          <key>Minute</key>
          <integer>15</integer>
    </dict>
</dict>
</plist>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM