Bash script to run command when remote server is available










0















I have an application which depends on another remote service. What's I'd like to do is to make simple script which will run command locally when remote server will be available.



Something like this:



#!/bin/bash 

CONNECTED = 0

while [ $CONNECTED != 1 ]; do
if [ $(ping my-service.com) != null ] then
my-command.sh
let CONNECTED = 1
fi

sleep 2
done


Actually it doesn't work because of ping command. It cause 'Name or service not known' error. I can compare string fully to make it work, but the question is if there are any ready-to-use commands, libs to do the same? Or mb this approach is bad and I should do it in another way.










share|improve this question






















  • CONNECTED = 0 there is no space when assigning a variable. It should be CONNECTED=0. [ $(ping my-service.com) != null ] is strange. Why would you expect ping program to print "null" string? Use smth like if ping -c1 "$ip_addr" 2>/dev/null >/dev/null to check if ip_addr is available. The error Name or service not known indicates you have no internet connection from your machine at all or that your dns servers are not configured properly. Is this expected?

    – Kamil Cuk
    Nov 13 '18 at 14:26
















0















I have an application which depends on another remote service. What's I'd like to do is to make simple script which will run command locally when remote server will be available.



Something like this:



#!/bin/bash 

CONNECTED = 0

while [ $CONNECTED != 1 ]; do
if [ $(ping my-service.com) != null ] then
my-command.sh
let CONNECTED = 1
fi

sleep 2
done


Actually it doesn't work because of ping command. It cause 'Name or service not known' error. I can compare string fully to make it work, but the question is if there are any ready-to-use commands, libs to do the same? Or mb this approach is bad and I should do it in another way.










share|improve this question






















  • CONNECTED = 0 there is no space when assigning a variable. It should be CONNECTED=0. [ $(ping my-service.com) != null ] is strange. Why would you expect ping program to print "null" string? Use smth like if ping -c1 "$ip_addr" 2>/dev/null >/dev/null to check if ip_addr is available. The error Name or service not known indicates you have no internet connection from your machine at all or that your dns servers are not configured properly. Is this expected?

    – Kamil Cuk
    Nov 13 '18 at 14:26














0












0








0








I have an application which depends on another remote service. What's I'd like to do is to make simple script which will run command locally when remote server will be available.



Something like this:



#!/bin/bash 

CONNECTED = 0

while [ $CONNECTED != 1 ]; do
if [ $(ping my-service.com) != null ] then
my-command.sh
let CONNECTED = 1
fi

sleep 2
done


Actually it doesn't work because of ping command. It cause 'Name or service not known' error. I can compare string fully to make it work, but the question is if there are any ready-to-use commands, libs to do the same? Or mb this approach is bad and I should do it in another way.










share|improve this question














I have an application which depends on another remote service. What's I'd like to do is to make simple script which will run command locally when remote server will be available.



Something like this:



#!/bin/bash 

CONNECTED = 0

while [ $CONNECTED != 1 ]; do
if [ $(ping my-service.com) != null ] then
my-command.sh
let CONNECTED = 1
fi

sleep 2
done


Actually it doesn't work because of ping command. It cause 'Name or service not known' error. I can compare string fully to make it work, but the question is if there are any ready-to-use commands, libs to do the same? Or mb this approach is bad and I should do it in another way.







bash






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 13 '18 at 14:20









Victor FedorenkoVictor Fedorenko

1218




1218












  • CONNECTED = 0 there is no space when assigning a variable. It should be CONNECTED=0. [ $(ping my-service.com) != null ] is strange. Why would you expect ping program to print "null" string? Use smth like if ping -c1 "$ip_addr" 2>/dev/null >/dev/null to check if ip_addr is available. The error Name or service not known indicates you have no internet connection from your machine at all or that your dns servers are not configured properly. Is this expected?

    – Kamil Cuk
    Nov 13 '18 at 14:26


















  • CONNECTED = 0 there is no space when assigning a variable. It should be CONNECTED=0. [ $(ping my-service.com) != null ] is strange. Why would you expect ping program to print "null" string? Use smth like if ping -c1 "$ip_addr" 2>/dev/null >/dev/null to check if ip_addr is available. The error Name or service not known indicates you have no internet connection from your machine at all or that your dns servers are not configured properly. Is this expected?

    – Kamil Cuk
    Nov 13 '18 at 14:26

















