Expect not exiting non-zero












0















My real goal is to have a script that will run locally to ssh into a remote server, restart tomcat, and use expect to wait for the "Server startup in" message, or exit non-zero if it sees startup failed. My attempt is this script. I'm using localhost for debugging, and INFO instead of "startup failed" to artificially trigger the exit:



ssh -T localhost <<SSH_EOF
docker restart app
expect <<EXPECT_EOF
set timeout 30
spawn docker logs --since 1s -f app
expect_before "INFO" { exit 1 }
expect "Server startup in"
EXPECT_EOF
if [[ $? -eq 0]]; then
echo "Success!"
else
echo "Failed!"
fi
SSH_EOF


The script does exit on the first INFO message, but prints Success! Why wouldn't it exit with 1?










share|improve this question























  • use expect -d to show verbose logging: which is seen first, "INFO" or "Server startup in", I wonder...

    – glenn jackman
    Jan 25 at 22:52











  • I had tried the -d flag, but it's still a great tip. INFO is definitely seen first. The very first line of output is 26-Jan-2019 17:51:32.070 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server version name: Apache Tomcat/9.0.14 and expect would exit immediately, just with $? already resolved (see accepted answer) to '0'.

    – Dave
    Jan 26 at 17:55
















0















My real goal is to have a script that will run locally to ssh into a remote server, restart tomcat, and use expect to wait for the "Server startup in" message, or exit non-zero if it sees startup failed. My attempt is this script. I'm using localhost for debugging, and INFO instead of "startup failed" to artificially trigger the exit:



ssh -T localhost <<SSH_EOF
docker restart app
expect <<EXPECT_EOF
set timeout 30
spawn docker logs --since 1s -f app
expect_before "INFO" { exit 1 }
expect "Server startup in"
EXPECT_EOF
if [[ $? -eq 0]]; then
echo "Success!"
else
echo "Failed!"
fi
SSH_EOF


The script does exit on the first INFO message, but prints Success! Why wouldn't it exit with 1?










share|improve this question























  • use expect -d to show verbose logging: which is seen first, "INFO" or "Server startup in", I wonder...

    – glenn jackman
    Jan 25 at 22:52











  • I had tried the -d flag, but it's still a great tip. INFO is definitely seen first. The very first line of output is 26-Jan-2019 17:51:32.070 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server version name: Apache Tomcat/9.0.14 and expect would exit immediately, just with $? already resolved (see accepted answer) to '0'.

    – Dave
    Jan 26 at 17:55














0












0








0








My real goal is to have a script that will run locally to ssh into a remote server, restart tomcat, and use expect to wait for the "Server startup in" message, or exit non-zero if it sees startup failed. My attempt is this script. I'm using localhost for debugging, and INFO instead of "startup failed" to artificially trigger the exit:



ssh -T localhost <<SSH_EOF
docker restart app
expect <<EXPECT_EOF
set timeout 30
spawn docker logs --since 1s -f app
expect_before "INFO" { exit 1 }
expect "Server startup in"
EXPECT_EOF
if [[ $? -eq 0]]; then
echo "Success!"
else
echo "Failed!"
fi
SSH_EOF


The script does exit on the first INFO message, but prints Success! Why wouldn't it exit with 1?










share|improve this question














My real goal is to have a script that will run locally to ssh into a remote server, restart tomcat, and use expect to wait for the "Server startup in" message, or exit non-zero if it sees startup failed. My attempt is this script. I'm using localhost for debugging, and INFO instead of "startup failed" to artificially trigger the exit:



ssh -T localhost <<SSH_EOF
docker restart app
expect <<EXPECT_EOF
set timeout 30
spawn docker logs --since 1s -f app
expect_before "INFO" { exit 1 }
expect "Server startup in"
EXPECT_EOF
if [[ $? -eq 0]]; then
echo "Success!"
else
echo "Failed!"
fi
SSH_EOF


The script does exit on the first INFO message, but prints Success! Why wouldn't it exit with 1?







linux bash ssh expect






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 25 at 22:16









DaveDave

1183




1183













  • use expect -d to show verbose logging: which is seen first, "INFO" or "Server startup in", I wonder...

    – glenn jackman
    Jan 25 at 22:52











  • I had tried the -d flag, but it's still a great tip. INFO is definitely seen first. The very first line of output is 26-Jan-2019 17:51:32.070 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server version name: Apache Tomcat/9.0.14 and expect would exit immediately, just with $? already resolved (see accepted answer) to '0'.

    – Dave
    Jan 26 at 17:55



















  • use expect -d to show verbose logging: which is seen first, "INFO" or "Server startup in", I wonder...

    – glenn jackman
    Jan 25 at 22:52











  • I had tried the -d flag, but it's still a great tip. INFO is definitely seen first. The very first line of output is 26-Jan-2019 17:51:32.070 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server version name: Apache Tomcat/9.0.14 and expect would exit immediately, just with $? already resolved (see accepted answer) to '0'.

    – Dave
    Jan 26 at 17:55

















