2

My script only runs after logging into the user FooUser. I would like to bash file to run before login. I am not sure why it only runs after user login.

The plist and path for boot-script.plist:

/Library/LaunchAgents/boot-script.plist

<?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>boot-script</string>

<key>ProgramArguments</key>
<array>
  <string>/usr/local/bin/boot.sh</string>
</array>

<key>RunAtLoad</key>
<true/>

</dict>
</plist>

The bash script and path for boot.sh:

/usr/local/bin/boot.sh

#!/bin/bash

function startup(){

    cd /Users/FooUser/Desktop
    touch FooFile
}

startup;

I believe this is the proper setup for startup processes, having the plist stored in the LaunchAgents

  • launchctl load -w boot-script.plist

  • boot-script.plist owner is root and group is wheel

  • boot.sh permission set to executable

2 Answers 2

1

You can't run a script before log-in, but you can call it as part of the log-in process.

The usual place to do this is in ~/.bashrc: you will need to look at what's in it already and decide on the appropriate place in the log-in sequence.

You will also need to decide if you want to call it in a subshell, which will perform the touch but leave the calling shell unchanged:

boot.sh

or whether to call it in the log-in shell itself, which will set the current directory to /Users/FooUser/Desktop and make the startup function available after log-in:

. boot.sh
2
  • That seems to make more sense. With OS X design it doesn't seem to be necessary to run processes before login. Jun 21, 2018 at 23:43
  • I don't really understand how Plists are used (I'm a Linux user), but it looks as though the Plist itself invokes the boot.sh script when the application starts, without the need to call it in the shell start-up.
    – AFH
    Jun 21, 2018 at 23:52
0

Add this to your plist

LimitLoadToSessionType LoginWindow

And place your script in /Library/PrivilegedHelperTools

Set ownership and permissions on the plist to root:wheel 644

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .