MacOS 自定义登录和注销脚本

登录和注销脚本(Login and Logout Scripts)

Important: There are numerous reasons to avoid using login and logout scripts:

  • 登录和注销脚本是一种不推荐使用的技术。在大多数情况下,您应该改用launchd作业,如创建启动守护程序和代理中所述。
  • Login and logout scripts are a deprecated technology. In most cases, you should use launchd jobs instead, as described inCreating Launch Daemons and Agents.
  • 登录和注销脚本以 root 身份运行,这存在安全风险。
  • Login and logout scripts are run as root, which presents a security risk.
  • 一次只能安装每个脚本中的一个。它们适用于系统管理员;应用程序开发人员不应在已发布的软件中使用它们。
  • Only one of each script can be installed at a time. They are intended for system administrators; application developers should not use them in released software.

在登录时运行应用程序的一种方法是使用自定义 shell 脚本启动它们。创建脚本文件时,请记住以下几点:
One way to run applications at login time is to launch them using a custom shell script. When creating your script file, keep the following in mind:

  • 脚本文件的权限应包括相应用户的执行权限。
  • The permissions for your script file should include execute privileges for the appropriate users.
  • 在您的脚本中,该变量$1返回登录用户的短名称。
  • In your script, the variable $1 returns the short name of the user who is logging in.
  • 其他登录操作会等到您的钩子完成执行。因此,您的脚本需要快速运行。
  • Other login actions wait until your hook finishes executing. Therefore, your script needs to run quickly.

Use the defaults tool to install your login script. Create the script file and put it in a directory that is accessible to all users. In Terminal, use the following command to install the script (where /path/to/script is the full path to your script file):

sudo defaults write LoginHook /path/to/script

To remove this hook, delete the property:

sudo defaults delete LoginHook

Use the same procedure to add or remove a logout hook, but type LogoutHook instead of LoginHook.

注意: 如果不存在plist文件,则此方法将不起作用。在用户更改登录窗口设置(例如打开快速用户切换)之前,全新安装中不存在此文件(/var/root/Library/Preferences/。

Note: If no plist file exists for, this method will not work. This file (/var/root/Library/Preferences/ does not exist on a fresh installation until the user changes a login window setting (such as turning on fast user switching).


If you must install startup scripts programmatically, you should consider providing a copy of this file containing the default configuration options. Then, if the file does not exist, copy that default configuration file into place before running defaults. Again, application developers are strongly discouraged from using login or logout scripts, because only one such script may be installed.