Bash init script skips reading commands in if loop












0















I am trying to create an init script for a program in bash. (rhel6)



It checks for the processes first. If processes are found it will echo that program is already online and if not it'll move on to to start the program as a certain user by using launch script. After doing that it should tail the log file of the program and check for a string of words together. If the words are found it should kill tail and echo that program is online.



Here's the start segment.



prog=someProg
user=someUser
threadCount=$(ps -ef | grep $prog |grep -v 'grep' |awk '{ print $2 }'| wc -l)

startb() {
if [ "$threadCount" -eq 2 ]; then
echo "$prog already online."
else
echo "Bringing $prog online."
su $user -c "/path/to/start/script.sh"
tail -f /path/to/$prog/log/file |
while IFS=$'n' read line
do
if [[ $line == *started up and registered in* ]]; then
pkill tail
echo "$prog now online."
fi
done
fi
}


My problems:




  1. The variable $prog doesn't get picked in $threadcount no
    matter how I try. (with single and double quotes)

  2. The logic about tailing the log file works randomly. Some times it
    just works perfect. It tails and waits till the string is found
    before echoing program is online and at times it just starts script
    and then echoes that program is online without the tail or wait.


It's unpredictable. I implemented the same logic in stop segment too to monitor log and then echo but even that works the same way as start. Just random.



I am sure that this might look dumb and broken. This is made by picking pieces here and there with my beginner bash skills.



Thanks in advance for suggestions and help.










share|improve this question


















  • 2





    pkill tail would attempt to kill all tails. I know that is not your question, just an observation.

    – Red Cricket
    Nov 23 '18 at 0:49


















0















I am trying to create an init script for a program in bash. (rhel6)



It checks for the processes first. If processes are found it will echo that program is already online and if not it'll move on to to start the program as a certain user by using launch script. After doing that it should tail the log file of the program and check for a string of words together. If the words are found it should kill tail and echo that program is online.



Here's the start segment.



prog=someProg
user=someUser
threadCount=$(ps -ef | grep $prog |grep -v 'grep' |awk '{ print $2 }'| wc -l)

startb() {
if [ "$threadCount" -eq 2 ]; then
echo "$prog already online."
else
echo "Bringing $prog online."
su $user -c "/path/to/start/script.sh"
tail -f /path/to/$prog/log/file |
while IFS=$'n' read line
do
if [[ $line == *started up and registered in* ]]; then
pkill tail
echo "$prog now online."
fi
done
fi
}


My problems:




  1. The variable $prog doesn't get picked in $threadcount no
    matter how I try. (with single and double quotes)

  2. The logic about tailing the log file works randomly. Some times it
    just works perfect. It tails and waits till the string is found
    before echoing program is online and at times it just starts script
    and then echoes that program is online without the tail or wait.


It's unpredictable. I implemented the same logic in stop segment too to monitor log and then echo but even that works the same way as start. Just random.



I am sure that this might look dumb and broken. This is made by picking pieces here and there with my beginner bash skills.



Thanks in advance for suggestions and help.










share|improve this question


















  • 2





    pkill tail would attempt to kill all tails. I know that is not your question, just an observation.

    – Red Cricket
    Nov 23 '18 at 0:49
















0












0








0








I am trying to create an init script for a program in bash. (rhel6)



It checks for the processes first. If processes are found it will echo that program is already online and if not it'll move on to to start the program as a certain user by using launch script. After doing that it should tail the log file of the program and check for a string of words together. If the words are found it should kill tail and echo that program is online.



Here's the start segment.



prog=someProg
user=someUser
threadCount=$(ps -ef | grep $prog |grep -v 'grep' |awk '{ print $2 }'| wc -l)

startb() {
if [ "$threadCount" -eq 2 ]; then
echo "$prog already online."
else
echo "Bringing $prog online."
su $user -c "/path/to/start/script.sh"
tail -f /path/to/$prog/log/file |
while IFS=$'n' read line
do
if [[ $line == *started up and registered in* ]]; then
pkill tail
echo "$prog now online."
fi
done
fi
}


My problems:




  1. The variable $prog doesn't get picked in $threadcount no
    matter how I try. (with single and double quotes)

  2. The logic about tailing the log file works randomly. Some times it
    just works perfect. It tails and waits till the string is found
    before echoing program is online and at times it just starts script
    and then echoes that program is online without the tail or wait.


It's unpredictable. I implemented the same logic in stop segment too to monitor log and then echo but even that works the same way as start. Just random.



I am sure that this might look dumb and broken. This is made by picking pieces here and there with my beginner bash skills.



Thanks in advance for suggestions and help.










share|improve this question














I am trying to create an init script for a program in bash. (rhel6)



It checks for the processes first. If processes are found it will echo that program is already online and if not it'll move on to to start the program as a certain user by using launch script. After doing that it should tail the log file of the program and check for a string of words together. If the words are found it should kill tail and echo that program is online.



