#macos #service #system #launcher

launchctl

A simple library for managing system services on MacOS

5 unstable releases

0.3.2 Nov 30, 2024
0.3.1 Nov 30, 2024
0.2.1 Oct 29, 2024
0.1.1 Oct 29, 2024
0.1.0 Oct 29, 2024

#74 in macOS and iOS APIs

Download history 315/week @ 2024-10-28 20/week @ 2024-11-04 35/week @ 2024-11-18 237/week @ 2024-11-25 44/week @ 2024-12-02 55/week @ 2024-12-09

371 downloads per month
Used in srhd

MIT license

10KB
138 lines

Launchctl

Tiny Rust wrapper library for MacOS service launcher launchctl. This library offers a more intuitive interface for managing services on MacOS. Other Rust crates exist for interfacing with cross platform launch services. This library is specifically for MacOS. For more info about launchctl and launchd see the official apple docs.

Install

cargo add launchctl

[!NOTE] Coming soon is a CLI version which will make it easier to automate creating a service.

Usage

The Service struct is the main entry point of this library. It uses a bon builder.

fn main() {
    // basic construction of a service
    let basic_service = Service::builder()
        .name("com.<owner name>.<binary name>")
        .build();

    // more advanced construction of a service
    let more_custom = Service::builder()
        .name("com.<owner name>.<binary name>")
        .
        .build();

    // create a .plist file for the service
    // ...

    basic.start().unwrap();
    custom.stop().unwrap();
}

Limitations

Currently this crate does not support creating or modifying plist files. There are other crates that can give you this behavior https://github.com/koenichiwa/launchd, or you can hard code them as strings which is what I prefer.

Here is an example of how I do that in my srhd crate.

pub fn install(ctl: &launchctl::Service) -> Result<(), Error> {
    let plist = format!(
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">
<plist version=\"1.0\">
<dict>
    <key>Label</key>
    <string>{}</string>
    <key>ProgramArguments</key>
    <array>
        <string>{}</string>
    </array>
    <key>RunAtLoad</key>
    <true/>
        <key>KeepAlive</key>
    <dict>
        <key>SuccessfulExit</key>
 	     <false/>
 	     <key>Crashed</key>
 	     <true/>
    </dict>
    <key>StandardOutPath</key>
    <string>/tmp/srhd_sylvanfranklin.out.log</string>
    <key>StandardErrorPath</key>
    <string>/tmp/srhd_sylvanfranklin.err.log</string>
    <key>ProcessType</key>
    <string>Interactive</string>
    <key>Nice</key>
    <integer>-20</integer>
</dict>
</plist>",
        ctl.name,
        ctl.bin_path.to_str().unwrap(),
    );

    Ok(fs::write(ctl.plist_path.clone(), plist)?)
}

Contribution

Bro I love when people contribute or even submit issues. It's good for everyone's career and understanding of everything, by all means open an issue or a PR!

Dependencies

~0.8–1.4MB
~30K SLoC