Simplifying Cloudflared Service Management with Bash Script and Alias

Managing services on a Linux system can sometimes be cumbersome, especially when it involves multiple steps such as stopping, starting, enabling, or disabling a service. In this blog post, we'll explore how to simplify the management of the Cloudflared service using a Bash script and an alias.

Overview of Cloudflared:

Cloudflared is a command-line tool provided by Cloudflare that tunnels traffic from your server to Cloudflare's network. It's commonly used for securely exposing local web servers to the internet, among other tasks.

The Challenge:

Managing the Cloudflared service typically involves executing multiple systemctl commands, which can be repetitive and error-prone.

Solution:

We can create a Bash script to encapsulate the necessary systemctl commands for managing the Cloudflared service. Additionally, we'll set up an alias to invoke this script with a simpler command.

Step-by-Step Implementation:

Create the Bash Script:

#!/bin/bash


case "$1" in

    "stop")

        systemctl stop cloudflared

        systemctl disable cloudflared

        ;;

    "start")

        systemctl start cloudflared

        systemctl enable cloudflared

        ;;

    *)

        echo "Usage: $0 {start|stop}"

        exit 1

        ;;

esac


exit 0


Make the Script Executable:

chmod +x cloudflared_service.sh


Create an Alias:


alias cloudflared_service='/path/to/cloudflared_service.sh'

Reload the Shell:

source ~/.bashrc

Usage:

Now that we have set up the script and alias, using it is straightforward:

cloudflared_service stop

cloudflared_service start


Conclusion:

By creating a Bash script and an alias, we have simplified the management of the Cloudflared service on our Linux system. This approach not only reduces the complexity of the commands required but also minimizes the chances of errors in service management tasks.

With this setup, system administrators can efficiently handle the Cloudflared service with just a few simple commands, improving productivity and reducing the risk of mistakes.