Keep launchd active when switching users

I'm just starting out reading into launchd and its possibilities.


I am wondering, is it possible to have launchd.plist remaining active on a non-active, but logged in, user?


I have created two users on my mac. Both are logged in.


I have a launchd setup for one of them, with a WatchPath on /Volumes, so that it will perform a task as soon as a certain external drive is plugged in.


This works perfectly as long as that user is active. If I switched over to the other logged in user and then plugin the drive, it does not.


Is there anyway to have it work, regardless of which user is in control?





MacBook

Posted on Dec 23, 2018 10:05 AM

Reply
5 replies

Dec 24, 2018 1:05 AM in response to BDAqua

I've been an idiot!


There was a mistake in the AppleScript, which caused it to work only once. I figured it out once I realised out that it wasn't working anymore while UserA was logged in. Removed the script, put it back, and it worked again, but only once.


Fixed the mistake, and now it works no matter which user is active.


Thanx for the help though!

Dec 23, 2018 10:54 AM in response to BDAqua

Yes, I put it in the Library of the specific user.


I have two users on my mac: UserA and UserB


I put the following .plist file in /Users/UserA/Library/LaunchAgents/ :


<?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>WatchingVolumesFolder123</string>

<key>LowPriorityIO</key>

<true/>

<key>Program</key>

<string>/usr/bin/osascript</string>

<key>ProgramArguments</key>

<array>

<string>osascript</string>

<string>/Users/UserA/Library/Scripts/launchd/WatchingVolumesFolder123.scpt</string>

</array>

<key>ServiceDescription</key>

<string>AppleScript directly when Volumes changes</string>

<key>WatchPaths</key>

<array>

<string>/Volumes</string>

</array>

</dict>

</plist>


I created an AppleScript with the name and path of the ProgramArguments.


I start the computer without the drive connected. I log in to UserA, I then switch and login to UserB, without logging out UserA.


If I plug in the external drive while UserB is active, the launchd job in the Library of UserA does nothing.


It only does what it is supposed to do if I plug in the drive while UserA is active.


I'm wondering if there is anyway to get it to work on UserA, without ever switching back to UserA, so just have it perform in the background.



If it is not possible, I will just set it up, so I put the launchd on UserB, and kick of the task by login in to UserA with shellscript and ssh user@localhost, but I'd prefer not to.



This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Keep launchd active when switching users

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.