Skip to main content
Solved

Running a script as a login user

  • January 28, 2016
  • 33 replies
  • 249 views

Show first post

33 replies

Forum|alt.badge.img+5
  • Contributor
  • January 3, 2019

@cdenesha Could you please elaborate?


cdenesha
Forum|alt.badge.img+14
  • Honored Contributor
  • January 3, 2019

@Buraq I was wrong about the space, per the next post in the thread. :)


Forum|alt.badge.img+5
  • Contributor
  • January 3, 2019

UPDATE:
I managed to load the Agent. The problem was with wrong extension. Now if I launchctl load -w MyAgent nothing happens, and when I run the same command again I get this: service already loaded.
Both times the actual script is not called.
This is how I'm calling it in the launch agent:

<key>ProgramArguments</key> <array> <string>sh</string> <string>-c</string> <string>/Users/buraqalsmadi/Library/LaunchAgents/DataScience/Datascience_High_Sierra.sh</string> </array> <key>RunAtLoad</key> <true/>


mm2270
Forum|alt.badge.img+24
  • Legendary Contributor
  • January 3, 2019

@Buraq I'm not sure if it's necessary to put the sh -c in front of your script path. If you make sure the script has the executable bit set (run chmod +x /Users/buraqalsmadi/Library/LaunchAgents/DataScience/Datascience_High_Sierra.sh against it) then simply putting in the path to the script should run it when the agent is loaded. You usually only need to put in things like sh if you want to use a different interpreter than specified in the script or if it's not set as executable. I would try removing the <string>sh</string> <string>-c</string> section and trying again.

But one other thing. If launchctl is saying the plist is already loaded, you'll want to make sure to unload it first before trying to load it again. Just use launchctl unload on it.


Forum|alt.badge.img+13
  • Honored Contributor
  • January 3, 2019

if you see yourself doing more of this in the future, you may want to check out outset

  1. Install outset
  2. Place script in applicable folder
  3. Create .pkg with composer for mass deploy
  4. Beer/Pub

Forum|alt.badge.img+5
  • Contributor
  • January 4, 2019

@mm2270 I have removed <string>sh</string> <string>-c</string> and tried again, no luck!
The script is executable.
It seems what matter I do, and even when the launch agent loads, it doesn't call the script


Forum|alt.badge.img+10
  • Valued Contributor
  • April 26, 2019

I needed the logged in user to run something similar from Self Service; thanks to tips here ended up using something like this in a script:

sudo -u $3 -i /path/to/binary --parameter $3 --destination /Users/$3/Desktop/$3 --verbose

whiteb
Forum|alt.badge.img+9
  • Valued Contributor
  • February 2, 2022

@swaroopmj You need the -l flag for your su command, and the pound sign for your shebang line. Try this:

#!/bin/bash
currentuser=$(/bin/ls -la /dev/console | /usr/bin/cut -d ' ' -f 4)
su -l $currentuser -c "defaults read /Users/$currentuser/Library/Preferences/com.apple.dock | grep corner"

Then chmod/chown as needed.


Fairly old thread but after trying numerous ways to do this, yours ended up working for me.

For hiding the bluetooth icon in the top bar:

defaults write ~/Library/Preferences/ByHost/com.apple.controlcenter.plist Bluetooth -int 24

Above doesn't work as sudo or admin, only works as logged in user.

#!/bin/bash currentuser=$(/bin/ls -la /dev/console | /usr/bin/cut -d ' ' -f 4) su -l $currentuser -c "defaults write ~/Library/Preferences/ByHost/com.apple.controlcenter.plist Bluetooth -int 24"

The above is working in 12.2 Monterey.