use expect -d to show verbose logging: which is seen first, "INFO" or "Server startup in", I wonder...

– glenn jackman
Jan 25 at 22:52





use expect -d to show verbose logging: which is seen first, "INFO" or "Server startup in", I wonder...

– glenn jackman
Jan 25 at 22:52













I had tried the -d flag, but it's still a great tip. INFO is definitely seen first. The very first line of output is 26-Jan-2019 17:51:32.070 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server version name: Apache Tomcat/9.0.14 and expect would exit immediately, just with $? already resolved (see accepted answer) to '0'.

– Dave
Jan 26 at 17:55





I had tried the -d flag, but it's still a great tip. INFO is definitely seen first. The very first line of output is 26-Jan-2019 17:51:32.070 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server version name: Apache Tomcat/9.0.14 and expect would exit immediately, just with $? already resolved (see accepted answer) to '0'.

– Dave
Jan 26 at 17:55










1 Answer
1






active

oldest

votes


















2














You are using << to include a here document, which is subject to parameter
expansion, command substitution, and arithmetic expansion
. In particular, the $? gets retplaced by 0 (probably) before it is even passed to ssh.



You need to quote the here delimiter to avoid this, eg <<'SSH_EOF'. The actual delimeter is still SSH_EOF. You should probably do the same for the expect EXPECT_EOF, in case of future changes that involve $.






share|improve this answer
























  • Brilliant. Adding quotes worked perfectly. Thanks!

    – Dave
    Jan 26 at 17:52











Your Answer








StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "3"
};
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%2fsuperuser.com%2fquestions%2f1398590%2fexpect-not-exiting-non-zero%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









2














You are using << to include a here document, which is subject to parameter
expansion, command substitution, and arithmetic expansion
. In particular, the $? gets retplaced by 0 (probably) before it is even passed to ssh.



You need to quote the here delimiter to avoid this, eg <<'SSH_EOF'. The actual delimeter is still SSH_EOF. You should probably do the same for the expect EXPECT_EOF, in case of future changes that involve $.






share|improve this answer
























  • Brilliant. Adding quotes worked perfectly. Thanks!

    – Dave
    Jan 26 at 17:52
















2














You are using << to include a here document, which is subject to parameter
expansion, command substitution, and arithmetic expansion
. In particular, the $? gets retplaced by 0 (probably) before it is even passed to ssh.



You need to quote the here delimiter to avoid this, eg <<'SSH_EOF'. The actual delimeter is still SSH_EOF. You should probably do the same for the expect EXPECT_EOF, in case of future changes that involve $.






share|improve this answer
























  • Brilliant. Adding quotes worked perfectly. Thanks!

    – Dave
    Jan 26 at 17:52














2












2








2







You are using << to include a here document, which is subject to parameter
expansion, command substitution, and arithmetic expansion
. In particular, the $? gets retplaced by 0 (probably) before it is even passed to ssh.



You need to quote the here delimiter to avoid this, eg <<'SSH_EOF'. The actual delimeter is still SSH_EOF. You should probably do the same for the expect EXPECT_EOF, in case of future changes that involve $.






share|improve this answer













You are using << to include a here document, which is subject to parameter
expansion, command substitution, and arithmetic expansion
. In particular, the $? gets retplaced by 0 (probably) before it is even passed to ssh.



You need to quote the here delimiter to avoid this, eg <<'SSH_EOF'. The actual delimeter is still SSH_EOF. You should probably do the same for the expect EXPECT_EOF, in case of future changes that involve $.







share|improve this answer












share|improve this answer



share|improve this answer










answered Jan 26 at 16:36









meuhmeuh

3,65511021




3,65511021













  • Brilliant. Adding quotes worked perfectly. Thanks!

    – Dave
    Jan 26 at 17:52



















  • Brilliant. Adding quotes worked perfectly. Thanks!

    – Dave
    Jan 26 at 17:52

















Brilliant. Adding quotes worked perfectly. Thanks!

– Dave
Jan 26 at 17:52





Brilliant. Adding quotes worked perfectly. Thanks!

– Dave
Jan 26 at 17:52


















draft saved

draft discarded




















































Thanks for contributing an answer to Super User!


  • 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%2fsuperuser.com%2fquestions%2f1398590%2fexpect-not-exiting-non-zero%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

Plaza Victoria

Puebla de Zaragoza

Musa