WeChat public account: [Front end cook in a pot]
A little technique, a little thinking.
For questions or suggestions, please leave a message on the official account.
Launchd is a key process used to initialize the system environment under Mac OS. It is the first process started in the OS environment after the kernel is successfully loaded. It can be used to control the automatic startup or shutdown of the service.
Its function is what we usually call the daemon. Simply put, the user daemon is a non-graphical program that runs in the background as a part of the system.
It is very simple to configure the self-starting item in this way. Only one plist file is needed. The directories where the file exists are:
LaunchDaemons before user login:
~/Library/LaunchDaemons
LaunchAgents after the user logs in:
~/Library/LaunchAgents
Create plist file
We can write a plist file and put it under ~/Library/LaunchAgents
. The file describes the program path and startup parameters. When we log in to the computer, the program will be launched, and it cannot be killed. After being killed, it will automatically restart.
Let's take mongodb as an example to demonstrate the whole process.
- First create a .plist file:
cd ~/Library/LaunchAgents && ls # 进入到目录
touch org.mongodb.mongod.plist # 创建 plist 文件
- Copy the following
<?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>KeepAlive</key>
<dict>
<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>
- Modify the program path and startup parameters
Paste the content you just copied into the org.mongodb.mongod.plist file, and then modify the above content according to your own actual situation:
- The mongod installation path
/usr/local/mongodb/bin/mongod
can use thewhich mongodb
command to view the specific path - The mongod configuration file path
/usr/local/etc/mongod.conf
If the configuration file is not used, delete this - mongodb working directory
/usr/local/var/mongodb
- StandardErrorPath error log output path, which can be found in the mongod configuration file
- StandardOutPath information log output path, which can be found in the mongod configuration file
Load plist file
After editing the plist file, we need to load the file into launchd and use the launchctl command. The specific commands are as follows:
Use launchctl to start mongodb
launchctl load -w ~/Library/LaunchAgents/org.mongodb.mongod.plist
Stop the self-starting of mongodb
launchctl unload -w ~/Library/LaunchAgents/org.mongodb.mongod.plist
Set alias
For ease of use, we can set aliases for mongodb's opening and closing commands:
vim ~/.bash_profile # Write global variables to the configuration file
# mongod 启动、停止、重启别名
alias mongod.start='launchctl load -w ~/Library/LaunchAgents/org.mongodb.mongod.plist'
alias mongod.stop='launchctl unload -w ~/Library/LaunchAgents/org.mongodb.mongod.plist'
alias mongod.restart='mongod.stop && mongod.start'
# redis 启动、停止、重启别名
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 启动、停止、重启别名
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 # Execute to take effect
plist command
The full name of Plist is Property lists, which is a file used to store serialized objects.
The file extension of the property list file is .plist, so it is usually called a plist file.
Plist files are usually used to store user settings, and can also be used to store bundled information.
plist command:
launchctl load # load
launchctl unload # cancel
launchctl list # View the services loaded by launchctl
-w # Start modification immediately
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。