The ultimate guide to Mac shell scripting
Learn how to create and run a macOS shell script, and review the common shell commands.
Get fresh insights, pro tips, and thought starters–only the best of posts for you.
Aiden Ramirez
May 8, 2023
12 min read
“An error occurred while installing the application.”
“The application can’t be opened.”
These words are one of the worst nightmares of all IT admins. Enough to drive someone into a state of confusion, thinking about what they missed. In addition, every app will have its own set of requirements to run correctly. Making sure all these prerequisites are satisfied fulfilled in all devices can be a daunting task. Being an IT admin is not easy😪. ‘Pat pat’ to you folks.
Alright, let’s say you managed to install the application. Is the job done? I’m afraid not. You might have to ensure all the necessary features are turned on before launching the app. Seems like too much work? Getting an application to work correctly shouldn’t be this hard, right? So, what is the solution?
(Read in Al Pacino’s voice) Say hello to my little friend- pre and post app install scripts.
Since my “catchy” intro is over, let’s look at what precisely are these pre and post app installation scripts and why we might need them.
Pre/post app installation scripts is a new feature for macOS devices, available now in the Hexnode portal. This feature allows you to customize your enterprise app installation process. For example, you can add custom-made scripts as pre-install scripts and post-install scripts while pushing an application management policy into the devices. Pre-install scripts run before the app installation, and post-install scripts run once the installation process is complete.
Pre-install scripts perform an action before installation occurs. For example, IT admins can use it to set up all the necessary prerequisites before executing the dmg/pkg file that installs the application. If the pre-install script fails to execute, it will cease the installation process and send an error message to the server.
Before installing your applications, you might have to perform cleanup tasks such as clearing all the cached data. Let’s see how you can automate this process by executing a bash script for cleaning cache data as a pre-install script.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
#!/bin/bash USER=`stat -f%Su /dev/console` #System Caches sudo mv /private/var/log/privoxy /private/var/privoxy > /dev/null 2>&1 sudo /bin/rm -rf /private/var/log/* > /dev/null 2>&1 sudo mv /private/var/privoxy /private/var/log/privoxy > /dev/null 2>&1 #System Caches sudo /bin/rm -rf /Users/$USER/Library/Logs/* > /dev/null 2>&1 & sudo /bin/rm -rf /Library/Logs/DiagnosticReports/*.* > /dev/null 2>&1 & sudo /bin/rm -rf /private/var/tmp/com.apple.messages > /dev/null 2>&1 & sudo /bin/rm -rf /Users/$USER/Library/Caches/* > /dev/null 2>&1 & sudo /bin/rm -rf /private/var/db/diagnostics/*/* > /dev/null 2>&1 & sudo /bin/rm -rf /Library/Logs/DiagnosticReports/ProxiedDevice-Bridge/*.ips > /dev/null 2>&1 & sudo /bin/rm -rf /Users/$USER/Library/Application\ Support/CrashReporter/* > /dev/null 2>&1 & sudo /bin/rm -rf /private/tmp/gzexe* > /dev/null 2>&1 #Safari Caches sudo /bin/rm -rf /Users/$USER/Library/Containers/com.apple.Safari/Data/Library/Caches/* > /dev/null 2>&1 & sudo /bin/rm -rf /private/var/folders/ry/*/*/com.apple.Safari/com.apple.Safari/com.apple.metal/*/libraries.data > /dev/null 2>&1 & sudo /bin/rm -rf /private/var/folders/ry/*/*/com.apple.Safari/com.apple.Safari/com.apple.metal/*/libraries.maps > /dev/null 2>&1 & sudo /bin/rm -rf /Users/$USER/Library/Containers/io.te0.WebView/Data/Library/Caches/WebKit > /dev/null 2>&1 & sudo /bin/rm -rf /Users/$USER/Library/Safari/History.db* > /dev/null 2>&1 & sudo /bin/rm -rf /Users/$USER/Library/Safari/RecentlyClosedTabs.plist > /dev/null 2>&1 & sudo /bin/rm -rf /Users/$USER/Library/Safari/CloudHistoryRemoteConfiguration.plist > /dev/null 2>&1 #Chrome Caches ChromePath="/Applications/Google Chrome.app" if [[ -d $ChromePath ]]; then sudo /bin/rm -rf /Users/$USER/Library/Application\ Support/Google/Chrome/*/GPUCache/* > /dev/null 2>&1 & sudo /bin/rm -rf /Users/$USER/Library/Application\ Support/Google/Chrome/*/Storage/ext/*/def/GPUCache/* > /dev/null 2>&1 & sudo /bin/rm -rf /Users/$USER/Library/Application\ Support/Google/Chrome/*/*-journal > /dev/null 2>&1 & sudo /bin/rm -rf /Users/$USER/Library/Application\ Support/Google/Chrome/*/databases/*-journal > /dev/null 2>&1 & sudo /bin/rm -rf /Users/$USER/Library/Application\ Support/Google/Chrome/*/Visited\ Links > /dev/null 2>&1 & sudo /bin/rm -rf /Users/$USER/Library/Application\ Support/Google/Chrome/*/Top\ Sites > /dev/null 2>&1 & sudo /bin/rm -rf /Users/$USER/Library/Application\ Support/Google/Chrome/*/History\ Provider\ Cache > /dev/null 2>&1 & sudo /bin/rm -rf /Users/$USER/Library/Application\ Support/Google/Chrome/*/Current\ Tabs > /dev/null 2>&1 & sudo /bin/rm -rf /Users/$USER/Library/Application\ Support/Google/Chrome/*/Network\ Action\ Predictor > /dev/null 2>&1 & sudo /bin/rm -rf /Users/$USER/Library/Application\ Support/Google/Chrome/*/*.ldb > /dev/null 2>&1 & sudo /bin/rm -rf /Users/$USER/Library/Application\ Support/Google/Chrome/*/*.log > /dev/null 2>&1 & sudo /bin/rm -rf /Users/$USER/Library/Application\ Support/Google/Chrome/*/Extension\ State/* > /dev/null 2>&1 & sudo /bin/rm -rf /Users/$USER/Library/Application\ Support/Google/Chrome/*/Session\ Storage/* > /dev/null 2>&1 & sudo /bin/rm -rf /Users/$USER/Library/Application\ Support/Google/Chrome/*/Current\ Session > /dev/null 2>&1 & sudo /bin/rm -rf /Users/$USER/Library/Application\ Support/Google/Chrome/*/Storage/ext/* > /dev/null 2>&1 & sudo /bin/rm -rf /Users/$USER/Library/Application\ Support/Google/Chrome/*/*/Cache > /dev/null 2>&1 fi #Clean Download History sudo sqlite3 ~/Library/Preferences/com.apple.LaunchServices.QuarantineEventsV* 'delete from LSQuarantineEvent' > /dev/null 2>&1 #Clean Terminal History sudo /bin/rm -rf /Users/$USER/.bash_sessions/* > /dev/null 2>&1 & sudo /bin/rm -rf /Users/$USER/.bash_history > /dev/null 2>&1 & sudo /bin/rm -rf /Users/$USER/.zsh_sessions/* > /dev/null 2>&1 & sudo /bin/rm -rf /Users/$USER/.zsh_history > /dev/null 2>&1 #Applications Caches for x in $(ls ~/Library/Containers/) do echo "Cleaning ~/Library/Containers/$x/Data/Library/Caches/" rm -rf ~/Library/Containers/$x/Data/Library/Caches/* done echo done |
When added as the pre-install script, this bash script clears the system cache, browser cache, user cache, download history, and terminal history before app installation.
You can also use pre-install scripts to do other cleanup tasks, such as removing licensing and clearing database files. Apart from this, you can use pre-install scripts to run configuration files to configure applications automatically during installation. When installation occurs, the installer finds the configuration files already placed to facilitate this process.
The Post-install script runs after the execution of the dmg/pkg/mpkg file. You can use it for applying configurations after the software completes the installation. Tasks that would typically be prompted for the user to resolve before running the app can be automated by using these scripts.
Consider the situation where the user has to launch the app you just installed every time they turn on their devices. Having these apps automatically opened as soon as the user logs in will be convenient. Again, you can deploy a script for this as the post-install script to your device fleet.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#!/bin/bash touch '/Library/LaunchAgents/com.app_name.plist' cat > /Library/LaunchAgents/com.app_name.plist <<EOF <?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>KeepAlive</key> <false/> <key>Label</key> <string>com.app_name.plist</string> <key>Program</key> <string>/System/Applications/app_name.app/Contents/MacOS/app_name</string> <key>RunAtLoad</key> <true/> </dict> </plist> |
Replace app_name in the script with the name of the application you require to launch automatically and specify the full path of the executable file for the application under the key. This script will create a launch agent for the specified app in /Library/LaunchAgents and run for every user account upon logging in.
You can also set up custom app configurations, Wi-Fi settings, VPN settings, and so on by post-install scripting.
An audit script checks the installation status of the enterprise app. The script can check for required files, settings, apps and more to find all possible errors. The audit script would check for the presence of an app at the specified location. If it is not present, the script will prompt Hexnode to reinstall it.
Configuring pre-install, post-install and audit scripts using Hexnode won’t take much time if you have your apps and scripts in hand. Feel free to check out Hexnode’s sample script repository if you need help creating your scripts.
To configure your app installation,
Once you upload the files and enter the necessary details, click Save and add the policy to your target devices. This action will initiate the execution of the pre-install script, followed by app installation. You can check the installation status from the Action history of your device under the Manage section.
The implementation of the app install happens in 4 stages after configuring all the scripts and arguments from the portal. One stage starts after the completion of the ongoing stage. If any stages fail, the action stops and acknowledges the server with error codes and output, if available.
Let’s see the steps involved in this process:
Get started with Hexnode’s Mac Management solution to save your time and the associated IT operational costs of managing your Mac devices.
DOWNLOAD DATASHEETEnterprise applications play a significant role in improving workflows and increasing employee productivity across almost all organizations. However, it’s crucial to ensure that you cover all app requirement checkboxes to ensure its proper functioning. Scripts can be a powerful weapon in your arsenal if you want to automate mundane tasks. In addition, pre/post app install scripts further simplify the process as it is incorporated within the app installation process.
With Hexnode, you get much more than being able to customize enterprise app installation. Hexnode lets you have greater control over your enterprise applications through policies such as blacklisting, whitelisting and having customized app catalogs. It also offers a wide range of device management features that gives you complete control over your organization’s fleet of devices. Rather than me explaining all of this, it’s better that you realize it yourself. So go ahead and sign up for the free trial to experience Hexnode’s capabilities.
Start your 14-day free trial to get a first hand experience on Hexnode's Mac management capabilities.
SIGN UP NOWThe Sample Scripts provided here are adapted from third-party Open-Source sites. We recommend you to manually validate the script execution on a system before executing the action in bulk.
Share your thoughts