MacOSLaunchd&LaunchDaemon&LaunchAgent&.plist file preparation

Hits: 1

launchd official documentation>>
Detailed article 1>>
Detailed article 2>>

what is launchd

From the official documentation:”Wikipedia defines launchd as "a unified, open-source service management framework for starting, stopping and managing daemons, applications, processes, and scripts. Written and designed by Dave Zarzycki at Apple, it was introduced with Mac OS X Tiger and is licensed under the Apache License."

It can be understood as launchda unified open source service management framework, which is used to start, stop and manage background programs, applications, processes and scripts. launchdIt is the first process started by macOS. The PID of this process is 1, and other processes of the entire system are created by it. When launchd starts, it scans /System/Library/LaunchDaemonsthe files /Library/LaunchDaemonsin plistand and loads them; when you enter the password to log in to the system, launchd scans the plist files in the three directories, , and loads them /System/Library/LaunchdAgents. Each file is a task, loading does not mean running immediately, only when it is set to or is will load and start these tasks at the same time/Library/LaunchAgents~/Library/LaunchAgents

================

Daemon, called in English Daemon, is actually a program running in the background. It has no interface and you can’t see it. Generally, commands are used to manage and control its process. The daemon is often set to start automatically. Startup (of course, it can also be started manually with a command after booting), the”server side”of many software generally runs as a daemon process, such as databases, memory caches, web servers, etc. are all daemon processes. They provide external services through”interfaces” (such as Unix socket / tcp mode, etc.).
Daemonsand Agentsare all background programs managed by launchd. The difference between them is that Agent belongs to the currently logged-in user (that is, the user name when you enter the password after booting), and they are started with the currently logged-in user authority, while Daemon belongs to the root user, but since it has root user authority, it can specify what user to run as, or it can not be specified (if it is not specified, it will run as the root user).

How launchd manages background processes

launchdIt defines the self-start of a program through a file ending with a “.plist” suffix , and we generally call it plista file.

Write a simple .plist configuration file to start an Agent process

1. Use XCode to write a program launchd_test that needs to be run

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        NSProcessInfo *proc = [NSProcessInfo processInfo];
        NSArray *args = [proc arguments];
        // insert code here...
        NSLog(@"%@",args[1]);
        NSLog(@"Hello, World!");
    }
    return 0;
}

ctrl + B compiles into an executable programlaunchd_test

2. Write the .plist configuration file

launchd reads the .plist file and decides how to start the process

1 <?xml version="1.0" encoding="UTF-8"?>
  2 <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
  3 <plist version="1.0">
  4     <dict>
  5         <key>Label</key>
  6         <string>local.launchd_test.app</string>
  7         <key>Program</key>
  8         <string>/Users/mac/Documents/launchd_test</string>
  9         <key>ProgramArguments</key>
 10         <array>
 11                 <string>/Users/mac/Documents/launchd_test</string>
 12                 <string>happy</string>
 13         </array>
 14         <key>RunAtLoad</key>
 15         <true/>
 16     </dict>
 17 </plist>

LabelSet a label for the process that needs to be started, here the name is local.launchd_test.app
Programset to the path of the executable program that needs to be started, and the path of the executable program that needs to be started is set
ProgramArgumentsto the parameters required for the program to run . If it starts at load time, you need to start it manually. For the user agent process, we put the .plist file in the ~/Library/ LaunchAgents . file, and for the daemon process, we put it in /Library/LaunchDaemons in filemain[0]
RunAtLoad
local.launchd_test.app.plist

3. Load, unload, start, stop, view launchd list

List all processes managed by launchd

launchctl list

View a specific process

host:~ user$ launchctl list | grep com.example.app
-   2   com.example.app

  1. First, after creating the .plist file, you need to load the file,

launchctl load ~/Library/LaunchAgents/local.launchd_test.app.plist

Normally will not output anything, if output

Load failed: 5: Input/output error
Try running {{EJS11}} as root for richer errors.

Can try

launchctl unload ~/Library/LaunchAgents/local.launchd_test.app.plist
launchctl load ~/Library/LaunchAgents/local.launchd_test.app.plist

2.launchd starts the agent process

launchctl start local.launchd_test.app

At this point, you can view the process number through the launchctllistlaunchd_test

launchctl list | grep local.launchd_test.app

Uninstall and close commands

launchctl unload ~/Library/LaunchAgents/local.launchd_test.app.plist
launchctl stop local.launchd_test.app

After starting the process through launchd, the output cannot be seen on bash. If you need to view the output, you need to redirect the NSLog output to the specified folder ===”

You may also like...

Leave a Reply

Your email address will not be published.