Passing Environment Variable For Program On Launchctl Macos

Nov 21, 2019  Apple/Mac launchd FAQ: Can you share some MacOS launchd examples (also written as launchd plist examples, or launchctl examples)? In an earlier tutorial (MacOS startup jobs with crontab, launchctl, and launchd) I demonstrated how to use the MacOS launchd facility instead of cron to run what would normally be a cron (crontab) job.As I started working with launchd and launchctl, I realized it.

Passing Environment Variable For Program On Launchctl Macos
Modifying this control will update this page automatically

Terminal User Guide

The shell uses environment variables to store information, such as the name of the current user, the name of the host computer, and the default paths to any commands. Environment variables are inherited by all commands executed in the shell’s context, and some commands depend on environment variables.

You can create environment variables and use them to control the behavior of a command without modifying the command itself. For example, you can use an environment variable to have a command print debug information to the console.

To set the value of an environment variable, use the appropriate shell command to associate a variable name with a value. For example, to set the variable PATH to the value /bin:/sbin:/user/bin:/user/sbin:/system/Library/, you would enter the following command in a Terminal window:

% PATH=/bin:/sbin:/user/bin:/user/sbin:/system/Library/ export PATH

To view all environment variables, enter:

When you launch an app from a shell, the app inherits much of the shell’s environment, including exported environment variables. This form of inheritance can be a useful way to configure the app dynamically. For example, your app can check for the presence (or value) of an environment variable and change its behavior accordingly.

Passing Environment Variable For Program On Launchctl Macos 10

Different shells support different semantics for exporting environment variables. For information, see your preferred shell’s man page.

Although child processes of a shell inherit the environment of that shell, shells are separate execution contexts that don’t share environment information with each other. Variables you set in one Terminal window aren’t set in other Terminal windows.

After you close a Terminal window, variables you set in that window are no longer available. If you want the value of a variable to persist across sessions and in all Terminal windows, you must set it in a shell startup script. For information about modifying your zsh shell startup script to keep variables and other settings across multiple sessions, see the “Invocation” section of the zsh man page.

See alsoApple Support article: Use zsh as the default shell on your MacOpen or quit Terminal on Mac

Interfaces with launchd to load, unload daemons/agents and generally control launchd. launchctl supports taking subcommands on the command line, interactively or even redirected from standard input.

Files

~/Library/LaunchAgents Per-user agents provided by the user.
/Library/LaunchAgents Per-user agents provided by the administrator.
/Library/LaunchDaemons System wide daemons provided by the administrator.
/System/Library/LaunchAgents macOS Per-user agents.
/System/Library/LaunchDaemons macOS System wide daemons.

launchd no longer loads configuration files from the network

Exit Codes

launchctl will exit with status 0 if the subcommand succeeded.
Otherwise, it will exit with an error code that can be given to the error subcommand to be decoded into human-readable form.

The syntax for launchctl changed in macOS 10.10 (Yosemite) the examples below show both the old and new syntax for starting and stopping launch scripts (services). The new syntax generally does not require sudo.

Examples

Display the launch scripts for all users that are currently loaded/running (legacy syntax):
$ sudo launchctl list
Display the launch scripts that are currently loaded for user ID 504 (new syntax):
$ launchctl print gui/504

Display the launch scripts that are currently loaded/running for root:
$ launchctl print system

Enable remote SSH login (legacy syntax):
$ sudo launchctl load -w /System/Library/LaunchDaemons/ssh.plist

Enable remote SSH login for login 504 (new syntax):
$ launchctl bootstrap gui/504 /System/Library/LaunchDaemons/ssh.plist

Disable remote SSH login (legacy syntax):
$ sudo launchctl unload -w /System/Library/LaunchDaemons/ssh.plist

Disable remote SSH login for login 504 (new syntax):
$ launchctl bootout gui/504 /System/Library/LaunchDaemons/ssh.plist

Disable the macOS Notification Centre (legacy syntax):
$ launchctl unload -w /System/Library/LaunchAgents/com.apple.notificationcenterui.plist
$ killall NotificationCenter

Disable the macOS Notification Centre for login 504 (new syntax):
$ launchctl bootout gui/504 /System/Library/LaunchAgents/com.apple.notificationcenterui.plist
$ killall NotificationCenter

An alternative is to turn on the Notification “Do Not Disturb” from the Menu Bar of macOS which lasts for 1 day.

Re-enable the macOS Notification Centre (legacy syntax):
$ launchctl load -w /System/Library/LaunchAgents/com.apple.notificationcenterui.plist
Then double-click '/System/Library/CoreServices/Notification Center” to launch it again.

Re-enable the macOS Notification Centre for login 504 (new syntax):
$ launchctl bootstrap gui/504 /System/Library/LaunchAgents/com.apple.notificationcenterui.plist
Then double-click '/System/Library/CoreServices/Notification Center” to launch it again.

“A good rule for rocket experimenters to follow is this: always assume that it will explode” ~ 'Astronautics,' issue 38, October 1937

Related macOS commands:

The macOS equivalent of Windows services is Launchd.
The macOS equivalent of services.msc on Windows is launchctl.
Launchd.info - Description of launchd, agents and daemons.
The daemons managed by launchd can be on demand or can be triggered periodically (this is configurable in launchd.plist).
launchd.plist(5), launchd.conf(5), launchd(8).

Passing Environment Variable For Program On Launchctl Macos Free

Copyright © 1999-2020 SS64.com
Some rights reserved