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?