Create executable files for Terminal scripts on Mac OS
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:
No ADS
Mac OS Tutorials
- Install Mac OS X 10.11 El Capitan in VMWare
- Install Mac OS Virtual Machine in VirtualBox
- Upgrade Mac Operating System
- Install Java on Mac OS
- Install VirtualBox on Mac OS
- How to use Windows-like shortcuts in Mac OS Virtual Machine
- Lightshot - Screenshot tool for Mac and Windows
- How do I take a MacOS Retina screenshot and get the image at its actual size?
- Microsoft Remote Desktop for Mac OS
- Transfer files between computers using Cyberduck on Mac OS
- Connect to the Server with Terminal on Mac OS
- How to use the "hosts" file?
- Are There Ways to Improve Gaming Experience on a Macbook?
- Mac Keyboard Shortcut List to Improve Efficiency
- Install FFmpeg on Mac OS
- How to open Terminal on Mac OS
- Setup environment variables on Mac Os
- Create executable files for Terminal scripts on Mac OS
Show More