Is there a way to make python subprocess not terminate after running through the script once?












0















Consider the following 2 files



script_to_start_other_script.py



import schedule
import time
import subprocess

def run_again():
subprocess.call(["bash", "-c", "" + " nohup python script_to_be_started.py > /dev/null 2>&1&"])


if __name__== "__main__":

schedule.every(5).seconds.do(run_again)

while True:
schedule.run_pending()
time.sleep(1)
pass


script_to_be_started.py



import logging
from logging.handlers import TimedRotatingFileHandler

# Init logger
logger = logging.getLogger('test_log')
hdlr = logging.handlers.TimedRotatingFileHandler('./test_log.log', when='h', interval=10)
formatter = logging.Formatter('%(asctime)s %(levelname)s : %(message)s')
hdlr.setFormatter(formatter)
logger.addHandler(hdlr)
logger.setLevel(logging.INFO)
logger.info('Beginning of test_log.py')

import schedule

def run_again():
logger.info('I am being called')

if __name__== "__main__":
schedule.every(5).seconds.do(run_again)

while True:
logger.info('how many time am I being called')
schedule.run_pending()
time.sleep(1)
pass


Whenever I run script_to_start_other_script.py , script_to_be_started.py will only run the entire script once



logger.info('how many time am I being called')  


will only print once even though there is a while loop. Is there a way for the script to keep running?










share|improve this question

























  • How are you reading the output of your spawned processes?

    – Blender
    Dec 31 '18 at 21:35













  • I use the logger from import logging

    – J L
    Dec 31 '18 at 21:37











  • Even after adding the missing imports and initializing a logger, your code produces no output. Can you edit your two scripts so that they actually run and reproduce your problem?

    – Blender
    Dec 31 '18 at 21:41











  • yeah let me give you the full script

    – J L
    Dec 31 '18 at 21:45
















0















Consider the following 2 files



script_to_start_other_script.py



import schedule
import time
import subprocess

def run_again():
subprocess.call(["bash", "-c", "" + " nohup python script_to_be_started.py > /dev/null 2>&1&"])


if __name__== "__main__":

schedule.every(5).seconds.do(run_again)

while True:
schedule.run_pending()
time.sleep(1)
pass


script_to_be_started.py



import logging
from logging.handlers import TimedRotatingFileHandler

# Init logger
logger = logging.getLogger('test_log')
hdlr = logging.handlers.TimedRotatingFileHandler('./test_log.log', when='h', interval=10)
formatter = logging.Formatter('%(asctime)s %(levelname)s : %(message)s')
hdlr.setFormatter(formatter)
logger.addHandler(hdlr)
logger.setLevel(logging.INFO)
logger.info('Beginning of test_log.py')

import schedule

def run_again():
logger.info('I am being called')

if __name__== "__main__":
schedule.every(5).seconds.do(run_again)

while True:
logger.info('how many time am I being called')
schedule.run_pending()
time.sleep(1)
pass


Whenever I run script_to_start_other_script.py , script_to_be_started.py will only run the entire script once



logger.info('how many time am I being called')  


will only print once even though there is a while loop. Is there a way for the script to keep running?










share|improve this question

























  • How are you reading the output of your spawned processes?

    – Blender
    Dec 31 '18 at 21:35













  • I use the logger from import logging

    – J L
    Dec 31 '18 at 21:37











  • Even after adding the missing imports and initializing a logger, your code produces no output. Can you edit your two scripts so that they actually run and reproduce your problem?

    – Blender
    Dec 31 '18 at 21:41











  • yeah let me give you the full script

    – J L
    Dec 31 '18 at 21:45














0












0








0








Consider the following 2 files



script_to_start_other_script.py



import schedule
import time
import subprocess

def run_again():
subprocess.call(["bash", "-c", "" + " nohup python script_to_be_started.py > /dev/null 2>&1&"])


if __name__== "__main__":

schedule.every(5).seconds.do(run_again)

while True:
schedule.run_pending()
time.sleep(1)
pass


script_to_be_started.py



import logging
from logging.handlers import TimedRotatingFileHandler

# Init logger
logger = logging.getLogger('test_log')
hdlr = logging.handlers.TimedRotatingFileHandler('./test_log.log', when='h', interval=10)
formatter = logging.Formatter('%(asctime)s %(levelname)s : %(message)s')
hdlr.setFormatter(formatter)
logger.addHandler(hdlr)
logger.setLevel(logging.INFO)
logger.info('Beginning of test_log.py')

import schedule

def run_again():
logger.info('I am being called')

if __name__== "__main__":
schedule.every(5).seconds.do(run_again)

while True:
logger.info('how many time am I being called')
schedule.run_pending()
time.sleep(1)
pass


Whenever I run script_to_start_other_script.py , script_to_be_started.py will only run the entire script once



logger.info('how many time am I being called')  


will only print once even though there is a while loop. Is there a way for the script to keep running?










share|improve this question
















Consider the following 2 files



script_to_start_other_script.py



import schedule
import time
import subprocess

def run_again():
subprocess.call(["bash", "-c", "" + " nohup python script_to_be_started.py > /dev/null 2>&1&"])


if __name__== "__main__":

schedule.every(5).seconds.do(run_again)

while True:
schedule.run_pending()
time.sleep(1)
pass


script_to_be_started.py



import logging
from logging.handlers import TimedRotatingFileHandler

# Init logger
logger = logging.getLogger('test_log')
hdlr = logging.handlers.TimedRotatingFileHandler('./test_log.log', when='h', interval=10)
formatter = logging.Formatter('%(asctime)s %(levelname)s : %(message)s')
hdlr.setFormatter(formatter)
logger.addHandler(hdlr)
logger.setLevel(logging.INFO)
logger.info('Beginning of test_log.py')

import schedule

def run_again():
logger.info('I am being called')

if __name__== "__main__":
schedule.every(5).seconds.do(run_again)

while True:
logger.info('how many time am I being called')
schedule.run_pending()
time.sleep(1)
pass


Whenever I run script_to_start_other_script.py , script_to_be_started.py will only run the entire script once



logger.info('how many time am I being called')  


will only print once even though there is a while loop. Is there a way for the script to keep running?







python subprocess






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 31 '18 at 21:46







J L

















asked Dec 31 '18 at 21:08









J LJ L

497




497













  • How are you reading the output of your spawned processes?

    – Blender
    Dec 31 '18 at 21:35













  • I use the logger from import logging

    – J L
    Dec 31 '18 at 21:37











  • Even after adding the missing imports and initializing a logger, your code produces no output. Can you edit your two scripts so that they actually run and reproduce your problem?

    – Blender
    Dec 31 '18 at 21:41











  • yeah let me give you the full script

    – J L
    Dec 31 '18 at 21:45



















  • How are you reading the output of your spawned processes?

    – Blender
    Dec 31 '18 at 21:35













  • I use the logger from import logging

    – J L
    Dec 31 '18 at 21:37











  • Even after adding the missing imports and initializing a logger, your code produces no output. Can you edit your two scripts so that they actually run and reproduce your problem?

    – Blender
    Dec 31 '18 at 21:41











  • yeah let me give you the full script

    – J L
    Dec 31 '18 at 21:45

















How are you reading the output of your spawned processes?

– Blender
Dec 31 '18 at 21:35







How are you reading the output of your spawned processes?

– Blender
Dec 31 '18 at 21:35















I use the logger from import logging

– J L
Dec 31 '18 at 21:37





I use the logger from import logging

– J L
Dec 31 '18 at 21:37













Even after adding the missing imports and initializing a logger, your code produces no output. Can you edit your two scripts so that they actually run and reproduce your problem?

– Blender
Dec 31 '18 at 21:41





Even after adding the missing imports and initializing a logger, your code produces no output. Can you edit your two scripts so that they actually run and reproduce your problem?

– Blender
Dec 31 '18 at 21:41













yeah let me give you the full script

– J L
Dec 31 '18 at 21:45





yeah let me give you the full script

– J L
Dec 31 '18 at 21:45












1 Answer
1






active

oldest

votes


















1














I tried you first script and it continuously ran the second one. Try to just run the script_to_be_started.py and make sure that it runs fine. One reason that the second script ran until the log statement might be the missing import for time.



import time



So, after printing the log message, the second script will silently crash because of missing import.



I am assuming you only stripped down the logging stuff but the missing import for time is actually part of your code.






share|improve this answer
























  • yup missing import time is the problem, thanks

    – J L
    Dec 31 '18 at 21:48











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%2f53991435%2fis-there-a-way-to-make-python-subprocess-not-terminate-after-running-through-the%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









1














I tried you first script and it continuously ran the second one. Try to just run the script_to_be_started.py and make sure that it runs fine. One reason that the second script ran until the log statement might be the missing import for time.



import time



So, after printing the log message, the second script will silently crash because of missing import.



I am assuming you only stripped down the logging stuff but the missing import for time is actually part of your code.






share|improve this answer
























  • yup missing import time is the problem, thanks

    – J L
    Dec 31 '18 at 21:48
















1














I tried you first script and it continuously ran the second one. Try to just run the script_to_be_started.py and make sure that it runs fine. One reason that the second script ran until the log statement might be the missing import for time.



import time



So, after printing the log message, the second script will silently crash because of missing import.



I am assuming you only stripped down the logging stuff but the missing import for time is actually part of your code.






share|improve this answer
























  • yup missing import time is the problem, thanks

    – J L
    Dec 31 '18 at 21:48














1












1








1







I tried you first script and it continuously ran the second one. Try to just run the script_to_be_started.py and make sure that it runs fine. One reason that the second script ran until the log statement might be the missing import for time.



import time



So, after printing the log message, the second script will silently crash because of missing import.



I am assuming you only stripped down the logging stuff but the missing import for time is actually part of your code.






share|improve this answer













I tried you first script and it continuously ran the second one. Try to just run the script_to_be_started.py and make sure that it runs fine. One reason that the second script ran until the log statement might be the missing import for time.



import time



So, after printing the log message, the second script will silently crash because of missing import.



I am assuming you only stripped down the logging stuff but the missing import for time is actually part of your code.







share|improve this answer












share|improve this answer



share|improve this answer










answered Dec 31 '18 at 21:43









faisalfaisal

1836




1836













  • yup missing import time is the problem, thanks

    – J L
    Dec 31 '18 at 21:48



















  • yup missing import time is the problem, thanks

    – J L
    Dec 31 '18 at 21:48

















yup missing import time is the problem, thanks

– J L
Dec 31 '18 at 21:48





yup missing import time is the problem, thanks

– J L
Dec 31 '18 at 21:48




















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%2f53991435%2fis-there-a-way-to-make-python-subprocess-not-terminate-after-running-through-the%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

Monofisismo

Angular Downloading a file using contenturl with Basic Authentication

Olmecas