How can you start a LaunchAgent for the first time without rebooting, when your code runs as a LaunchDaemon? - macos

I have a LaunchDaemon. When it runs, it checks if SIMBL is installed. If SIMBL is not installed, it uses NSTask to run /usr/sbin/installer on the SIMBL.pkg.
SIMBL's postflight script then tries to run a launchctl load command to start SIMBL's LaunchAgent immediately:
sudo -u "$USER" -- /bin/launchctl load -F -S Aqua -D user "${LAUNCHD_PLIST}"
This fails, because my LaunchDaemon's NSTask environment doesn't have $USER set.
If I have my daemon detect the current user with the System Configuration framework and pass it to NSTask with setEnvironment, launchctl bugs out on me:
Bug: launchctl.c:2325 (23930):13: (dbfd = open(g_job_overrides_db_path, O_RDONLY | O_EXLOCK | O_CREAT, S_IRUSR | S_IWUSR)) != -1
I realize a daemon, by definition, should not operate in a user session. By the same token, Apple seems to recommend LaunchAgents as helper objects for LaunchDaemons, to do that user session work. Is there any way to get such an agent up and running immediately?
I have all the .plists in the right places (they start running after a reboot, the next time launchctl does its regular loading) so my first thought was to just tell launchctl to reload. But all the code to do that is commented out in launchctl.c:
// { "reload", reload_cmd, "Reload configuration files and/or directories" },
...
* In later versions of launchd, I hope to load everything in the first pass,
* then do the Bonjour magic on the jobs that need it, and reload them, but for now,
* I haven't thought through the various complexities of reloading jobs, and therefore
* launchd doesn't have reload support right now.

Oh how launchd drives me crazy....
To cut to the chase, after much study and experimentation, this is how I do it on 10.5+:
# If possible, tell launchd to start the LaunchAgent. This will fail on 10.4.
# $F[0] is the pid
# $F[1] is the username
# $F[2] is the first word of the command
ps -ww -A -opid,user,command | \
perl -nae 'if($F[2] =~ /\bloginwindow\b/) { system(
qq(launchctl bsexec $F[0] su $F[1] -c "launchctl load -w <your_plist>"))
}'
I have found no way to achieve this directly on 10.4. I cheat on 10.4 and just run the thing the LaunchAgent would have run, even though it has a GUI and you're not supposed to be able to do that (you can anyway in 10.4-10.6; you can't in 10.7). On 10.4, the LaunchAgent works correct after the next reboot.
The above code looks for loginwindow processes and uses bsexec to run commands in those contexts. Keep in mind that with Fast User Switching, there can be multiple contexts.
Some useful links:
Daemons and Services Programming Guide. You have to read it, but it won't actually answer any of the hard questions. But it will at least give you hints at where everything is located.
TN2083. This is a maddening document that raises as many questions as it answers, but is gospel and mandatory reading for anyone entering the abyss of launchd.
Starting/stopping a launchd agent for all users with GUI sessions. This has several other useful links and explanation.
IMO, launchd is one of the worst "great ideas" Apple has ever deployed. The idea is very useful, but the API is horrible.

Related

How to properly start a Gnome-Shell extension via command line?

After copying myexentension#me.com folder to .../gnome-shell/extensions/ I'm executing this command on the terminal:
gnome-shell-extension-tool -e myexentension#me.com
Then, I restart my session with Alt + F2 and execute r, and everything works fine.
But can I start my extension only through the command line? Without Alt+F2+r? Without restarting my gnome-shell session?
According to some answers around the internet, sending SIGHUP to the gnome-shell process restarts it (i. e. killall -HUP gnome-shell), but I haven’t been able to find a clear source on this and couldn’t find the signal handling in the code. What I do know is that this should be exactly equivalent to Alt+F2 r:
busctl --user call org.gnome.Shell /org/gnome/Shell org.gnome.Shell Eval s 'Meta.restart("Restarting…")'
Because apart from a gettext call on the message, this is exactly what Alt+F2 r is bound to (see runDialog.js – search for _restart).
January 2022 update: Since Gnome 41, calling Eval is restricted and requires “unsafe mode” to be enabled, so by default this will no longer work. I’m not currently aware of a replacement for this particular usage.
Personally, I prefer a solid Alt+F2, r+Enter but maybe try disabling and enabling:
gnome-shell-extension-tool -d myexentension#me.com && gnome-shell-extension-tool -e myexentension#me.com
or
gnome-shell-extension-tool -r myexentension#me.com
Which may do the same thing. There's also gnome-shell-extension-prefs which you can use to do the same thing (and is typically hidden in Gnome for some reason).

