Opening interactive python shell after running python script in backgroundRun script in a non interactive...

How to compare two different formulations of a problem?

How much code would a codegolf golf if a codegolf could golf code?

Do I have to learn /o/ or /ɔ/ separately?

Why does my house heat up, even when it's cool outside?

Most practical knots for hitching a line to an object while keeping the bitter end as tight as possible, without sag?

What is "Wayfinder's Guide to Eberron"?

Is refusing to concede in the face of an unstoppable Nexus combo punishable?

Is it insecure to have an ansible user with passwordless sudo?

What happens when I copy a legendary creature with Rite of Replication?

Why were movies shot on film shot at 24 frames per second?

The logic of invoking virtual functions is not clear (or it is method hiding?)

Vacuum collapse -- why do strong metals implode but glass doesn't?

Is there a known non-euclidean geometry where two concentric circles of different radii can intersect? (as in the novel "The Universe Between")

Shouldn't the "credit score" prevent Americans from going deeper and deeper into personal debt?

Why my earth simulation is slower than the reality?

Does Git delete empty folders?

Thread-safe, Convenient and Performant Random Number Generator

Why doesn't the Falcon-9 first stage use three legs to land?

Have only girls been born for a long time in this village?

How to create a summation symbol with a vertical bar?

What is the evidence on the danger of feeding whole blueberries and grapes to infants and toddlers?

Was 'help' pronounced starting with a vowel sound?

What can I do to keep a threaded bolt from falling out of its slot?

The sound of thunder's like a whip



Opening interactive python shell after running python script in background


Run script in a non interactive shell?Start a background process from a script and manage it when the script endsWhy does my Python background process end when SSH session is terminated?Can't open terminal because of settingsStarting an interactive shell as an asynchronous process (signal delivery)Can an interactive shell become non-interactive or vice versa?Why putting a background job into a script can make the job survive termination of the script and the caller of the script?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}







1















python3 -i test.py opens an interactive python shell after running test.py. However, if I try to run it in the background with python3 -i test.py &
the job stops automatically with a ^C and shows



[4]+  Stopped                 python3 -i test.py


, and I can't access python's interactive shell (with the variables from test.py still in the environment) afterwards. fging the process (i.e. fg %4) leads to an interactive shell where my input can't be seen but is still run after pressing <Enter>. How do I run the interactive shell "normally" after running test.py in the background?



(For reference, test.py contains



from time import sleep
a = 'hello world'
for i in range(10):
sleep(1)
print(a)


and my shell looks like this:



$ python3 -i test.py &
[4] 6708
$ hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
fg %4
python3 -i test.py
>>> 'hello world'
>>>


and I typed a after being prompted by the first >>>, but it isn't shown.
)



-- Edit @muru --



Sending it to the bg after running it normally in the fg gives:



$
$
$ python3 -i test.py
hello world
hello world
hello world
hello world
^Z
[4]+ Stopped python3 -i test.py
$ bg %4
[4]+ python3 -i test.py &
$ hello world
hello world
hello world
hello world
hello world
hello world
echo 'hello world'
hello world

[4]+ Stopped python3 -i test.py
$
$


where shell was expecting input and I typed echo 'hello world' after the 10 "Hello World"'s.










share|improve this question









New contributor



Anonymous is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






















  • Why would an interactive process be run in the background?

    – muru
    2 days ago











  • I want to run a python file that takes quite some time, and do other things with the same terminal in the meantime, but still examine the script’s environment (interactively) when it’s done running.

    – Anonymous
    2 days ago











  • Try sending it to the background after starting it normally (Ctrl-Z then bg).

    – muru
    2 days ago


















1















python3 -i test.py opens an interactive python shell after running test.py. However, if I try to run it in the background with python3 -i test.py &
the job stops automatically with a ^C and shows



[4]+  Stopped                 python3 -i test.py


, and I can't access python's interactive shell (with the variables from test.py still in the environment) afterwards. fging the process (i.e. fg %4) leads to an interactive shell where my input can't be seen but is still run after pressing <Enter>. How do I run the interactive shell "normally" after running test.py in the background?



