How to run scripts on Mac?
A script is a programming language used to automate the execution of certain routine or time-consuming tasks. It is a single file that contains a set of commands or instructions to streamline various processes for Mac management. With scripts, the admin can automate processes to perform specific operations, which otherwise has to be executed one-by-one on Apple’s Terminal.
Hexnode UEM allows IT admins to execute custom scripts on Mac devices remotely. The admin can run any scripts to perform system-level configurations on Mac devices without any user interaction. You can shut down/restart devices, install/uninstall apps, push updates, set up app configurations, and so forth. You can enhance Mac management to the next level by configuring extra settings that may not be natively available in MDM’s features stack.
Create and run a Mac script
Here, we will show you how to write a sample bash script that allows you to restart your device.
- Open Text Edit from Applications.
- Click New Document and write your script in the text area as below:
Script to reboot the Mac device12#!/bin/sh/sbin/shutdown -r now
- Now, click Format > Make Plain Text to convert the file into a plain text.
- Save the file by clicking on File > Save. Make sure to uncheck the option If no extension is provided, use ‘.txt’ while saving the file. Also, note the file name and location of the file.
- Next, navigate to the folder where you have saved the file. Right-click on the file and click Get Info. Unlock the bottom padlock present on the opened info window.
- Open Terminal and type the following:
Script to change current working directory1cd (location of the script)
For example, if you have saved the file in Documents, type as follows:Sample script to change current working directory1cd Documents
- Next, convert the file into an executable file. Type in
Script to convert file into an executable file1chmod 700 (filename)
If the file name is SampleScript, type as follows:Sample script to convert file into an executable file1chmod 700 SampleScript
- Next, type in ./(filename) to restart your device. In this case, type as follows:
Script to run the created file1./SampleScript
Your device will restart once the script is executed.
Run scripts on Mac using Hexnode UEM
To execute a custom script on Mac,
- From your Hexnode portal, navigate to Manage > Devices.
- Click on the Mac device you need to deploy scripts.
- Click Actions > Execute Custom Script.
- Specify the below parameters to execute the script on that device:
Choose script file source You can choose the script file either by uploading it or selecting it from the Hexnode repository if the file is already added to Content > My Files.
File name The file name will be auto-populated based on the uploaded script or the script chosen from the Hexnode repository. Binary path The binary path gets auto-filled based on the type of added script. The binary paths for various types of scripts are listed below:
C Shell: /bin/csh
Korn Shell: /bin/ksh
Hypertext Preprocessor: /usr/bin/php
Arguments Specify the arguments, if needed, while executing the script.
- You may use any number of arguments for executing the scripts, each of which must be separated by a space. For example, specifying the following string Alexander Sam Richard in the argument field takes in three arguments Alexander, Sam, and Richard, respectively.
- Hexnode allows the usage of single quotes while providing the arguments. It helps you include multiple words for a single argument. For instance, the following string, ‘William Alexander’ ‘Sam Anderson’, specifies two arguments containing multiple words.
- For displaying a normal single quote within an argument, you should add two single quotes instead of one. Quoting an example, specifying the string William’’s Arcade takes in a single argument of the form of William’s Arcade while executing the script.
Consider the case of a shell script (say, file_search.sh) used to search a file (Samplefile.txt) in the Desktop folder. The script is shown below:
This script requires two arguments which are indicated by $1 and $2 respectively. To run this script, we provide the arguments as:
It also supports the use of the following wildcards:
For example, specifying the string %name% %email% in the arguments field retrieves the name and email address of the user as the two arguments.
- Click the Execute button to confirm your action.
- Now, navigate to the Action History sub-tab to view the execution status of the script. You can see the output details by clicking on the Show Output button corresponding to the execution status of that script.
The script, if coded correctly, will be executed successfully to automate the specified operations on the device.