mydomain
No ADS
No ADS

Create executable files for Terminal scripts on Mac OS

  1. Create executable file on Mac
  2. Run the file
Sometimes in Mac OS you need to execute a script on Terminal, if that job needs to be done frequently it will be very boring. Why don't you put all those commands in one file and execute it with just one click?
Here are a few boring tasks I have to do quite often:
Backup a Postgres database:
#!/bin/bash

# Create variable today 'YYYY-MM-dd'
today=$(date '+%Y-%m-%d')

# PostgreSQL database binary path:
cd /Library/PostgreSQL/15/bin

# Dump Database to a File
./pg_dump --file "/Volumes/Data/BACKUP/mydatabase_$today.backup" --host "localhost" --port "5432" --username "postgres" --verbose --role "postgres" --format=c --blobs --encoding "UTF8" "mydatabase"
Run a Java Spring application from Terminal:
#!/bin/bash

# Set _JAVA_OPTION variable
export _JAVA_OPTION=-Xmx1024M

# Run Java Spring App
java -jar /Volumes/Dev/MySpringApp-1.0.war -port=8080

1. Create executable file on Mac

First, you need to create a file with the extension "command" or "sh". Such as:
  • my_executable_file.sh
The contents of the file are the Terminal commands you want to execute. Such as:
my_executable_file.sh
#!/bin/bash

# Set _JAVA_OPTION variable
export _JAVA_OPTION=-Xmx1024M

# Run Java Spring App
java -jar /Volumes/Dev/MySpringApp-1.0.war -port=8080
Note that the first line of the file needs to be:
#!/bin/bash
No ADS
Next, on Terminal, execute the command below to make your file executable.
chmod u+x my_executable_file.sh
Next, select the default application to execute this file when you double-click on it. Right-click on your file and select:
  • Open With > Other...
Select Terminal application:

2. Run the file

There are two ways to run the file you created above. The first way is to just double click on it. The second way is to execute the file through Terminal:
./my_executable_file.sh
No ADS
No ADS