Here's the start segment.



prog=someProg
user=someUser
threadCount=$(ps -ef | grep $prog |grep -v 'grep' |awk '{ print $2 }'| wc -l)

startb() {
if [ "$threadCount" -eq 2 ]; then
echo "$prog already online."
else
echo "Bringing $prog online."
su $user -c "/path/to/start/script.sh"
tail -f /path/to/$prog/log/file |
while IFS=$'n' read line
do
if [[ $line == *started up and registered in* ]]; then
pkill tail
echo "$prog now online."
fi
done
fi
}


My problems:




  1. The variable $prog doesn't get picked in $threadcount no
    matter how I try. (with single and double quotes)

  2. The logic about tailing the log file works randomly. Some times it
    just works perfect. It tails and waits till the string is found
    before echoing program is online and at times it just starts script
    and then echoes that program is online without the tail or wait.


It's unpredictable. I implemented the same logic in stop segment too to monitor log and then echo but even that works the same way as start. Just random.



I am sure that this might look dumb and broken. This is made by picking pieces here and there with my beginner bash skills.



Thanks in advance for suggestions and help.







bash shell startup init rhel6






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 23 '18 at 0:32









Arun K ChowArun K Chow

11




11








  • 2





    pkill tail would attempt to kill all tails. I know that is not your question, just an observation.

    – Red Cricket
    Nov 23 '18 at 0:49
















  • 2





    pkill tail would attempt to kill all tails. I know that is not your question, just an observation.

    – Red Cricket
    Nov 23 '18 at 0:49










2




2





pkill tail would attempt to kill all tails. I know that is not your question, just an observation.

– Red Cricket
Nov 23 '18 at 0:49







pkill tail would attempt to kill all tails. I know that is not your question, just an observation.

– Red Cricket
Nov 23 '18 at 0:49














1 Answer
1






active

oldest

votes


















0














I can't reproduce the error you are experiencing with the "grep $prog"...sorry.



But for the other part.




  1. I will assume that the script starting your program, the line with su, is starting something in background and that the script end by itself. If not, your example will wait indefinitely.


  2. Could be a personal preference, but when I'm using something like tail to verify lines, I use a named pipe (mkfifo).



That would give something like :



# Getting the tail in background
tail -f /path/to/$prog/log/file > some_fifo &
# Getting the tail PID
tailPID=$!

while read line; do #You don't need to modify/use IFS here.
if [[ $line == *started up and registered in* ]]; then
kill -15 $tailPID #since you know the PID you won't kill another tail
echo "$prog now online."
break # don't like the possibility, even remote, of an infinite loop :)
fi
done < some_fifo #reading from the named pipe


Hope it can help you






share|improve this answer
























  • Thank you very much for your input Andre! Your guess is right. It's just command to start java program with load of arguments and then it throws everything to dev null. For the first one I'm not getting any error. Let's just say I declare variable $prog=someProg and when I use the command.. ps -ef | grep $prog ..as expected it should do this.. ps -ef| grep someProg But it is literally grepping for $prog and echoes that program is not running. As obviously there's no program running with that name. Thanks once again!

    – Arun K Chow
    Nov 23 '18 at 18:44











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%2f53439408%2fbash-init-script-skips-reading-commands-in-if-loop%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














I can't reproduce the error you are experiencing with the "grep $prog"...sorry.



But for the other part.




  1. I will assume that the script starting your program, the line with su, is starting something in background and that the script end by itself. If not, your example will wait indefinitely.


  2. Could be a personal preference, but when I'm using something like tail to verify lines, I use a named pipe (mkfifo).



That would give something like :



# Getting the tail in background
tail -f /path/to/$prog/log/file > some_fifo &
# Getting the tail PID
tailPID=$!

while read line; do #You don't need to modify/use IFS here.
if [[ $line == *started up and registered in* ]]; then
kill -15 $tailPID #since you know the PID you won't kill another tail
echo "$prog now online."
break # don't like the possibility, even remote, of an infinite loop :)
fi
done < some_fifo #reading from the named pipe


Hope it can help you






share|improve this answer
























  • Thank you very much for your input Andre! Your guess is right. It's just command to start java program with load of arguments and then it throws everything to dev null. For the first one I'm not getting any error. Let's just say I declare variable $prog=someProg and when I use the command.. ps -ef | grep $prog ..as expected it should do this.. ps -ef| grep someProg But it is literally grepping for $prog and echoes that program is not running. As obviously there's no program running with that name. Thanks once again!

    – Arun K Chow
    Nov 23 '18 at 18:44
















0














I can't reproduce the error you are experiencing with the "grep $prog"...sorry.



But for the other part.




  1. I will assume that the script starting your program, the line with su, is starting something in background and that the script end by itself. If not, your example will wait indefinitely.


  2. Could be a personal preference, but when I'm using something like tail to verify lines, I use a named pipe (mkfifo).



