Let Mac OS x system execute scripts when it starts

Source: Internet
Author: User



The company purchased Zoho's asset management software Assetexplorer, which collects client hardware and software information by installing Agent client software on the client computer. The company has several imac computers running the MAC OS X 10.6 System, because Assetexplorer only supports the Windows version of the agent, for other system computers need to execute Bash script files on the client, Collects the client's information and uploads it to the server. This requires the client's Mac OS x system to automatically execute the script when booting, and based on the system service level, the user does not need to log in, the script can also run in the background, it is best to script the background can also be fixed time automatically run.






We can use a mechanism called launch daemon/agent built into Mac OS X to automate the execution of scripts at system startup. Mac OS x starts with 10.4 and uses the LAUNCHD process to manage the entire operating system's services and processes. Traditional UNIX uses/etc/rc.* or other mechanisms to manage the startup service to start when booting, and now Mac OS X is managed using LAUNCHD, which is called launch daemon/agents. With launch Daemon/agent, we can make the script run in the background when the system starts.





    • The difference between Launch daemon and Launch agent


Launch Daemon and Launch agents are names of the same kind of things in different application scopes. Launch Daemon is a system-level service, called the Daemon,launch agent, which is a user-level service, called an agent, that is loaded when the user logs on and then loaded after the login. So we run the script in the launch daemon way.





    • Export a script file from Assetexolorer


Select scan → isolate workstation audit → script Scan workstation →mac Export the script file ae_scan_mac.sh, and modify hostname= "Server IP address or domain name address" in the script file and check if PortNo is correct.





    • Create a plist configuration file


The LAUNCHD process needs to read an XML-formatted plist configuration file, create the file, and place it in the/system/library/launchdaemons/or/library/launchdaemons/directory. The file can be pre-created under Windows, and it needs to be created using a text editor (such as UltraEdit) that supports UNIX format.






Where the plist file format and the meaning of each field are:



Examples of ae_scan_mac.plist files:



<?xmlversion="1.0"encoding="UTF-8"?><!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN"     "http://www.apple.com/DTDs/PropertyList-1.0.dtd"><plistversion="1.0"><dict>    <key>Label</key>        <string>com.manageengine.ae_scan_mac</string>    <key>Program</key>    <string>ae_scan_mac.sh</string>    <key>RunAtLoad</key>         <true/>    <key>WorkingDirectory</key>        <string>/Library/LaunchDaemons/</string>    <key>KeepAlive</key>         <false/>     <key>StartCalendarInterval</key>     <dict>               <key>Hour</key>         <integer>11</integer>         <key>Minute</key>        <integer>0</integer>     </dict> </dict></plist>







One of the key words explained:



1)Label (required)



The name of the service.



2)programargument is required and must include the program keyword in the absence of programargument



Specifies the path and name of the executable file.



3)runatload (optional)



Identifies the executable file specified by Launchd to start the path immediately after the service has been loaded. The default value is False. Set to True to enable the run script file to start.



4)workingdirectory (optional)



Specifies the path to the current working directory before running the executable file.



5)KeepAlive (optional)



This keyword can be used to control whether the executable is kept running and the default value is False, which means that the specific condition is met before it is started. When the value is set to Ture, it indicates that the executable file is unconditionally opened and kept within the entire system run cycle.



6)startcalendarinterval (optional)


This keyword can be used to set a timed execution executable program, you can use the month, day, Hour, minute, and other sub-keywords, it can specify how many months, days, hours, minutes, weeks, and so on, if a keyword is missing any of that point in time, A Unix-like crontab the way the task is set up, such as setting the script file to run 11 o'clock every day in this example.

All key keyword Detailed instructions can be queried using the command man launchd.plist under Mac OS x terminal.




    • Deploying configuration files and script files on client computers


1) First set a password for the current user of Mac OS x system, set it in System Preferences → account.



You can then copy the script files ae_scan_mac.sh and plist files ae_scan_mac.plist to a temporary directory on your local hard disk by sharing, and so on, and then open utility → terminal using the following command:



sudo mv ae_scan_mac.*/library/launchdaemons



Move the two files to the/library/launchdaemons/directory.






2) Modify file permissions:



sudo chown root:wheel/library/launchdaemons/ae_scan_mac.*



sudo chmod a+x/library/launchdaemons/ae_scan_mac.sh



sudo chmod 644/library/launchdaemons/ae_scan_mac.plist











3) editing and checking of plist files



You can use the command sudo vim ae_scan_mac.plist to modify the file.



Check the syntax of the plist file:



Plutil ae_scan_mac.plist



Returning OK indicates that the file syntax is correct.






4) Start the service and load the Plsit file:



sudo launchctl load/library/launchdaemons/ae_scan_mac.plist



Or you can restart your Mac computer.



Note that each time you finish modifying the plist file, you must reload the plist file with the following command, or restart your computer, plist the configuration will take effect.



sudo launchctl unload/library/launchdaemons/ae_scan_mac.plist



sudo launchctl load/library/launchdaemons/ae_scan_mac.plist






Next, check the operational status of the service:



sudo launchctl list








5) After the final script is run, the client's information can be seen on the Assetexplorer server, and then the client will automatically upload the machine information to the server after the time set by the plist file or after restarting the machine.






There are several places to note for deployment of two files:



1) file permissions must be correct, two file owners must be root, script files must have execute permissions.



2) The syntax of the plist file must be correct, pay attention to the configuration of the program working directory and operating parameters do not be wrong. Executable: sudo launchctl start/library/launchdaemons/ae_scan_mac.plist



Force the service to run, or run the script directly to test if the script file is running properly.



3) Each time the plist file is modified, you must perform a launchctl unload/load uninstall and reload the plist file to make the new configuration file effective immediately.



Scripts and configuration files once deployed on multiple clients, Assetexplorer can automatically collect hardware and software information for all Mac computers, even if the machine configuration changes and does not need to be manually added to facilitate the company's asset management. The LAUNCHD process is powerful, Apple recommends and has used this process instead of the traditional cron, and it's interesting to have a look at it.






Let Mac OS x system execute scripts when it starts


Related Article

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.