Script building launch agent using variables

mucgyver
New Contributor III

Hello.

For backup purposes, I want to write out an individualized launch agent plist via a shell script to certain Macs.

This script should at first gather two variables locally (currently logged in user and a user specific storage directory, I would technically know how to get both of them), but then successfully hand over these two variables to an individualized launch agent created by the same script, doing watch paths on certain directories for running a backup script as soon as there is a change to the root directories.

How could I leverage this?

Thank you and best regards

Christian

1 REPLY 1

mschlosser
Contributor

below please find a script I use to create a launch agent or launch daemon, depending om how the script variables are customized. I also added logic to get the current logged in user, if you need that.  Hope this is helpful.

M

--------------

#!/bin/bash
 
# launchDaemon and Agent creator
# feed it with;
# launcher type =  Daemon or Agent
# launchItem = path to the app being launched, the full path to the executable
# launcherPlistName = unique bit of the plist name. it will be com.mycorp.{unique bit}.plist
# isScriptIn = is this a script yes or no. defines which plist will be used
#1) $4 launcher type = Daemon or Agent
#2) $5 launchItem = path to the app being launched, the full path to the executable or script
#3) $6 launcherPlistName = unique bit of the plist name. it will be com.mycorp.{unique bit}.plist
#4) $7 isScriptIn = is this a script yes or no. defines which plist type will be used
 
loggedInUser=$( echo "show State:/Users/ConsoleUser" | scutil | awk '/Name :/ && ! /loginwindow/ { print $3 }' )
 
launcherTypeIn="Agent"
launchItem="<commandtorunoritremtolaunchgoeshere>"
launcherPlistName="<namegoeshere>"
isScriptIn="no"
# set input strings to lower to avoid case failure
launcherType=$(echo $launcherTypeIn | tr '[:upper:]' '[:lower:]')
isScript=$(echo $isScriptIn | tr '[:upper:]' '[:lower:]')
#echo "$launchItem"
 
case "$launcherType" in
 
"daemon" )
#echo "**** Daemon ****"
launchFolder="LaunchDaemons"
;;
 
"agent" )
#echo "**** Agent ****"
launchFolder="LaunchAgents"
;;
 
*)
echo "**** no launcher type selected. Exiting *****"
exit 1
;;
esac
 
case "$isScript"  in
 
"no" )
 
cat << EOF > /Library/${launchFolder}/com.mycorp.${launcherPlistName}.plist
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>com.mycorp.${launcherPlistName}</string>
    <key>ProgramArguments</key>
    <array>
        <string>${launchItem}</string>
    </array>
    <key>RunAtLoad</key>
    <true/>
</dict>
</plist>
EOF
 
;;
 
"yes" )
 
cat << EOF > /Library/${launchFolder}/com.mycorp.${launcherPlistName}.plist
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>com.mycorp.${launcherPlistName}</string>
    <key>ProgramArguments</key>
    <array>
        <string>/bin/bash</string>
        <string>-c</string>
        <string>${launchItem}</string>
    </array>
    <key>RunAtLoad</key>
    <true/>
</dict>
</plist>
EOF
 
;;
 
*)
echo "**** no type selected. Exiting *****"
exit 1
;;
esac
 
/usr/sbin/chown root:wheel /Library/${launchFolder}/com.mycorp.${launcherPlistName}.plist
/bin/chmod 644 /Library/${launchFolder}/com.mycorp.${launcherPlistName}.plist