Skip to main content
Solved

How do I delete a launch daemon using a script?


howie_isaacks
Forum|alt.badge.img+23

I have been working on a policy that will create a temporary admin account. A launch daemon gets installed and launched to handle the account deletion. Once the alotted time has elapsed, I see the account and its home folder get deleted. I see that the launchd process is no longer running. Everything works exactly as intended, but the launch daemon does not get deleted. I can manually run a command such as "sudo rm /Library/LaunchDaemons/com.my.launchdaemon.plist" and that works perfectly. The launch daemon triggers another Jamf Pro policy to run that deploys a script that runs the account deletion, deletes the home folder, unloads the launch daemon and then is supposed to delete the launch daemon. A moment ago, I commented out all the steps except the launchdaemon unload and deletion, and watched as the script deleted the launch daemon! Below are all the steps that the script runs.

#!/bin/zsh # Delete the Rescue Admin account and its home folder echo "Removing Rescue Admin account" dscl . -delete /Users/rescueadmin echo "Deleting home folder" rm -rf /Users/rescueadmin # Unload and delete the Rescue Admin launch daemon echo "Unloading launchdaemon" launchctl bootout system /Library/LaunchDaemons/com.cbre.RescueAdmin.plist echo "Deleting launchdaemon" rm -f "/Library/LaunchDaemons/com.cbre.RescueAdmin.plist"

 I don't understand why removing all the previous steps in the script allows the launch daemon to get deleted. I know there are other temporary admin account solutions but I wanted to create one myself. It's only the launch daemon deletion that won't work. How can I delete the launch daemon?

Best answer by sdagley

howie_isaacks wrote:

OK. The launch daemon does get unloaded. I see that when I run launchctl list. It just doesn't get deleted. I will modify the bootout command as you mentioned.


I should have added that because the process for the LaunchDaemon is still running the .plist for it is still "busy" and won't go away until the process is killed.

View original
Did this topic help you find an answer to your question?

4 replies

sdagley
Forum|alt.badge.img+25
  • Jamf Heroes
  • 3541 replies
  • March 21, 2025

@howie_isaacks Your bootout command is wrong.

Should be 

launchctl bootout system/com.cbre.RescueAdmin
(or whatever the label is for the LaunchDaemon)

 

 


howie_isaacks
Forum|alt.badge.img+23
  • Author
  • Esteemed Contributor
  • 773 replies
  • March 21, 2025
sdagley wrote:

@howie_isaacks Your bootout command is wrong.

Should be 

launchctl bootout system/com.cbre.RescueAdmin
(or whatever the label is for the LaunchDaemon)

 

 


OK. The launch daemon does get unloaded. I see that when I run launchctl list. It just doesn't get deleted. I will modify the bootout command as you mentioned.


sdagley
Forum|alt.badge.img+25
  • Jamf Heroes
  • 3541 replies
  • Answer
  • March 21, 2025
howie_isaacks wrote:

OK. The launch daemon does get unloaded. I see that when I run launchctl list. It just doesn't get deleted. I will modify the bootout command as you mentioned.


I should have added that because the process for the LaunchDaemon is still running the .plist for it is still "busy" and won't go away until the process is killed.


howie_isaacks
Forum|alt.badge.img+23
  • Author
  • Esteemed Contributor
  • 773 replies
  • March 21, 2025
sdagley wrote:

I should have added that because the process for the LaunchDaemon is still running the .plist for it is still "busy" and won't go away until the process is killed.


You're right! And right after I responded to you a moment ago, I realized that plist won't get deleted if it's still in use. Here's what has worked twice over the last few minutes. I removed the unload and delete steps from the script that handles the account deletion. I added those steps to files and processes in the same policy that is running the script that deletes the admin account. That worked! I set the launch daemon to launch this process after 2 minutes. When I put this into production, I will set it to 15 or 20 minutes.


Reply


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings