How to create a service that will respond to an HTTP request by running a bash script and returning the...












0















I have a DO loadbalancer setup in front of a MySQL Group Replication setup. What I need to do is create a health check for the loadbalancer via an http request.



What I've come up with so far is to create a bash script (code below) that basically checks if MySQL is up and running and then run this via xinetd on port 9201.



However, when I curl to http://ip:port from another server it never reads any output from my bash file and just connection reset by peer (after connecting and timing out).



I'm very new to all this, so I'm not sure if what I'm doing is even correct in the slightest.



service mysqlchk
{
flags = REUSE
socket_type = stream
protocol = tcp
port = 9201
wait = no
user = root
server = /opt/mysqlchk.sh
disable = no
log_type = FILE /var/log/xinetd.log
log_on_success = DURATION EXIT HOST PID USERID
log_on_failure = ATTEMPT HOST USERID
only_from = 0.0.0.0/0
}


Bash (Found online)



MYSQL_HOST="ip"
MYSQL_PORT="3306"
MYSQL_USERNAME="user"
MYSQL_PASSWORD="=password"

#
# We perform a simple query that should return a few results :-p

ERROR_MSG=`/usr/bin/mysql --host=$MYSQL_HOST --port=$MYSQL_PORT --
user=$MYSQL_USERNAME --password=$MYSQL_PASSWORD -e "show databases;"
2>/dev/null`

#
# Check the output. If it is not empty then everything is fine and we
return
# something. Else, we just do not return anything.
#
if [ "$ERROR_MSG" != "" ]
then
# mysql is fine, return http 200
/bin/echo -e "HTTP/1.1 200 OKrn"
/bin/echo -e "Content-Type: Content-Type: text/plainrn"
/bin/echo -e "rn"
/bin/echo -e "MySQL is running.rn"
/bin/echo -e "rn"
else
# mysql is not fine, return http 503
/bin/echo -e "HTTP/1.1 503 Service Unavailablern"
/bin/echo -e "Content-Type: Content-Type: text/plainrn"
/bin/echo -e "rn"
/bin/echo -e "MySQL is *down*.rn"
/bin/echo -e "rn"
fi


What's returned when I curl:



curl -v http://ip:9201/
* Trying ip...
* TCP_NODELAY set
* Connected to ip (ip) port 9201 (#0)
> GET / HTTP/1.1
> Host: ip:9201
> User-Agent: curl/7.58.0
> Accept: */*
>
* Recv failure: Connection reset by peer
* stopped the pause stream!
* Closing connection 0
curl: (56) Recv failure: Connection reset by peer


I'm not sure if the issue is because I'm just responding incorrectly to the request or?



EDIT: Running the bash script manually returns the right output based on if it can connect or not. I don't think there's a syntactical error in the bash itself, just the way it responds to an http request possibly.










share|improve this question

























  • Some extra newlines are present in the example posted. Can you double-check that the example posted matches the live one?

    – user000001
    Dec 31 '18 at 13:00













  • @user000001 Which file are you referencing? The bash or the curl output?

    – Mo Tawakol
    Dec 31 '18 at 13:02













  • the bash file............

    – user000001
    Dec 31 '18 at 13:03











  • The bash file is exactly as I have it on my setup

    – Mo Tawakol
    Dec 31 '18 at 13:05











  • Then it's broken. The assignment of ERROR_MSG should not span multiple lines, and the following return is part of the comment in the line above.

    – user000001
    Dec 31 '18 at 13:06


















0















I have a DO loadbalancer setup in front of a MySQL Group Replication setup. What I need to do is create a health check for the loadbalancer via an http request.



What I've come up with so far is to create a bash script (code below) that basically checks if MySQL is up and running and then run this via xinetd on port 9201.



However, when I curl to http://ip:port from another server it never reads any output from my bash file and just connection reset by peer (after connecting and timing out).



I'm very new to all this, so I'm not sure if what I'm doing is even correct in the slightest.



service mysqlchk
{
flags = REUSE
socket_type = stream
protocol = tcp
port = 9201
wait = no
user = root
server = /opt/mysqlchk.sh
disable = no
log_type = FILE /var/log/xinetd.log
log_on_success = DURATION EXIT HOST PID USERID
log_on_failure = ATTEMPT HOST USERID
only_from = 0.0.0.0/0
}


Bash (Found online)



MYSQL_HOST="ip"
MYSQL_PORT="3306"
MYSQL_USERNAME="user"
MYSQL_PASSWORD="=password"

#
# We perform a simple query that should return a few results :-p

ERROR_MSG=`/usr/bin/mysql --host=$MYSQL_HOST --port=$MYSQL_PORT --
user=$MYSQL_USERNAME --password=$MYSQL_PASSWORD -e "show databases;"
2>/dev/null`

#
# Check the output. If it is not empty then everything is fine and we
return
# something. Else, we just do not return anything.
#
if [ "$ERROR_MSG" != "" ]
then
# mysql is fine, return http 200
/bin/echo -e "HTTP/1.1 200 OKrn"
/bin/echo -e "Content-Type: Content-Type: text/plainrn"
/bin/echo -e "rn"
/bin/echo -e "MySQL is running.rn"
/bin/echo -e "rn"
else
# mysql is not fine, return http 503
/bin/echo -e "HTTP/1.1 503 Service Unavailablern"
/bin/echo -e "Content-Type: Content-Type: text/plainrn"
/bin/echo -e "rn"
/bin/echo -e "MySQL is *down*.rn"
/bin/echo -e "rn"
fi


What's returned when I curl:



curl -v http://ip:9201/
* Trying ip...
* TCP_NODELAY set
* Connected to ip (ip) port 9201 (#0)
> GET / HTTP/1.1
> Host: ip:9201
> User-Agent: curl/7.58.0
> Accept: */*
>
* Recv failure: Connection reset by peer
* stopped the pause stream!
* Closing connection 0
curl: (56) Recv failure: Connection reset by peer


I'm not sure if the issue is because I'm just responding incorrectly to the request or?



EDIT: Running the bash script manually returns the right output based on if it can connect or not. I don't think there's a syntactical error in the bash itself, just the way it responds to an http request possibly.










share|improve this question

























  • Some extra newlines are present in the example posted. Can you double-check that the example posted matches the live one?

    – user000001
    Dec 31 '18 at 13:00













  • @user000001 Which file are you referencing? The bash or the curl output?

    – Mo Tawakol
    Dec 31 '18 at 13:02













  • the bash file............

    – user000001
    Dec 31 '18 at 13:03











  • The bash file is exactly as I have it on my setup

    – Mo Tawakol
    Dec 31 '18 at 13:05











  • Then it's broken. The assignment of ERROR_MSG should not span multiple lines, and the following return is part of the comment in the line above.

    – user000001
    Dec 31 '18 at 13:06
















0












0








0








I have a DO loadbalancer setup in front of a MySQL Group Replication setup. What I need to do is create a health check for the loadbalancer via an http request.



What I've come up with so far is to create a bash script (code below) that basically checks if MySQL is up and running and then run this via xinetd on port 9201.



However, when I curl to http://ip:port from another server it never reads any output from my bash file and just connection reset by peer (after connecting and timing out).



I'm very new to all this, so I'm not sure if what I'm doing is even correct in the slightest.



service mysqlchk
{
flags = REUSE
socket_type = stream
protocol = tcp
port = 9201
wait = no
user = root
server = /opt/mysqlchk.sh
disable = no
log_type = FILE /var/log/xinetd.log
log_on_success = DURATION EXIT HOST PID USERID
log_on_failure = ATTEMPT HOST USERID
only_from = 0.0.0.0/0
}


Bash (Found online)



MYSQL_HOST="ip"
MYSQL_PORT="3306"
MYSQL_USERNAME="user"
MYSQL_PASSWORD="=password"

#
# We perform a simple query that should return a few results :-p

ERROR_MSG=`/usr/bin/mysql --host=$MYSQL_HOST --port=$MYSQL_PORT --
user=$MYSQL_USERNAME --password=$MYSQL_PASSWORD -e "show databases;"
2>/dev/null`

#
# Check the output. If it is not empty then everything is fine and we
return
# something. Else, we just do not return anything.
#
if [ "$ERROR_MSG" != "" ]
then
# mysql is fine, return http 200
/bin/echo -e "HTTP/1.1 200 OKrn"
/bin/echo -e "Content-Type: Content-Type: text/plainrn"
/bin/echo -e "rn"
/bin/echo -e "MySQL is running.rn"
/bin/echo -e "rn"
else
# mysql is not fine, return http 503
/bin/echo -e "HTTP/1.1 503 Service Unavailablern"
/bin/echo -e "Content-Type: Content-Type: text/plainrn"
/bin/echo -e "rn"
/bin/echo -e "MySQL is *down*.rn"
/bin/echo -e "rn"
fi


What's returned when I curl:



curl -v http://ip:9201/
* Trying ip...
* TCP_NODELAY set
* Connected to ip (ip) port 9201 (#0)
> GET / HTTP/1.1
> Host: ip:9201
> User-Agent: curl/7.58.0
> Accept: */*
>
* Recv failure: Connection reset by peer
* stopped the pause stream!
* Closing connection 0
curl: (56) Recv failure: Connection reset by peer


I'm not sure if the issue is because I'm just responding incorrectly to the request or?



EDIT: Running the bash script manually returns the right output based on if it can connect or not. I don't think there's a syntactical error in the bash itself, just the way it responds to an http request possibly.










share|improve this question
















I have a DO loadbalancer setup in front of a MySQL Group Replication setup. What I need to do is create a health check for the loadbalancer via an http request.



What I've come up with so far is to create a bash script (code below) that basically checks if MySQL is up and running and then run this via xinetd on port 9201.



However, when I curl to http://ip:port from another server it never reads any output from my bash file and just connection reset by peer (after connecting and timing out).



I'm very new to all this, so I'm not sure if what I'm doing is even correct in the slightest.



service mysqlchk
{
flags = REUSE
socket_type = stream
protocol = tcp
port = 9201
wait = no
user = root
server = /opt/mysqlchk.sh
disable = no
log_type = FILE /var/log/xinetd.log
log_on_success = DURATION EXIT HOST PID USERID
log_on_failure = ATTEMPT HOST USERID
only_from = 0.0.0.0/0
}


Bash (Found online)



MYSQL_HOST="ip"
MYSQL_PORT="3306"
MYSQL_USERNAME="user"
MYSQL_PASSWORD="=password"

#
# We perform a simple query that should return a few results :-p

ERROR_MSG=`/usr/bin/mysql --host=$MYSQL_HOST --port=$MYSQL_PORT --
user=$MYSQL_USERNAME --password=$MYSQL_PASSWORD -e "show databases;"
2>/dev/null`

#
# Check the output. If it is not empty then everything is fine and we
return
# something. Else, we just do not return anything.
#
if [ "$ERROR_MSG" != "" ]
then
# mysql is fine, return http 200
/bin/echo -e "HTTP/1.1 200 OKrn"
/bin/echo -e "Content-Type: Content-Type: text/plainrn"
/bin/echo -e "rn"
/bin/echo -e "MySQL is running.rn"
/bin/echo -e "rn"
else
# mysql is not fine, return http 503
/bin/echo -e "HTTP/1.1 503 Service Unavailablern"
/bin/echo -e "Content-Type: Content-Type: text/plainrn"
/bin/echo -e "rn"
/bin/echo -e "MySQL is *down*.rn"
/bin/echo -e "rn"
fi


What's returned when I curl:



curl -v http://ip:9201/
* Trying ip...
* TCP_NODELAY set
* Connected to ip (ip) port 9201 (#0)
> GET / HTTP/1.1
> Host: ip:9201
> User-Agent: curl/7.58.0
> Accept: */*
>
* Recv failure: Connection reset by peer
* stopped the pause stream!
* Closing connection 0
curl: (56) Recv failure: Connection reset by peer


I'm not sure if the issue is because I'm just responding incorrectly to the request or?



EDIT: Running the bash script manually returns the right output based on if it can connect or not. I don't think there's a syntactical error in the bash itself, just the way it responds to an http request possibly.







bash http ubuntu xinetd






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 31 '18 at 13:11







Mo Tawakol

















asked Dec 31 '18 at 12:32









Mo TawakolMo Tawakol

441110




441110













  • Some extra newlines are present in the example posted. Can you double-check that the example posted matches the live one?

    – user000001
    Dec 31 '18 at 13:00













  • @user000001 Which file are you referencing? The bash or the curl output?

    – Mo Tawakol
    Dec 31 '18 at 13:02













  • the bash file............

    – user000001
    Dec 31 '18 at 13:03











  • The bash file is exactly as I have it on my setup

    – Mo Tawakol
    Dec 31 '18 at 13:05











  • Then it's broken. The assignment of ERROR_MSG should not span multiple lines, and the following return is part of the comment in the line above.

    – user000001
    Dec 31 '18 at 13:06





















  • Some extra newlines are present in the example posted. Can you double-check that the example posted matches the live one?

    – user000001
    Dec 31 '18 at 13:00













  • @user000001 Which file are you referencing? The bash or the curl output?

    – Mo Tawakol
    Dec 31 '18 at 13:02













  • the bash file............

    – user000001
    Dec 31 '18 at 13:03











  • The bash file is exactly as I have it on my setup

    – Mo Tawakol
    Dec 31 '18 at 13:05











  • Then it's broken. The assignment of ERROR_MSG should not span multiple lines, and the following return is part of the comment in the line above.

    – user000001
    Dec 31 '18 at 13:06



















Some extra newlines are present in the example posted. Can you double-check that the example posted matches the live one?

– user000001
Dec 31 '18 at 13:00







Some extra newlines are present in the example posted. Can you double-check that the example posted matches the live one?

– user000001
Dec 31 '18 at 13:00















@user000001 Which file are you referencing? The bash or the curl output?

– Mo Tawakol
Dec 31 '18 at 13:02







@user000001 Which file are you referencing? The bash or the curl output?

– Mo Tawakol
Dec 31 '18 at 13:02















the bash file............

– user000001
Dec 31 '18 at 13:03





the bash file............

– user000001
Dec 31 '18 at 13:03













The bash file is exactly as I have it on my setup

– Mo Tawakol
Dec 31 '18 at 13:05





The bash file is exactly as I have it on my setup

– Mo Tawakol
Dec 31 '18 at 13:05













Then it's broken. The assignment of ERROR_MSG should not span multiple lines, and the following return is part of the comment in the line above.

– user000001
Dec 31 '18 at 13:06







Then it's broken. The assignment of ERROR_MSG should not span multiple lines, and the following return is part of the comment in the line above.

– user000001
Dec 31 '18 at 13:06














1 Answer
1






active

oldest

votes


















0














xinetd listens for incoming requests over a network and launches the appropriate service for that request. Requests are made using port numbers as identifiers and xinetd usually launches another daemon to handle the request.



In you case, you are launching a script. Make sure it's executable, have the right permission and add the corresponding shebang



#! /bin/bash


Then try



telnet ip 9201





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53987569%2fhow-to-create-a-service-that-will-respond-to-an-http-request-by-running-a-bash-s%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    xinetd listens for incoming requests over a network and launches the appropriate service for that request. Requests are made using port numbers as identifiers and xinetd usually launches another daemon to handle the request.



    In you case, you are launching a script. Make sure it's executable, have the right permission and add the corresponding shebang



    #! /bin/bash


    Then try



    telnet ip 9201





    share|improve this answer




























      0














      xinetd listens for incoming requests over a network and launches the appropriate service for that request. Requests are made using port numbers as identifiers and xinetd usually launches another daemon to handle the request.



      In you case, you are launching a script. Make sure it's executable, have the right permission and add the corresponding shebang



      #! /bin/bash


      Then try



      telnet ip 9201





      share|improve this answer


























        0












        0








        0







        xinetd listens for incoming requests over a network and launches the appropriate service for that request. Requests are made using port numbers as identifiers and xinetd usually launches another daemon to handle the request.



        In you case, you are launching a script. Make sure it's executable, have the right permission and add the corresponding shebang



        #! /bin/bash


        Then try



        telnet ip 9201





        share|improve this answer













        xinetd listens for incoming requests over a network and launches the appropriate service for that request. Requests are made using port numbers as identifiers and xinetd usually launches another daemon to handle the request.



        In you case, you are launching a script. Make sure it's executable, have the right permission and add the corresponding shebang



        #! /bin/bash


        Then try



        telnet ip 9201






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Dec 31 '18 at 17:44









        Diego Torres MilanoDiego Torres Milano

        50.2k680107




        50.2k680107
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53987569%2fhow-to-create-a-service-that-will-respond-to-an-http-request-by-running-a-bash-s%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Monofisismo

            Angular Downloading a file using contenturl with Basic Authentication

            Olmecas