Photo by Christin Hume / Unsplash

Among the many languages and tools I work with as a developer and software engineer, I probably use bash the most because it helps make life easier through scripting.

We all realize that some tasks need to be done frequently when working. I have encountered a few of these common and repetitive processes in my line of work:

  • Clearing records during a database format change
  • Testing if my old APIs are hampered by my changes in code
  • A sequence of tasks I need to do to keep my local server up and tunnelled for testing
  • Deploying and managing various test server security
  • Benchmark testing
  • API calls

Almost all of the processes mentioned above often include multiple tasks. For example, when I deploy and manage various test servers on separate Virtual Private Servers, I need to install and configure the firewall, Nginx, nodejs, and npm every time. After that, I need to create users and give them the appropriate permissions to deploy the server.

I am currently managing 7 test servers, 2 staging servers, and 2 production servers. Imagine all the repetitive tasks that need to be done for them! Fortunately, bash comes to the rescue. I write bash scripts since I primarily operate on Linux. I have written a script that does the tasks mentioned above automatically. So, now I can simply copy my bash code to the new remote server and execute it there. Even that can be automated, as I have a bash script for copying my files to the remote server!

Below are the scripts that I mentioned, which I use to make my workflow more efficient.

Automatic Remote Server Setup


# this script thinks that the script is executed from the projects main directory
# this script expects the following :
# 1. a rsa-key pair is available in $HOME/.ssh/ of device executing this script, and the key will allow 'git pull' in remote server
# this script has two parameters :
# 1. ip of remote server
# 2. rsa-key pair name as mentioned above
# this script transmits setup script, deploy script and rsa-key pair to root directory of remote server
# Example code for execution
# cd projectDir
# ./scripts/ testserver

# CERT_PATH is the location of the mongodb CA certificate

scp scripts/ root@$1:/root
scp scripts/ root@$1:/root
scp scripts/ root@$1:/root
scp prod.env root@$1:/root
scp $HOME/.ssh/$2 root@$1:/root/id_rsa
scp $HOME/.ssh/$ root@$1:/root/
scp $CERT_PATH root@$1:/opt
Bash Script to Copy Local Files to Remote Server

The code shown above uses a tool called scp that copies files from one place to another. We can use it to send and fetch files.

Automatic Timezone and cron Service Restart



# set timezone
timedatectl set-timezone "$META_TIMEZONE"

# after timezone is changed, cron must be restarted
/etc/init.d/cron stop
/etc/init.d/cron start
Bash Script to Change the Timezone

The code shown above uses Linux commands to set the timezone and restart the cron service.

Why Script?

Scripting helps skip the boring parts of work and makes the experience enjoyable again. It gives us extra time to write even more scripts, templates and modules of codes, create APIs used by multiple projects, prefabs, or user-auth systems. Once tested (and when executed properly), scripts can do repeating tasks for us and free us from the mundane parts of our job.

Now, you should make a list of your own. I mostly use bash because that's what fits my work. But you can do the same thing with python, JavaScript or any other coding language you are comfortable with that can get the job done. Automation is an efficient way of spending your precious time.