Login Items:
In macOS, there is a default file association between .command files and the terminal, so rename your bash script to something like login.command n.b. not "login.command.txt", then drag-and-drop the file into the login items pane: (System Preferences ➞ General ➞ Login Items)
Other file extensions such as .sh can also be used if a file association is setup in the finder so that they open with the shell and not something else like a text editor or xcode.
Automator:
If you prefer not to use a specific file extension, then this can also be done using Apple Automator.
In the Apple Finder, open Applications and double click on the Automator icon. Then select Application ➞ Utilities ➞ Run Shell Script ➞ Add the shell script to be run.
Then, save the Automator application and add it to your Login Items as above.
Create a launchd Global agent by writing an XML plist file like the example below which you can save in /Library/LaunchAgents/ e.g. /Library/LaunchAgents/com.windevcluster.startup.plist
In this example we are running the bash shell (/bin/bash) and then passing the script to be executed:
<?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> <!-- CHOOSE A UNIQUE LABEL (TASK ID) FOR THE AGENT --> <string>com.windevcluster.startup</string> <key>LaunchOnlyOnce</key> <true/> <key>ProgramArguments</key> <array> <!-- THE ITEMS TO BE EXECUTED WITH ANY ARGUMENTS --> <string>/bin/bash</string> <string>absolute_path_to_bash_script</string> </array> </dict> </plist>Set the owner of the .plist to root:
sudo chown root:wheel /Library/LaunchAgents/com.windevcluster.startup.plistMake sure the file has read and execute rights:
sudo chmod 755 /Library/LaunchAgents/com.windevcluster.startup.plistTo test this, you can then either logout and in again or manually start the agent with:
sudo launchctl load /Library/LaunchAgents/com.windevcluster.startup.plist
On Darwin operating systems, the canonical way to launch a daemon is through launchd as opposed to traditional POSIX and POSIX-like mechanisms or mechanisms provided in earlier versions of macOS. These alternate methods should be considered deprecated and not suitable for new projects.
On Darwin platforms, a user environment includes a specific Mach bootstrap subset, audit session and other characteristics not recognized by POSIX. Therefore, making the appropriate setuid(2) and setgid(2) system calls is not sufficient to completely assume the identity for a given user. Running a service as a launchd agent or a per-user XPC service is the only way to run a process with a complete identity of that user.
FILES
~/Library/LaunchAgents Per-user agents provided by the user. /Library/LaunchAgents Per-user agents provided by the administrator. /Library/LaunchDaemons System-wide daemons provided by the administrator. /System/Library/LaunchAgents Per-user agents provided by Apple. /System/Library/LaunchDaemons System-wide daemons provided by Apple.
How-to: Run a shell script
Launchctl - Load or unload daemons/agents.
macOS How To