read

On my Digital Ocean VPS instances, I can have up for 5 different services running that I need to configure over time. Unfortunately, I only need to configure them every couple months, so I often forget the name, location, or command parameters needed.

So I decided to create some bash functions and aliases to help me out.

# My help menu to view available aliases and functions
help () {  
    echo "Available services:"
    echo "   hubot, gunicorn, nginx, ghost"
    echo "===================================================================="
    echo "services                 Show service management menu"
    echo "syse                     Edit service config"
    echo "syss                     Start service"
    echo "sysr                     Restart service"
    echo "viewlog [service]        hubot, gunicorn, nginx, ghost"
    echo "ngedit                   Choose an nginx config file to edit"
}

# View the journalctl entries for a service
alias viewlog="sudo journalctl -f -u "

# Edit a service's config file
syse () {  
  sudo vim /lib/systemd/system/"$1".service
}

# Restart a system service
sysr () {  
  sudo systemctl restart "$1".service
}

# Start a system service
syss () {  
  sudo systemctl start "$1".service
}

# Choose an nginx configuration file to edit 
ngedit () {  
  echo "1. stevebrownlee.com"
  echo "2. api.stevebrownlee.com"
  echo -n "> "
  read choice
  case "$choice" in
        1)
            sudo vim /etc/nginx/sites-available/default
            ;;
        2)
            sudo vim /etc/nginx/sites-available/api
            ;;
  esac
}

# Menu system for picking a service and an action to perform on it
# Useful when prolonged periods of time elapse between usage, and 
# I've completely forgot all commands
services () {  
  echo "1. ghost"
  echo "2. nginx"
  echo "3. hubot"
  echo "4. gunicorn"
  echo -n "> "
  read choice
  case "$choice" in
        1)
            service=ghost
            ;;
        2)
            service=nginx
            ;;
        3)
            service=hubot
            ;;
        4)
            service=gunicorn
            ;;
  esac

  echo "1. Restart"
  echo "2. Stop"
  echo "3. Start"
  echo "4. Status"
  echo "5. Edit"
  echo "6. View logs"
  echo -n "> "
  read action

  case "$action" in
        1)
            sudo systemctl restart "$service".service
            ;;
        2)
            sudo systemctl stop "$service".service
            ;;
        3)
            sudo systemctl start "$service".service
            ;;
        4)
            sudo systemctl status "$service".service
            ;;
        5)
            sudo vim /lib/systemd/system/"$service".service
            ;;
        6)
            sudo journalctl -f -u $service
            ;;
  esac
}

# Alias to back up my .zshrc file
bzc () {  
  cwd=$(pwd)
  cd ~/.zsh
  git pull origin master
  cp ~/.zshrc ~/.zshrc.stevebrownlee.com
  mv ~/.zshrc.stevebrownlee.com .
  git add .
  git commit -m "Updated config file"
  git push origin master
  cd "$cwd"
}
Blog Logo

Steve Brownlee

Head Coach at Nashville Software School. Evolving software development education.


Published