Close "<app> quit unexpectedly" window from terminal/bash

Is there a way to close/kill " < app> quit unexpectedly" window from terminal or bash script? What's the process name?
(AppleScript automation solutions are not acceptible.)
You can:
killall UserNotificationCenter
It will kill the UserNotificationCenter (an ALL it's opened windows too), so the message disappears. (Don't worry, the next error message will start is again automatically.)
But, (IMHO) it is better to use the osascript command in a form:
osascript -l JavaScript <<EOS
... apple-scripting using JavaScript ...
EOS
IMHO JavaScript is much easier to understand (for an common programmer) as the "standard" applescript.
You can disable it appearing in the first place by:
defaults write com.apple.CrashReporter DialogType none
Other possible values are developer† (show stack traces for all processes) and crashreport (the default).
This also means that no entries will be written to the Console.app though. The dialog itself is shown by the UserNotificationCenter and can be disabled (along with many other notifications) by:
sudo launchctl unload -w /System/Library/LaunchDaemons/com.apple.UserNotificationCenter.plist
Some context:
Mach has a concept of exception ports. Each thread/process has a task, process and a host exception port, which are checked when an exception occurs. The CrashReporter daemon registers the host exception port and gets activated when no other signal handler ran. It then creates a stack trace and memory map of the process and instructs the UserNotificationCenter to show it. By default it only does so for GUI applications.
On High Sierra, I had to use defaults write com.apple.CrashReporter -string "developer"
I am not sure if apple hast the same core utilities, but I come from the unix world too.
for example: the solution would be to find the process id via name. on my linux system I can use the following to find a process id...
ps -aux
An other variation would be top. both give a ton of information and I have to filter the code with grep.
After that I would filter the string via cut or sed.
last but not least the kill command.
the script should look some like this ...
#!/bin/sh
PNAME="< app> quit unexpectedly"
ps -aux | grep "$PNAME" | cut -d" " -f2 | kill
but be warned, this script can make huge damage if u dont know how to use it.
To be hornest I would never use some like this, instead would execute kill manually..

stopping LaunchAgent when uninstalling package