That would give something like :



# Getting the tail in background
tail -f /path/to/$prog/log/file > some_fifo &
# Getting the tail PID
tailPID=$!

while read line; do #You don't need to modify/use IFS here.
if [[ $line == *started up and registered in* ]]; then
kill -15 $tailPID #since you know the PID you won't kill another tail
echo "$prog now online."
break # don't like the possibility, even remote, of an infinite loop :)
fi
done < some_fifo #reading from the named pipe


Hope it can help you






share|improve this answer
























  • Thank you very much for your input Andre! Your guess is right. It's just command to start java program with load of arguments and then it throws everything to dev null. For the first one I'm not getting any error. Let's just say I declare variable $prog=someProg and when I use the command.. ps -ef | grep $prog ..as expected it should do this.. ps -ef| grep someProg But it is literally grepping for $prog and echoes that program is not running. As obviously there's no program running with that name. Thanks once again!

    – Arun K Chow
    Nov 23 '18 at 18:44














0












0








0







I can't reproduce the error you are experiencing with the "grep $prog"...sorry.



But for the other part.




  1. I will assume that the script starting your program, the line with su, is starting something in background and that the script end by itself. If not, your example will wait indefinitely.


  2. Could be a personal preference, but when I'm using something like tail to verify lines, I use a named pipe (mkfifo).



That would give something like :



# Getting the tail in background
tail -f /path/to/$prog/log/file > some_fifo &
# Getting the tail PID
tailPID=$!

while read line; do #You don't need to modify/use IFS here.
if [[ $line == *started up and registered in* ]]; then
kill -15 $tailPID #since you know the PID you won't kill another tail
echo "$prog now online."
break # don't like the possibility, even remote, of an infinite loop :)
fi
done < some_fifo #reading from the named pipe


Hope it can help you






share|improve this answer













I can't reproduce the error you are experiencing with the "grep $prog"...sorry.



But for the other part.




  1. I will assume that the script starting your program, the line with su, is starting something in background and that the script end by itself. If not, your example will wait indefinitely.


  2. Could be a personal preference, but when I'm using something like tail to verify lines, I use a named pipe (mkfifo).



That would give something like :



# Getting the tail in background
tail -f /path/to/$prog/log/file > some_fifo &
# Getting the tail PID
tailPID=$!

while read line; do #You don't need to modify/use IFS here.
if [[ $line == *started up and registered in* ]]; then
kill -15 $tailPID #since you know the PID you won't kill another tail
echo "$prog now online."
break # don't like the possibility, even remote, of an infinite loop :)
fi
done < some_fifo #reading from the named pipe


Hope it can help you







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 23 '18 at 4:18









Andre GelinasAndre Gelinas

553149




553149













  • Thank you very much for your input Andre! Your guess is right. It's just command to start java program with load of arguments and then it throws everything to dev null. For the first one I'm not getting any error. Let's just say I declare variable $prog=someProg and when I use the command.. ps -ef | grep $prog ..as expected it should do this.. ps -ef| grep someProg But it is literally grepping for $prog and echoes that program is not running. As obviously there's no program running with that name. Thanks once again!

    – Arun K Chow
    Nov 23 '18 at 18:44



















  • Thank you very much for your input Andre! Your guess is right. It's just command to start java program with load of arguments and then it throws everything to dev null. For the first one I'm not getting any error. Let's just say I declare variable $prog=someProg and when I use the command.. ps -ef | grep $prog ..as expected it should do this.. ps -ef| grep someProg But it is literally grepping for $prog and echoes that program is not running. As obviously there's no program running with that name. Thanks once again!

    – Arun K Chow
    Nov 23 '18 at 18:44

















Thank you very much for your input Andre! Your guess is right. It's just command to start java program with load of arguments and then it throws everything to dev null. For the first one I'm not getting any error. Let's just say I declare variable $prog=someProg and when I use the command.. ps -ef | grep $prog ..as expected it should do this.. ps -ef| grep someProg But it is literally grepping for $prog and echoes that program is not running. As obviously there's no program running with that name. Thanks once again!

– Arun K Chow
Nov 23 '18 at 18:44





Thank you very much for your input Andre! Your guess is right. It's just command to start java program with load of arguments and then it throws everything to dev null. For the first one I'm not getting any error. Let's just say I declare variable $prog=someProg and when I use the command.. ps -ef | grep $prog ..as expected it should do this.. ps -ef| grep someProg But it is literally grepping for $prog and echoes that program is not running. As obviously there's no program running with that name. Thanks once again!

– Arun K Chow
Nov 23 '18 at 18:44




















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%2f53439408%2fbash-init-script-skips-reading-commands-in-if-loop%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







這個網誌中的熱門文章

Post-Redirect-Get with Spring WebFlux and Thymeleaf

Xamarin.form Move up view when keyboard appear

JBPM : POST request for execute process go wrong