(For reference, test.py contains



from time import sleep
a = 'hello world'
for i in range(10):
sleep(1)
print(a)


and my shell looks like this:



$ python3 -i test.py &
[4] 6708
$ hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
fg %4
python3 -i test.py
>>> 'hello world'
>>>


and I typed a after being prompted by the first >>>, but it isn't shown.
)



-- Edit @muru --



Sending it to the bg after running it normally in the fg gives:



$
$
$ python3 -i test.py
hello world
hello world
hello world
hello world
^Z
[4]+ Stopped python3 -i test.py
$ bg %4
[4]+ python3 -i test.py &
$ hello world
hello world
hello world
hello world
hello world
hello world
echo 'hello world'
hello world

[4]+ Stopped python3 -i test.py
$
$


where shell was expecting input and I typed echo 'hello world' after the 10 "Hello World"'s.










share|improve this question









New contributor



Anonymous is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






















  • Why would an interactive process be run in the background?

    – muru
    2 days ago











  • I want to run a python file that takes quite some time, and do other things with the same terminal in the meantime, but still examine the script’s environment (interactively) when it’s done running.

    – Anonymous
    2 days ago











  • Try sending it to the background after starting it normally (Ctrl-Z then bg).

    – muru
    2 days ago














1












1








1


0






python3 -i test.py opens an interactive python shell after running test.py. However, if I try to run it in the background with python3 -i test.py &
the job stops automatically with a ^C and shows



[4]+  Stopped                 python3 -i test.py


, and I can't access python's interactive shell (with the variables from test.py still in the environment) afterwards. fging the process (i.e. fg %4) leads to an interactive shell where my input can't be seen but is still run after pressing <Enter>. How do I run the interactive shell "normally" after running test.py in the background?



(For reference, test.py contains



from time import sleep
a = 'hello world'
for i in range(10):
sleep(1)
print(a)


and my shell looks like this:



$ python3 -i test.py &
[4] 6708
$ hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
fg %4
python3 -i test.py
>>> 'hello world'
>>>


and I typed a after being prompted by the first >>>, but it isn't shown.
)



-- Edit @muru --



Sending it to the bg after running it normally in the fg gives:



$
$
$ python3 -i test.py
hello world
hello world
hello world
hello world
^Z
[4]+ Stopped python3 -i test.py
$ bg %4
[4]+ python3 -i test.py &
$ hello world
hello world
hello world
hello world
hello world
hello world
echo 'hello world'
hello world

[4]+ Stopped python3 -i test.py
$
$


where shell was expecting input and I typed echo 'hello world' after the 10 "Hello World"'s.










share|improve this question









New contributor



Anonymous is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











python3 -i test.py opens an interactive python shell after running test.py. However, if I try to run it in the background with python3 -i test.py &
the job stops automatically with a ^C and shows



[4]+  Stopped                 python3 -i test.py


, and I can't access python's interactive shell (with the variables from test.py still in the environment) afterwards. fging the process (i.e. fg %4) leads to an interactive shell where my input can't be seen but is still run after pressing <Enter>. How do I run the interactive shell "normally" after running test.py in the background?



(For reference, test.py contains



from time import sleep
a = 'hello world'
for i in range(10):
sleep(1)
print(a)


and my shell looks like this:



$ python3 -i test.py &
[4] 6708
$ hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
fg %4
python3 -i test.py
>>> 'hello world'
>>>


and I typed a after being prompted by the first >>>, but it isn't shown.
)



-- Edit @muru --



Sending it to the bg after running it normally in the fg gives:



$
$
$ python3 -i test.py
hello world
hello world
hello world
hello world
^Z
[4]+ Stopped python3 -i test.py
$ bg %4
[4]+ python3 -i test.py &
$ hello world
hello world
hello world
hello world
hello world
hello world
echo 'hello world'
hello world

[4]+ Stopped python3 -i test.py
$
$


where shell was expecting input and I typed echo 'hello world' after the 10 "Hello World"'s.







shell python background-process jobs interactive






share|improve this question









New contributor



Anonymous is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.










share|improve this question









New contributor



Anonymous is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.








share|improve this question




share|improve this question








edited 13 hours ago







Anonymous













New contributor



Anonymous is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.








asked 2 days ago









AnonymousAnonymous

83 bronze badges




83 bronze badges




New contributor



Anonymous is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




New contributor




