Is there a way to make python subprocess not terminate after running through the script once?
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
add a comment |
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
How are you reading the output of your spawned processes?
– Blender
Dec 31 '18 at 21:35
I use the logger fromimport logging
– J L
Dec 31 '18 at 21:37
Even after adding the missingimport
s 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
add a comment |
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
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
python subprocess
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 fromimport logging
– J L
Dec 31 '18 at 21:37
Even after adding the missingimport
s 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
add a comment |
How are you reading the output of your spawned processes?
– Blender
Dec 31 '18 at 21:35
I use the logger fromimport logging
– J L
Dec 31 '18 at 21:37
Even after adding the missingimport
s 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
import
s 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
import
s 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
add a comment |
1 Answer
1
active
oldest
votes
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.
yup missing import time is the problem, thanks
– J L
Dec 31 '18 at 21:48
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%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
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.
yup missing import time is the problem, thanks
– J L
Dec 31 '18 at 21:48
add a comment |
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.
yup missing import time is the problem, thanks
– J L
Dec 31 '18 at 21:48
add a comment |
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.
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.
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
add a comment |
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
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%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
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
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
import
s 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