CONNECTED = 0 there is no space when assigning a variable. It should be CONNECTED=0. [ $(ping my-service.com) != null ] is strange. Why would you expect ping program to print "null" string? Use smth like if ping -c1 "$ip_addr" 2>/dev/null >/dev/null to check if ip_addr is available. The error Name or service not known indicates you have no internet connection from your machine at all or that your dns servers are not configured properly. Is this expected?

– Kamil Cuk
Nov 13 '18 at 14:26






CONNECTED = 0 there is no space when assigning a variable. It should be CONNECTED=0. [ $(ping my-service.com) != null ] is strange. Why would you expect ping program to print "null" string? Use smth like if ping -c1 "$ip_addr" 2>/dev/null >/dev/null to check if ip_addr is available. The error Name or service not known indicates you have no internet connection from your machine at all or that your dns servers are not configured properly. Is this expected?

– Kamil Cuk
Nov 13 '18 at 14:26













1 Answer
1






active

oldest

votes


















0














Try something like this:



#!/bin/bash 

CONNECTED=1

while [[ $CONNECTED != 0 ]]; do
ping -c1 my-service.com
CONNECTED=$?
if [[ $CONNECTED == 0 ]] ; then
my-command.sh
fi
sleep 2
done


$? contains the return value of the last executed command (ping in this case). When ping return 0, it means no errors, so it will execute my-command.sh






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%2f53283107%2fbash-script-to-run-command-when-remote-server-is-available%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














    Try something like this:



    #!/bin/bash 

    CONNECTED=1

    while [[ $CONNECTED != 0 ]]; do
    ping -c1 my-service.com
    CONNECTED=$?
    if [[ $CONNECTED == 0 ]] ; then
    my-command.sh
    fi
    sleep 2
    done


    $? contains the return value of the last executed command (ping in this case). When ping return 0, it means no errors, so it will execute my-command.sh






    share|improve this answer



























      0














      Try something like this:



      #!/bin/bash 

      CONNECTED=1

      while [[ $CONNECTED != 0 ]]; do
      ping -c1 my-service.com
      CONNECTED=$?
      if [[ $CONNECTED == 0 ]] ; then
      my-command.sh
      fi
      sleep 2
      done


      $? contains the return value of the last executed command (ping in this case). When ping return 0, it means no errors, so it will execute my-command.sh






      share|improve this answer

























        0












        0








        0







        Try something like this:



        #!/bin/bash 

        CONNECTED=1

        while [[ $CONNECTED != 0 ]]; do
        ping -c1 my-service.com
        CONNECTED=$?
        if [[ $CONNECTED == 0 ]] ; then
        my-command.sh
        fi
        sleep 2
        done


        $? contains the return value of the last executed command (ping in this case). When ping return 0, it means no errors, so it will execute my-command.sh






        share|improve this answer













        Try something like this:



        #!/bin/bash 

        CONNECTED=1

        while [[ $CONNECTED != 0 ]]; do
        ping -c1 my-service.com
        CONNECTED=$?
        if [[ $CONNECTED == 0 ]] ; then
        my-command.sh
        fi
        sleep 2
        done


        $? contains the return value of the last executed command (ping in this case). When ping return 0, it means no errors, so it will execute my-command.sh







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 13 '18 at 14:33









        JoseLinaresJoseLinares

        536312




        536312



























            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%2f53283107%2fbash-script-to-run-command-when-remote-server-is-available%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







            這個網誌中的熱門文章

            How to read a connectionString WITH PROVIDER in .NET Core?

            In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

            Museum of Modern and Contemporary Art of Trento and Rovereto