Mac uses launchctl to regularly run programs

Source: Internet
Author: User

Mac uses launchctl to regularly run programs. in linux, crontab can be used to periodically execute tasks, in mac, launchctl can be used for scheduled task execution. In this example, we use launchctl to create a scheduled task execution script. The Script Name Is run123.sh, the script function is to create a file under the/Users/alecyan/Downloads/directory. The script should be changed to the executable permission chmod 777 run123.sh. The script code is as follows: java code cd/Users/alecyan/Downloads/touch abcabc123.txt and then enter ~ Create a plist file under/Library/LaunchAgents. This is the file name used by the worker system to execute the task. The file name is com. alecyan. testcron. plist. The content of the file is as follows: Java code <? 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. alecyan. testcron </string> <key> ProgramArguments </key> <array> <string>/Users/alecyan/Downloads/run123.sh </string> </array> <key> StartCalendarInterval </key> <dict> <key> Minute </key> <integer> 4 </integer> <key> Hour </key> <integer> 13 </integer> </ Dict> <key> StandardOutPath </key> <string>/Users/alecyan/Downloads/abc. log </string> <key> StandardErrorPath </key> <string>/Users/alecyan/Downloads/abcerror. log </string> </dict> </plist> briefly describes the content here. label indicates the name of the task. Here, the plist file name is generally used, this name cannot be the same as other plist names. Run123.sh is the script we want to execute, the parameter in StartCalendarInterval indicates that the script can be executed at every day, and then the following commands can be used to operate the task. The Java code launchctl load com is completed. alecyan. testcron. plist launchctl unload com. alecyan. testcron. plist launchctl start com. alecyan. testcron. plist launchctl stop com. alecyan. testcron. plist launchctl list to load the plist file we have prepared, it is to use the first command load ran, at this time, the compaction system will execute our script at every day. If you want to remove our scheduled task, you can run the unload command. If a task has been executed at today, then you changed it. com. alecyan. testcron. for example, if the time in plist is changed to, it must be unloaded and re-loaded. Otherwise, the start command will not be executed on the same day to test the task. This command is executed immediately, no matter when the time is reached, you can stop the task. OK. A simple scheduled task can be used. In fact, there are usually three directories such as/Library/LaunchAgents on mac, we are talking about the contents in the current user's directory, there are two other users in/Library/LaunchAgents and the other one in/System/Library/LaunchAgents/. If no user needs to execute them regularly, they should be placed under/Library/LaunchAgents.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.