Anonymous is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.


















  • Why would an interactive process be run in the background?

    – muru
    2 days ago











  • I want to run a python file that takes quite some time, and do other things with the same terminal in the meantime, but still examine the script’s environment (interactively) when it’s done running.

    – Anonymous
    2 days ago











  • Try sending it to the background after starting it normally (Ctrl-Z then bg).

    – muru
    2 days ago



















  • Why would an interactive process be run in the background?

    – muru
    2 days ago











  • I want to run a python file that takes quite some time, and do other things with the same terminal in the meantime, but still examine the script’s environment (interactively) when it’s done running.

    – Anonymous
    2 days ago











  • Try sending it to the background after starting it normally (Ctrl-Z then bg).

    – muru
    2 days ago

















Why would an interactive process be run in the background?

– muru
2 days ago





Why would an interactive process be run in the background?

– muru
2 days ago













I want to run a python file that takes quite some time, and do other things with the same terminal in the meantime, but still examine the script’s environment (interactively) when it’s done running.

– Anonymous
2 days ago





I want to run a python file that takes quite some time, and do other things with the same terminal in the meantime, but still examine the script’s environment (interactively) when it’s done running.

– Anonymous
2 days ago













Try sending it to the background after starting it normally (Ctrl-Z then bg).

– muru
2 days ago





Try sending it to the background after starting it normally (Ctrl-Z then bg).

– muru
2 days ago










1 Answer
1






active

oldest

votes


















0














That happens because the Python interpreter in background "competes" the prompt with the command-line shell as soon as it finishes executing the test.py program.



Naturally Python loses that "battle" because it is not the one allowed to go interactive at that moment. However it gets stopped a bit too late, enough to leave its own prompt in a state that will not resume cleanly on an fg command.



One way to settle this is by adding the following lines at the end of your Python program:



import os, signal  # access os-level functions and UNIX-signals
os.kill(os.getpid(), signal.SIGSTOP) # send myself the UNIX SIGSTOP signal


(Of course the import can also be placed on top of your program as per typical practice.)



That os.kill() line will put the Python interpreter in stopped state, just like it gets when it tries to go interactive at the wrong moment. Only, this time it does it itself, before even attempting to prompt, so that it is not left in an inconsistent state.



You know when that os.kill() is reached because the command-line shell notifies you that Python got Stopped. An fg at that moment will resume Python making it proceed from the os.kill() line, thus starting its own interactive session.



Don't use bg to resume it after it got Stopped by that os.kill(), because doing so will only make the kernel stop Python again for attempting to go interactive while in background.