I build an app which provides LaunchAgent (an small UI app with "tray" icon; let's say that defined in /Library/LaunchAgents/foo.plist.
When installing package launchctl loads this LaunchAgent description automatically and starts processes for all logged-in users. (i.e i don't have to load it using launchctl load like i have to do with LaunchDaemons).
When i uninstall package, all files - including LaunchAgent plist file in /Library/LaunchAgents are removed but processes are still running.
(i have several users logged in, so there are several instances of this agent)
Now, how should i tell root launchd to stop these processes (for unspecified number of users) when running as root ? (note, launchctl ran as root doesn't even see those agents).
(pid-files,killall more-or-less-unique-executable-name is last resort and i'm keeping these solution as last resort)
Try this:
LOGGEDUSERS=`who | grep console | awk '{ print $1 }'`
for CURRUSER in $LOGGEDUSERS
do
su -l $CURRUSER -c 'launchctl unload /Library/LaunchAgents/your.plist'
done
In my uninstaller script there is also killall -15 <app_name> after that code (just in case)

Mac OS X: Refreshing service menus works in Terminal but not in post-install-script

I have an OSX application providing a service menu to other applications. This works fine so far, the menu is activated and available after a user installed the app and logged out and back in. (see similiar post)
I know the log-out-and-back-in is obsolete when triggering the service menu agent (pbs) to scan for changed services by opening a terminal and running
/System/Library/CoreServices/pbs
(this also works without having the new application launched even once)
So I´d like to provide an installer (.pkg) which runs "pbs" as post-install script. Suprisingly, running "bps" as post-install only works if the application was launched before e.g.:
#! /bin/sh
sleep 3
open /Applications/MyApp.app
sleep 3
/System/Library/CoreServices/pbs
exit 0
Unfortunately, (due to application specific reasons) I do not want to start my application directly from the installer. Does anyone know why the post-install-scrips behaves different than the Terminal?
Depending on the rights the Installer requires the post-install scripts maybe runs as root user. Try:
sleep 3
su -l "${USER}" -c "open /Applications/MyApp.app"
sleep 3
su -l "${USER}" -c "/System/Library/CoreServices/pbs"
exit 0
Eventually, as a workarround, I am going to start my application hidden and stopping it again before running pbs. Still looking forward for a better solution.

How to examine processes in OS X's Terminal?

I’d like to view information for processes running in OS X. Running ps in the terminal just lists the open Terminal windows. How can I see all processes that are running?
Say I’m running a web browser, terminal and text editor. I’d like to see information for the text editor and web browser.
Running ps -e does the trick. Found the answer here.
You can just use top
It will display everything running on your OSX
Using top and ps is okay, but I find that using htop is far better & clearer than the standard tools Mac OS X uses. My fave use is to hit the T key while it is running to view processes in tree view (see screenshot). Shows you what processes are co-dependent on other processes.
You can install it from Homebrew using:
brew install htop
And if you have Xcode and related tools such as git installed on your system and you want to install the latest development code from the official source repository—just follow these steps.
First clone the source code from the htop GitHub repository:
git clone git#github.com:hishamhm/htop.git
Now go into the repository directory:
cd htop
Run autogen.sh:
./autogen.sh
Run this configure command:
./configure
Once the configure process completes, run make:
make
Finally install it by running sudo make install:
sudo make install
Try ps -ef. man ps will give you all the options.
-A Display information about other users' processes, including those without controlling terminals.
-e Identical to -A.
-f Display the uid, pid, parent pid, recent CPU usage, process start time, controlling tty, elapsed CPU usage, and the associated command. If the -u option is also used, display
the user name rather then the numeric uid. When -o or -O is used to add to the display following -f, the command field is not truncated as severely as it is in other formats.
Try the top command. It's an interactive command that will display the running processes.
You may also use the Apple's "Activity Monitor" application (located in /Applications/Utilities/).
It provides an actually quite nice GUI. You can see all the running processes, filter them by users, get extended informations about them (CPU, memory, network, etc), monitor them, etc...
Probably your best choice, unless you want to stick with the terminal (in such a case, read the top or ps manual, as those commands have a bunch of options).
To sort by cpu usage: top -o cpu
if you are using ps, you can check the manual
man ps
there is a list of keywords allowing you to build what you need. for example to show, userid / processid / percent cpu / percent memory / work queue / command :
ps -e -o "uid pid pcpu pmem wq comm"
-e is similar to -A (all inclusive; your processes and others), and -o is to force a format.
if you are looking for a specific uid, you can chain it using awk or grep such as :
ps -e -o "uid pid pcpu pmem wq comm" | grep 501
this should (almost) show only for userid 501. try it.
The slightly GUI way
if you are a cli (ui) fan. I recommend trying https://github.com/clementtsang/bottom which shows not only processes, but also temperature, disk usage and network. Screenshot is running from kitty (terminal) as an example, I use it on OSX default terminal and the color shows up a bit different, but still amazing.
The tree way
As described here : https://en.wikipedia.org/wiki/Pstree will give a better connection on the hierarchy of the processes
brew install pstree # if you need to install it
pstree
pstree -u <user> # show only processes by your user
pstree -s <string> # show only processes with string
pstree -help # show help

Resources