demand

In some scenarios, the server needs to be restarted automatically. For example, some applications, such as the server database, are restarted automatically. In this case, we can use the following methods:

  1. Pm2 restart
  2. Launchctl daemon

Pm2 restart

Many nodeJS applications are now deployed using PM2, which comes with a self-restart function

Setup steps

  1. Start the application with PM2
pm2 start ./dist/client/ --watch --ignore-watch="node_modules"
Copy the code

Then use pM2 Status to check whether the application displays properly

pm2 status
Copy the code
  1. pm2 startup

After the pM2 startup command is executed on the terminal, the following output is displayed:

[PM2] Init System found: launchd
[PM2] To setup the Startup Script, copy/paste the following command:
sudo env PATH=$PATH:/usr/local/bin /usr/local/lib/node_modules/pm2/bin/pm2 startup launchd -u tuxiuluo --hp /Users/tuxiuluo
Copy the code
  1. In this case, copy the command displayed in the previous step as prompted and run it on the terminal

Execute on terminal:

sudo env PATH=$PATH:/usr/local/bin /usr/local/lib/node_modules/pm2/bin/pm2 startup launchd -u tuxiuluo --hp /Users/tuxiuluo
Copy the code

If normal, the terminal will output:

[PM2] Init System found: launchd Platform launchd Template <? 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>Label</key> <string> com.pm2 </string> <key>UserName</key> <string>tuxiuluo</string> <key>KeepAlive</key> <true/> <key>ProgramArguments</key> <array> <string>/bin/sh</string> <string>-c</string> <string>/usr/local/lib/node_modules/pm2/bin/pm2 resurrect</string> </array> <key>RunAtLoad</key> <true/> <key>OnDemand</key> <false/> <key>LaunchOnlyOnce</key> <true/> <key>EnvironmentVariables</key> <dict> <key>PATH</key> <string>/usr/local/bin</string> <key>PM2_HOME</key> <string>/Users/tuxiuluo/.pm2</string> </dict> <key>StandardErrorPath</key> <string>/tmp/com.PM2.err</string> <key>StandardOutPath</key> <string>/tmp/com.PM2.out</string> </dict> </plist> Target path /Users/tuxiuluo/Library/LaunchAgents/pm2.tuxiuluo.plist Command list [ 'launchctl load -w /Users/tuxiuluo/Library/LaunchAgents/pm2.tuxiuluo.plist' ] [PM2] Writing init configuration in /Users/tuxiuluo/Library/LaunchAgents/pm2.tuxiuluo.plist [PM2] Making script booting at startup... >>> Executing launchctl load -w /Users/tuxiuluo/Library/LaunchAgents/pm2.tuxiuluo.plist [DONE] +---------------------------------------+ [PM2] Freeze a process list on reboot via: $ pm2 save [PM2] Remove init script via: $ pm2 unstartup launchdCopy the code
  1. As you can see, this is actually using launchctl to set up a self-launching application

Set up since the start of the configuration file path in/Users/tuxiuluo/Library/LaunchAgents/pm2 tuxiuluo. Plist

Launchctl daemon

Launchctl definition

Launchctl can often be used for scheduled tasks.

In computing, launchd, a unified operating system service management framework, starts, stops and manages daemons, applications, processes, And scripts in macOS. It was introduced with Mac OS X Tiger and is licensed under the Apache License. — Wikipedia

English official website address

The core is to define a set of self-starting application configuration files

Type Lacation Run on behalf of
User Agents ~/Library/LaunchAgents Current logged in user
Global Agents /Library/LaunchAgents Current logged in user
Global Daemons /Library/LaunchDaemons root or the user sepcified with the key UserName
System Agents /System/Library/LaunchAgents Current logged in user
System Daemons /System/Library/LaunchDaemons root or the user sepcified with the key UserName

The startup sequence is as follows:

  1. The computer starts, without login, the System will scan/System/Library/LaunchDaemons/and/Library/LaunchDaemons/configuration
  2. User logged in, the System will scan/System/Library/LaunchAgents, / Library/LaunchAgents, and user directory: ~ / Library/LaunchAgents directory in the configuration

As we saw in the first section, pM2’s self-start is placed in User Agents

Launchctl configuration steps

Next we make a self-start application, used to print ls-al ~ every time we start the computer

Go to ~/Library/LaunchAgents and create a new configuration file named com.startup.test.plist.

<? 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>Label</key> <string>com.startup.test</string> <key>UserName</key> <string>tuxiuluo</string> <key>KeepAlive</key> <true/> <key>ProgramArguments</key> <array> <string>/bin/sh</string> <string>ls -al ~</string> </array> <key>RunAtLoad</key> <true/> <key>OnDemand</key> <false/> <key>LaunchOnlyOnce</key> <true/> <key>StandardErrorPath</key> <string>/Users/tuxiuluo/com.startup.test.err</string> <key>StandardOutPath</key> <string>/Users/tuxiuluo/com.startup.test.out</string> </dict> </plist>Copy the code

Then type in the command

launchctl load -w /Users/tuxiuluo/Library/LaunchAgents/com.startup.test.plist
Copy the code

Next, look at the loaded list:

launchctl list
Copy the code

Then restart the computer to/Users/tuxiuluo/com. Startup. Test. The out looking for the right output

In the pit of

Permission problems

After testing the plist file in /Library/LaunchAgents, the permissions need to be:

Root :wheel, and the executable time limit is 600, otherwise, the setting will not respond

sudo chown root:wheel /Library/LaunchDaemons/myfile.plist
sudo chmod 600 /Library/LaunchDaemons/myfile.plist
Copy the code