WeChat official account: [front-end one-pot cooking]


A little technology, a little thinking.


Questions or suggestions, please leave a message on the public account.

Launchd is a key process for initializing the system environment under Mac OS. It is the first process to be started in the OS environment after the kernel is successfully loaded. It can be used to control the automatic start and shutdown of services.

This is what we call a daemon. Simply put, a user daemon is a non-graphical program that runs in the background as part of the system.

Configure a bootstrap item this way is simple. All you need is a plist file that resides in directories such as:

LaunchDaemons before user login:

~/Library/LaunchDaemons

Children and senior citizens after user login:

~/Library/LaunchAgents

Create the plist file

Do not obstruct, or start as a chain chain? We can write a plist file to ~/Library/ launcha. this file describes the program path and launch parameters. This program is started as soon as we log in.

Let’s take MongoDB as an example to demonstrate the whole process.

  1. First create a.plist file:
Do not obstruct or place a chain as a means of entry to the directory touch org.mongodb.mongod.plist
  1. Copy the following
<? The XML version = "1.0" encoding = "utf-8"? > <! DOCTYPE plist PUBLIC "- / / / / DTD plist Apple / 1.0 / EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd" > < plist Version ="1.0"> <dict> <key>KeepAlive</key> <key> <key>SuccessfulExit</key> <false/> </dict> <key>Label</key> <string>org.mongodb.mongod</string> <key>ProgramArguments</key> <array> <string>/usr/local/mongodb/bin/mongod</string> <string>run</string> <string>--config</string> <string>/usr/local/etc/mongod.conf</string> </array> <key>RunAtLoad</key>  <true/> <key>WorkingDirectory</key> <string>/usr/local/var/mongodb</string> <key>StandardErrorPath</key> <string>/usr/local/var/log/mongodb/error.log</string> <key>StandardOutPath</key> <string>/usr/local/var/log/mongodb/mongo.log</string> </dict> </plist>
  1. Modify the program path and startup parameters

Paste what you just copied into the org.mongodb.mongod.plist file and then modify it to suit your own situation:

  • The mongod installation path/usr/local/mongodb/bin/mongodYou can usewhich mongodbCommand to view the specific path
  • The path to the mongod configuration file/usr/local/etc/mongod.confDelete this if you are not using the configuration file
  • MongoDB working directory/usr/local/var/mongodb
  • The StandardErrorPath error log output path can be found in the mongod configuration file
  • The StandardOutPath information log output path can be found in the mongod configuration file

Load the plist file

After editing the plist file, we need to load it into launchd using the command launchctl:

Start MongoDB with launchctl

launchctl load -w ~/Library/LaunchAgents/org.mongodb.mongod.plist

Stop self-starting MongoDB

launchctl unload -w ~/Library/LaunchAgents/org.mongodb.mongod.plist

Set an alias

For ease of use, we can alias MongoDB’s open and close commands:

Vim ~/.bash_profile # Writes global variables to the configuration file

# mongod start, stop, restart the alias alias mongod. Start = 'launchctl load - w ~ / Library/LaunchAgents/org. The mongo. Mongod. Plist' alias mongod.stop='launchctl unload -w ~/Library/LaunchAgents/org.mongodb.mongod.plist' alias mongod.restart='mongod.stop && Mongod. Start '# redis start, stop, restart the alias alias redis. Start =' launchctl load - w ~ / Library/LaunchAgents/org. Redis. Plist 'alias redis.stop='launchctl unload -w ~/Library/LaunchAgents/org.redis.plist' alias redis.restart='redis.stop && redis.start' # nginx start, stop, restart the alias alias nginx. Start = 'launchctl load - w ~ / Library/LaunchAgents/org. Nginx. Plist' alias nginx.stop='launchctl unload -w ~/Library/LaunchAgents/org.nginx.plist' alias redis.restart='nginx.stop && nginx.start'

Soucre ~/.bash_profile # executes to make it effective

Plist command

The full name of Plist is Property lists, which is a file used to store serialized objects.

The file extension for a property list file is.plist, so it is often referred to as a plist file.

Plist files are usually used to store user Settings, but can also be used to store bundled information.

Plist command:

Launchctl load #

Launchctl unload # Cancelled

Launchctl list # View the services that launchctl loads

-W # Start the changes now