0

In this question, a response details that com.apple.loginwindow login hooks can be used to run script as root on user login.

I have followed the instructions by using

sudo defaults write com.apple.loginwindow LoginHook /Users/Shared/Test.sh

except nothing is executed on login. My script makes use of a small background process and nothing else and works by typing it into my terminal so I am lead to believe that it simply isn't possible anymore.

If this method (as it is already deprecated) no longer works, are there any method that currently work for running login scripts as root?

9
  • Have you tried adding it to your crontab?
    – JMY1000
    Commented Jan 26, 2020 at 19:18
  • @JMY1000 If by crontab you mean launchd agents, the answer in the link by mklement0 suggests it is run as the user logging in and not root.
    – agjertsen
    Commented Jan 26, 2020 at 19:27
  • I do not. I'll go ahead and post an answer.
    – JMY1000
    Commented Jan 26, 2020 at 19:29
  • 1
    Does it need to run at login of a specific user, or at login of the first user to login, or at boot time?
    – nohillside
    Commented Jan 26, 2020 at 19:34
  • 1
    To be run as the user or to be run as root? As user is easy, just add the script as a login item. As root (or if you want to mass-deploy) it‘s more tricky, may be worth another question.
    – nohillside
    Commented Jan 26, 2020 at 21:10

1 Answer 1

1

I recommend using crontab.

Open the root user crontab (this will create a new crontab file if you don't have it):

sudo crontab -e

Add you command, with the time you want it to run in front (if you want to do it at a specific time, you can use this handy tool):

@reboot /Users/Shared/Test.sh

Exit and save the file.

That's it! Your command should now run whenever your computer starts up. While this isn't technically the same as on login, it should be enough for almost all intents and purposes.

3
  • Trying this now.
    – agjertsen
    Commented Jan 26, 2020 at 19:39
  • This works perfectly so thank you! May I ask if there is also a way to do it on login rather than boot?
    – agjertsen
    Commented Jan 26, 2020 at 20:06
  • Unfortunately not that I know of, @nohillside might know of something that I don't.
    – JMY1000
    Commented Jan 26, 2020 at 20:08

You must log in to answer this question.

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