share|improve this answer






























    Your Answer








    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "106"
    };
    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: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    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
    });


    }
    });






    Anonymous is a new contributor. Be nice, and check out our Code of Conduct.










    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f536119%2fopening-interactive-python-shell-after-running-python-script-in-background%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














    That happens because the Python interpreter in background "competes" the prompt with the command-line shell as soon as it finishes executing the test.py program.



    Naturally Python loses that "battle" because it is not the one allowed to go interactive at that moment. However it gets stopped a bit too late, enough to leave its own prompt in a state that will not resume cleanly on an fg command.



    One way to settle this is by adding the following lines at the end of your Python program:



    import os, signal  # access os-level functions and UNIX-signals
    os.kill(os.getpid(), signal.SIGSTOP) # send myself the UNIX SIGSTOP signal


    (Of course the import can also be placed on top of your program as per typical practice.)



    That os.kill() line will put the Python interpreter in stopped state, just like it gets when it tries to go interactive at the wrong moment. Only, this time it does it itself, before even attempting to prompt, so that it is not left in an inconsistent state.



    You know when that os.kill() is reached because the command-line shell notifies you that Python got Stopped. An fg at that moment will resume Python making it proceed from the os.kill() line, thus starting its own interactive session.



    Don't use bg to resume it after it got Stopped by that os.kill(), because doing so will only make the kernel stop Python again for attempting to go interactive while in background.






    share|improve this answer
































      0














      That happens because the Python interpreter in background "competes" the prompt with the command-line shell as soon as it finishes executing the test.py program.



      Naturally Python loses that "battle" because it is not the one allowed to go interactive at that moment. However it gets stopped a bit too late, enough to leave its own prompt in a state that will not resume cleanly on an fg command.



      One way to settle this is by adding the following lines at the end of your Python program:



      import os, signal  # access os-level functions and UNIX-signals
      os.kill(os.getpid(), signal.SIGSTOP) # send myself the UNIX SIGSTOP signal


      (Of course the import can also be placed on top of your program as per typical practice.)



      That os.kill() line will put the Python interpreter in stopped state, just like it gets when it tries to go interactive at the wrong moment. Only, this time it does it itself, before even attempting to prompt, so that it is not left in an inconsistent state.



      You know when that os.kill() is reached because the command-line shell notifies you that Python got Stopped. An fg at that moment will resume Python making it proceed from the os.kill() line, thus starting its own interactive session.



      Don't use bg to resume it after it got Stopped by that os.kill(), because doing so will only make the kernel stop Python again for attempting to go interactive while in background.






      share|improve this answer






























        0












        0








        0







        That happens because the Python interpreter in background "competes" the prompt with the command-line shell as soon as it finishes executing the test.py program.



        Naturally Python loses that "battle" because it is not the one allowed to go interactive at that moment. However it gets stopped a bit too late, enough to leave its own prompt in a state that will not resume cleanly on an fg command.



        One way to settle this is by adding the following lines at the end of your Python program:



        import os, signal  # access os-level functions and UNIX-signals
        os.kill(os.getpid(), signal.SIGSTOP) # send myself the UNIX SIGSTOP signal


        (Of course the import can also be placed on top of your program as per typical practice.)



        That os.kill() line will put the Python interpreter in stopped state, just like it gets when it tries to go interactive at the wrong moment. Only, this time it does it itself, before even attempting to prompt, so that it is not left in an inconsistent state.



        You know when that os.kill() is reached because the command-line shell notifies you that Python got Stopped. An fg at that moment will resume Python making it proceed from the os.kill() line, thus starting its own interactive session.



        Don't use bg to resume it after it got Stopped by that os.kill(), because doing so will only make the kernel stop Python again for attempting to go interactive while in background.






        share|improve this answer















        That happens because the Python interpreter in background "competes" the prompt with the command-line shell as soon as it finishes executing the test.py program.



        Naturally Python loses that "battle" because it is not the one allowed to go interactive at that moment. However it gets stopped a bit too late, enough to leave its own prompt in a state that will not resume cleanly on an fg command.



        One way to settle this is by adding the following lines at the end of your Python program:



        import os, signal  # access os-level functions and UNIX-signals
        os.kill(os.getpid(), signal.SIGSTOP) # send myself the UNIX SIGSTOP signal


        (Of course the import can also be placed on top of your program as per typical practice.)



        That os.kill() line will put the Python interpreter in stopped state, just like it gets when it tries to go interactive at the wrong moment. Only, this time it does it itself, before even attempting to prompt, so that it is not left in an inconsistent state.



        You know when that os.kill() is reached because the command-line shell notifies you that Python got Stopped. An fg at that moment will resume Python making it proceed from the os.kill() line, thus starting its own interactive session.



        Don't use bg to resume it after it got Stopped by that os.kill(), because doing so will only make the kernel stop Python again for attempting to go interactive while in background.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited 13 hours ago

























        answered yesterday









        LL3LL3

        2,1621 silver badge14 bronze badges




        2,1621 silver badge14 bronze badges

























            Anonymous is a new contributor. Be nice, and check out our Code of Conduct.










            draft saved

            draft discarded


















            Anonymous is a new contributor. Be nice, and check out our Code of Conduct.













            Anonymous is a new contributor. Be nice, and check out our Code of Conduct.












            Anonymous is a new contributor. Be nice, and check out our Code of Conduct.
















            Thanks for contributing an answer to Unix & Linux Stack Exchange!


            • 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%2funix.stackexchange.com%2fquestions%2f536119%2fopening-interactive-python-shell-after-running-python-script-in-background%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

            Hudson River Historic District Contents Geography History The district today Aesthetics Cultural...

            The number designs the writing. Feandra Aversely Definition: The act of ingrafting a sprig or shoot of one...

            Ayherre Geografie Demografie Externe links Navigatiemenu43° 23′ NB, 1° 15′ WL43° 23′ NB, 1°...