Bash script to run command when remote server is available
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
add a comment |
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
CONNECTED = 0
there is no space when assigning a variable. It should beCONNECTED=0
.[ $(ping my-service.com) != null ]
is strange. Why would you expectping
program to print"null"
string? Use smth likeif ping -c1 "$ip_addr" 2>/dev/null >/dev/null
to check if ip_addr is available. The errorName 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
add a comment |
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
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
bash
asked Nov 13 '18 at 14:20
Victor FedorenkoVictor Fedorenko
1218
1218
CONNECTED = 0
there is no space when assigning a variable. It should beCONNECTED=0
.[ $(ping my-service.com) != null ]
is strange. Why would you expectping
program to print"null"
string? Use smth likeif ping -c1 "$ip_addr" 2>/dev/null >/dev/null
to check if ip_addr is available. The errorName 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
add a comment |
CONNECTED = 0
there is no space when assigning a variable. It should beCONNECTED=0
.[ $(ping my-service.com) != null ]
is strange. Why would you expectping
program to print"null"
string? Use smth likeif ping -c1 "$ip_addr" 2>/dev/null >/dev/null
to check if ip_addr is available. The errorName 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
add a comment |
1 Answer
1
active
oldest
votes
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
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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
add a comment |
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
add a comment |
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
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
answered Nov 13 '18 at 14:33
JoseLinaresJoseLinares
536312
536312
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
CONNECTED = 0
there is no space when assigning a variable. It should beCONNECTED=0
.[ $(ping my-service.com) != null ]
is strange. Why would you expectping
program to print"null"
string? Use smth likeif ping -c1 "$ip_addr" 2>/dev/null >/dev/null
to check if ip_addr is available. The errorName 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