Margrop
Articles158
Tags353
Categories21
1password AC AP API AppDaemon Aqara Cron Date Diagrams.net HA HADashboard HomeAssistant IP IPv4 Java LVM‑Thin Linux MacOS MySQL NAS PPPoE PostgreSQL ProcessOn Proxmox VE SSL Shell TTS TimeMachine UML Uptime Kuma Web Windows activate ad adb adblock agent aligenie aliyun alpine annotation aop authy autofs backup baidupan bash bitwarden boot brew browser caddy2 cdn centos cert certbot charles chat chrome classloader client clone closures cloudflare cmd command commit container crontab ctyun ddsm demo dependency deploy developer devtools dll dns docker domain download draw drawio dsm dump dylib edge exception export fail2ban feign firewall-cmd flow frp frpc frps fuckgfw function gcc gfw git github golang gperftools gridea grub gvt-g hacs havcs heap hello hexo hibernate hidpi hoisting homeassistant hosts html htmlparser https idea image img img2kvm import index install intel io ios ip iptables iptv ipv6 iso java javascript jetbrains jni jnilib jpa js json jsonb jupter jupyterlab jvm k8s kernel key kid kms kodi koolproxy koolproxyr kvm lan lastpass launchctl learning lede letsencrypt linux live low-code lvm lxc m3u8 mac macos mariadb markdown maven md5 microcode mirror modem modules monitor mount mstsc mysql n2n n5105 nas network nfs node node-red nodejs nohup notepad++ npm nssm ntp oop openfeign openssl os otp ovz packet capture pat pdf pem perf ping pip plugin png powerbutton print pro proxy pve pvekclean python qcow2 qemu qemu-guest-agent rar reboot reflog remote remote desktop renew repo resize retina root route router rule rules runtime safari sata scipy-notebook scoping scp server slmgr so socks source spk spring springboot springfox ssh ssl stash string supernode svg svn swagger sync synology systemctl tap tap-windows tapwindows telecom template terminal tls token totp tvbox txt ubuntu udisk ui undertow uninstall unlocker upgrade url v2ray vhd vim vlmcsd vm vmdk web websocket wechat windows with worker wow xiaoya xml yum zip 中国电信 云电脑 交换机 光猫 公网IP 内存 内网IP 升级 启动 夏令时 天猫精灵 天翼云 安装 容器 导入 小米 常用软件 广告屏蔽 序列号 应用市场 异常 抓包 描述文件 时区 显卡虚拟化 智能家居 智能音箱 梯子 模块 流程 流程图 浏览器 漫游 激活 火绒 电信 画图 直播源 续期 网关 网络风暴 群晖 腾讯 虚拟机 证书 路由 路由器 软件管家 软路由 运维监控 镜像 镜像源 门窗传感器 防火墙 阿里云 阿里源 集客

Hitokoto

Archive

MacOS 自定义登录和注销脚本

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.

使用该defaults工具安装您的登录脚本。创建脚本文件并将其放在所有用户都可以访问的目录中。在终端中,使用以下命令安装脚本(其中/path/to/script是脚本文件的完整路径):
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):

1
sudo defaults write com.apple.loginwindow LoginHook /path/to/script

要删除此挂钩,请删除该属性:
To remove this hook, delete the property:

1
sudo defaults delete com.apple.loginwindow LoginHook

使用相同的过程添加或删除注销挂钩,但键入LogoutHook而不是LoginHook
Use the same procedure to add or remove a logout hook, but type LogoutHook instead of LoginHook.

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

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

如果必须以编程方式安装启动脚本,则应考虑提供包含默认配置选项的此文件的副本。然后,如果该文件不存在,请在运行defaults之前将该默认配置文件复制到位。同样,强烈建议应用程序开发人员不要使用登录或注销脚本,因为只能安装一个这样的脚本。

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.

参考文章

https://developer.apple.com/library/archive/documentation/MacOSX/Conceptual/BPSystemStartup/Chapters/CustomLogin.html

Author:Margrop
Link:http://blog.margrop.com/post/macos-lign/
版权声明:本文采用 CC BY-NC-SA 3.0 CN 协议进行许可