Skip to content

Instantly share code, notes, and snippets.

@danigunawan
Forked from ahmedsadman/create-service.sh
Created February 22, 2023 17:25
Show Gist options
  • Save danigunawan/0640e74d5eeea02bd62a8dbea747a91a to your computer and use it in GitHub Desktop.
Save danigunawan/0640e74d5eeea02bd62a8dbea747a91a to your computer and use it in GitHub Desktop.

Revisions

  1. @ahmedsadman ahmedsadman revised this gist Dec 10, 2019. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions create-service.sh
    Original file line number Diff line number Diff line change
    @@ -5,13 +5,13 @@
    ##

    # Sample JSON file:
    #{
    # {
    # "service_name": "test_service",
    # "description": "Netcore dev server",
    # "package_path": "dotnet",
    # "service_path": "/home/muhib/DevOps/loopdatetime/Output/BuildApp.dll",
    # "service_url": "localhost:6000"
    #}
    # }

    # get the first argument, the JSON file
    args=("$@")
  2. @ahmedsadman ahmedsadman revised this gist Dec 10, 2019. 1 changed file with 8 additions and 0 deletions.
    8 changes: 8 additions & 0 deletions create-service.sh
    Original file line number Diff line number Diff line change
    @@ -4,6 +4,14 @@
    ## Creates Service file based on JSON data
    ##

    # Sample JSON file:
    #{
    # "service_name": "test_service",
    # "description": "Netcore dev server",
    # "package_path": "dotnet",
    # "service_path": "/home/muhib/DevOps/loopdatetime/Output/BuildApp.dll",
    # "service_url": "localhost:6000"
    #}

    # get the first argument, the JSON file
    args=("$@")
  3. @ahmedsadman ahmedsadman revised this gist Dec 9, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion create-service.sh
    Original file line number Diff line number Diff line change
    @@ -30,7 +30,7 @@ SERVICE_PATH=${SERVICE_PATH//'"'/}
    # SERVICE_URL=${SERVICE_URL//'"'/}

    # check if service is active
    IS_ACTIVE=$(sudo systemctl is-active $SERVICE_NAME) # replace " with null
    IS_ACTIVE=$(sudo systemctl is-active $SERVICE_NAME)
    if [ "$IS_ACTIVE" == "active" ]; then
    # restart the service
    echo "Service is running"
  4. @ahmedsadman ahmedsadman created this gist Dec 9, 2019.
    63 changes: 63 additions & 0 deletions create-service.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,63 @@
    #!/usr/bin/bash

    ##
    ## Creates Service file based on JSON data
    ##


    # get the first argument, the JSON file
    args=("$@")
    DATA_FILE=${args[0]}

    # check if argument is passed
    if [ -z "$DATA_FILE" ]; then
    echo "JSON Data file should be passed as first argument"
    exit 1
    fi

    # parse the json file
    SERVICE_NAME=$(cat $DATA_FILE | jq '.service_name')
    DESCRIPTION=$(cat $DATA_FILE | jq '.description')
    PKG_PATH=$(cat $DATA_FILE | jq '.package_path')
    SERVICE_PATH=$(cat $DATA_FILE | jq '.service_path')
    # SERVICE_URL=$(cat $DATA_FILE | jq '.service_url')

    # remove the double quotes
    DESCRIPTION=${DESCRIPTION//'"'/}
    SERVICE_NAME=${SERVICE_NAME//'"'/}
    PKG_PATH=${PKG_PATH//'"'/}
    SERVICE_PATH=${SERVICE_PATH//'"'/}
    # SERVICE_URL=${SERVICE_URL//'"'/}

    # check if service is active
    IS_ACTIVE=$(sudo systemctl is-active $SERVICE_NAME) # replace " with null
    if [ "$IS_ACTIVE" == "active" ]; then
    # restart the service
    echo "Service is running"
    echo "Restarting service"
    sudo systemctl restart $SERVICE_NAME
    echo "Service restarted"
    else
    # create service file
    echo "Creating service file"
    sudo cat > /etc/systemd/system/${SERVICE_NAME//'"'/}.service << EOF
    [Unit]
    Description=$DESCRIPTION
    After=network.target
    [Service]
    ExecStart=$PKG_PATH $SERVICE_PATH
    Restart=on-failure
    [Install]
    WantedBy=multi-user.target
    EOF
    # restart daemon, enable and start service
    echo "Reloading daemon and enabling service"
    sudo systemctl daemon-reload
    sudo systemctl enable ${SERVICE_NAME//'.service'/} # remove the extension
    sudo systemctl start ${SERVICE_NAME//'.service'/}
    echo "Service Started"
    fi

    exit 0