Run script on networked user login
-
14-01-2021 - |
Domanda
The computer lab is running 10.8. I would like to run a script when networked users login. As far as I can tell my choices are:
launchd agents - Not meant for this. On the launchd.plist man page it says "An agent launched by launchd SHOULD NOT do the following as a part of their startup initialization: Setup the working directory." That's basically what my script does.
login items - A number of posts indicate that they worked in 10.7 but don't work in 10.8
login hooks - This works but the "Customizing Login and Logout" on developer.apple.com says "There are numerous reasons to avoid using login and logout scripts: Login and logout scripts are a deprecated technology."
I don't want to use something that won't work tomorrow. I would like to do this the "right way" if there is one. Please advise. Thanks.
Soluzione
Setting the Working Directory for a Script
You should use a launchd agent job placed in the /Library/LaunchAgents
folder.
The advice you mention, quoted below, is aimed at the executable or script being launched. You should let launchd
manage the working directory for you.
A daemon or agent launched by launchd SHOULD NOT do the following as a part of their startup initialization:
- Setup the user ID or group ID.
- Setup the working directory.
- chroot(2)
- setsid(2)
- Close "stray" file descriptors.
- Change stdio(3) to /dev/null.
- Setup resource limits with setrusage(2).
- Setup priority with setpriority(2).
- Ignore the SIGTERM signal.
The launchd job ticket is responsible for requesting the desired working directory; use the WorkingDirectory
key to set the directory. The directory will be automatically set before your executable or script is launched.
A full description of the available keys in a launchd job ticket are available in the launchd.plist(5) manual page.
Setting the Working Directory for the User
Consider providing a modified ~/.bashrc or other shell login script to provide a known current working directly.