Bash script “listen” to output from an infinitely looping python script












0














I've written a python script to check the status of the smoke detectors in my house. it runs every second and returns a value of 0,1,2. It uses a separate function on its own thread to recognise the difference between low battery and alarm beeps.



I need a bash script to listen for these conditions and send a netcat packet to my main server if one of the conditions for an alert is met.



I tried using



alertvalue = `python alarm.py`


and running that in a while loop, this echos nothing.



I thought of redirecting the python output to a file but some reading said that it would fail if the script wrote to the file at the same time the bash script would attempt to read from it.



I'm wondering if I can send and receive netcat information directly within the python script?



Could you point me in the right direction here?










share|improve this question
























  • python alarm.py | while read -r; do alertvalue="$REPLY"; ... done?
    – melpomene
    Nov 19 '18 at 0:16






  • 1




    You might not need bash or netcat. Check out python sockets.
    – Cole Tierney
    Nov 19 '18 at 0:33










  • @melpomene Just tried it. Still returning nothing. In the python script the output is produced with the print function if it helps.
    – Scu11y
    Nov 19 '18 at 1:13
















0














I've written a python script to check the status of the smoke detectors in my house. it runs every second and returns a value of 0,1,2. It uses a separate function on its own thread to recognise the difference between low battery and alarm beeps.



I need a bash script to listen for these conditions and send a netcat packet to my main server if one of the conditions for an alert is met.



I tried using



alertvalue = `python alarm.py`


and running that in a while loop, this echos nothing.



I thought of redirecting the python output to a file but some reading said that it would fail if the script wrote to the file at the same time the bash script would attempt to read from it.



I'm wondering if I can send and receive netcat information directly within the python script?



Could you point me in the right direction here?










share|improve this question
























  • python alarm.py | while read -r; do alertvalue="$REPLY"; ... done?
    – melpomene
    Nov 19 '18 at 0:16






  • 1




    You might not need bash or netcat. Check out python sockets.
    – Cole Tierney
    Nov 19 '18 at 0:33










  • @melpomene Just tried it. Still returning nothing. In the python script the output is produced with the print function if it helps.
    – Scu11y
    Nov 19 '18 at 1:13














0












0








0







I've written a python script to check the status of the smoke detectors in my house. it runs every second and returns a value of 0,1,2. It uses a separate function on its own thread to recognise the difference between low battery and alarm beeps.



I need a bash script to listen for these conditions and send a netcat packet to my main server if one of the conditions for an alert is met.



I tried using



alertvalue = `python alarm.py`


and running that in a while loop, this echos nothing.



I thought of redirecting the python output to a file but some reading said that it would fail if the script wrote to the file at the same time the bash script would attempt to read from it.



I'm wondering if I can send and receive netcat information directly within the python script?



Could you point me in the right direction here?










share|improve this question















I've written a python script to check the status of the smoke detectors in my house. it runs every second and returns a value of 0,1,2. It uses a separate function on its own thread to recognise the difference between low battery and alarm beeps.



I need a bash script to listen for these conditions and send a netcat packet to my main server if one of the conditions for an alert is met.



I tried using



alertvalue = `python alarm.py`


and running that in a while loop, this echos nothing.



I thought of redirecting the python output to a file but some reading said that it would fail if the script wrote to the file at the same time the bash script would attempt to read from it.



I'm wondering if I can send and receive netcat information directly within the python script?



Could you point me in the right direction here?







python bash






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 19 '18 at 0:15









melpomene

59k54489




59k54489










asked Nov 18 '18 at 23:47









Scu11yScu11y

53




53












  • python alarm.py | while read -r; do alertvalue="$REPLY"; ... done?
    – melpomene
    Nov 19 '18 at 0:16






  • 1




    You might not need bash or netcat. Check out python sockets.
    – Cole Tierney
    Nov 19 '18 at 0:33










  • @melpomene Just tried it. Still returning nothing. In the python script the output is produced with the print function if it helps.
    – Scu11y
    Nov 19 '18 at 1:13


















  • python alarm.py | while read -r; do alertvalue="$REPLY"; ... done?
    – melpomene
    Nov 19 '18 at 0:16






  • 1




    You might not need bash or netcat. Check out python sockets.
    – Cole Tierney
    Nov 19 '18 at 0:33










  • @melpomene Just tried it. Still returning nothing. In the python script the output is produced with the print function if it helps.
    – Scu11y
    Nov 19 '18 at 1:13
















python alarm.py | while read -r; do alertvalue="$REPLY"; ... done?
– melpomene
Nov 19 '18 at 0:16




python alarm.py | while read -r; do alertvalue="$REPLY"; ... done?
– melpomene
Nov 19 '18 at 0:16




1




1




You might not need bash or netcat. Check out python sockets.
– Cole Tierney
Nov 19 '18 at 0:33




You might not need bash or netcat. Check out python sockets.
– Cole Tierney
Nov 19 '18 at 0:33












@melpomene Just tried it. Still returning nothing. In the python script the output is produced with the print function if it helps.
– Scu11y
Nov 19 '18 at 1:13




@melpomene Just tried it. Still returning nothing. In the python script the output is produced with the print function if it helps.
– Scu11y
Nov 19 '18 at 1:13












0






active

oldest

votes











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%2f53366613%2fbash-script-listen-to-output-from-an-infinitely-looping-python-script%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















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%2f53366613%2fbash-script-listen-to-output-from-an-infinitely-looping-python-script%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

How to change which sound is reproduced for terminal bell?

Can I use Tabulator js library in my java Spring + Thymeleaf project?

Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents