Using built-in commands from running process in script





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















So lately I've been trying to quit a running process in a clean manner, through its 'quit' command.
If I don't go through the entire quitting process it creates a .lck file and I don't want that.



I've been unable to succeed, I've tried multiple approaches such as echoing 'quit' and 15, 12, n to /proc/pid/fd/0, but none of these managed to validate what was in the buffer.
I've tried to screen a terminal, and reptyr the process into it so it goes into a terminal but doing so I can't find a way to emulate the enter key.



So, since it seems that no matter what I try I can't find how to validate a text buffer, I decided that I should use that built-in quit directly and not trying to input it.



Is there ways to use another process commands directly ? When I try



quit | reptyr <process pid> 


for instance, it says that quit isn't a recognized command, because the shell is trying to execute it in its own environment and I wish to execute it in the other process environment.



NOTE: Some info about said running process, I have to provide several input to quit it.





  • press Ctlr + C to stop it
    It then swap to SimDebug mode which just provides infos should I press enter a few times. It is in read mode looking like



    SimDebug>



  • There I should write quit and press enter, then q and enter, n and enter then lastly enter.



I manage in my script to do the 1st point by using kill -INT and I'm stuck with the rest.



EDIT : I can skip some of those task using the SIGTERM as suggested, although it gets me to the last read




Do you want to do a checkpoint (Y/N)
_




And there I can't do another SIGTERM, a SIGKILL pops up an error message and a .lck file is created. I only do need to feed a "n" or "y" inside it.
I will take a look at the expect since now I have the question but i'm still wondering how to use it to be listening to the process and not the terminal in which i'm running the script.










share|improve this question

























  • echo quit | reptyr will send quit as standard input to reptyr. Making it quit immediately sounds like a weird thing to do, though.

    – tripleee
    Mar 8 at 9:40






  • 3





    For general approach research expect. See this example.

    – Kamil Maciorowski
    Mar 8 at 9:40











  • I'm not trying to quit reptyr but the process inside.

    – Choup
    Mar 8 at 9:40











  • I'm not sure expect would be appropriate here since I don't have any expectation, it's just a console in read mode without any prior questions.

    – Choup
    Mar 8 at 9:45






  • 2





    I've been trying to quit a running process in a clean manner – Well writen programs quit gracefully on SIGTERM (as opposed to SIGKILL).

    – Kamil Maciorowski
    Mar 8 at 9:51


















0















So lately I've been trying to quit a running process in a clean manner, through its 'quit' command.
If I don't go through the entire quitting process it creates a .lck file and I don't want that.



I've been unable to succeed, I've tried multiple approaches such as echoing 'quit' and 15, 12, n to /proc/pid/fd/0, but none of these managed to validate what was in the buffer.
I've tried to screen a terminal, and reptyr the process into it so it goes into a terminal but doing so I can't find a way to emulate the enter key.



So, since it seems that no matter what I try I can't find how to validate a text buffer, I decided that I should use that built-in quit directly and not trying to input it.



Is there ways to use another process commands directly ? When I try



quit | reptyr <process pid> 


for instance, it says that quit isn't a recognized command, because the shell is trying to execute it in its own environment and I wish to execute it in the other process environment.



NOTE: Some info about said running process, I have to provide several input to quit it.





  • press Ctlr + C to stop it
    It then swap to SimDebug mode which just provides infos should I press enter a few times. It is in read mode looking like



    SimDebug>



  • There I should write quit and press enter, then q and enter, n and enter then lastly enter.



I manage in my script to do the 1st point by using kill -INT and I'm stuck with the rest.



EDIT : I can skip some of those task using the SIGTERM as suggested, although it gets me to the last read




Do you want to do a checkpoint (Y/N)
_




And there I can't do another SIGTERM, a SIGKILL pops up an error message and a .lck file is created. I only do need to feed a "n" or "y" inside it.
I will take a look at the expect since now I have the question but i'm still wondering how to use it to be listening to the process and not the terminal in which i'm running the script.










share|improve this question

























  • echo quit | reptyr will send quit as standard input to reptyr. Making it quit immediately sounds like a weird thing to do, though.

    – tripleee
    Mar 8 at 9:40






  • 3





    For general approach research expect. See this example.

    – Kamil Maciorowski
    Mar 8 at 9:40











  • I'm not trying to quit reptyr but the process inside.

    – Choup
    Mar 8 at 9:40











  • I'm not sure expect would be appropriate here since I don't have any expectation, it's just a console in read mode without any prior questions.

    – Choup
    Mar 8 at 9:45






  • 2





    I've been trying to quit a running process in a clean manner – Well writen programs quit gracefully on SIGTERM (as opposed to SIGKILL).

    – Kamil Maciorowski
    Mar 8 at 9:51














0












0








0


0






So lately I've been trying to quit a running process in a clean manner, through its 'quit' command.
If I don't go through the entire quitting process it creates a .lck file and I don't want that.



I've been unable to succeed, I've tried multiple approaches such as echoing 'quit' and 15, 12, n to /proc/pid/fd/0, but none of these managed to validate what was in the buffer.
I've tried to screen a terminal, and reptyr the process into it so it goes into a terminal but doing so I can't find a way to emulate the enter key.



So, since it seems that no matter what I try I can't find how to validate a text buffer, I decided that I should use that built-in quit directly and not trying to input it.



Is there ways to use another process commands directly ? When I try



quit | reptyr <process pid> 


for instance, it says that quit isn't a recognized command, because the shell is trying to execute it in its own environment and I wish to execute it in the other process environment.



NOTE: Some info about said running process, I have to provide several input to quit it.





  • press Ctlr + C to stop it
    It then swap to SimDebug mode which just provides infos should I press enter a few times. It is in read mode looking like



    SimDebug>



  • There I should write quit and press enter, then q and enter, n and enter then lastly enter.



I manage in my script to do the 1st point by using kill -INT and I'm stuck with the rest.



EDIT : I can skip some of those task using the SIGTERM as suggested, although it gets me to the last read




Do you want to do a checkpoint (Y/N)
_




And there I can't do another SIGTERM, a SIGKILL pops up an error message and a .lck file is created. I only do need to feed a "n" or "y" inside it.
I will take a look at the expect since now I have the question but i'm still wondering how to use it to be listening to the process and not the terminal in which i'm running the script.










share|improve this question
















So lately I've been trying to quit a running process in a clean manner, through its 'quit' command.
If I don't go through the entire quitting process it creates a .lck file and I don't want that.



I've been unable to succeed, I've tried multiple approaches such as echoing 'quit' and 15, 12, n to /proc/pid/fd/0, but none of these managed to validate what was in the buffer.
I've tried to screen a terminal, and reptyr the process into it so it goes into a terminal but doing so I can't find a way to emulate the enter key.



So, since it seems that no matter what I try I can't find how to validate a text buffer, I decided that I should use that built-in quit directly and not trying to input it.



Is there ways to use another process commands directly ? When I try



quit | reptyr <process pid> 


for instance, it says that quit isn't a recognized command, because the shell is trying to execute it in its own environment and I wish to execute it in the other process environment.



NOTE: Some info about said running process, I have to provide several input to quit it.





  • press Ctlr + C to stop it
    It then swap to SimDebug mode which just provides infos should I press enter a few times. It is in read mode looking like



    SimDebug>



  • There I should write quit and press enter, then q and enter, n and enter then lastly enter.



I manage in my script to do the 1st point by using kill -INT and I'm stuck with the rest.



EDIT : I can skip some of those task using the SIGTERM as suggested, although it gets me to the last read




Do you want to do a checkpoint (Y/N)
_




And there I can't do another SIGTERM, a SIGKILL pops up an error message and a .lck file is created. I only do need to feed a "n" or "y" inside it.
I will take a look at the expect since now I have the question but i'm still wondering how to use it to be listening to the process and not the terminal in which i'm running the script.







linux command-line bash process






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 8 at 10:09







Choup

















asked Mar 8 at 9:28









ChoupChoup

11




11













  • echo quit | reptyr will send quit as standard input to reptyr. Making it quit immediately sounds like a weird thing to do, though.

    – tripleee
    Mar 8 at 9:40






  • 3





    For general approach research expect. See this example.

    – Kamil Maciorowski
    Mar 8 at 9:40











  • I'm not trying to quit reptyr but the process inside.

    – Choup
    Mar 8 at 9:40











  • I'm not sure expect would be appropriate here since I don't have any expectation, it's just a console in read mode without any prior questions.

    – Choup
    Mar 8 at 9:45






  • 2





    I've been trying to quit a running process in a clean manner – Well writen programs quit gracefully on SIGTERM (as opposed to SIGKILL).

    – Kamil Maciorowski
    Mar 8 at 9:51



















  • echo quit | reptyr will send quit as standard input to reptyr. Making it quit immediately sounds like a weird thing to do, though.

    – tripleee
    Mar 8 at 9:40






  • 3





    For general approach research expect. See this example.

    – Kamil Maciorowski
    Mar 8 at 9:40











  • I'm not trying to quit reptyr but the process inside.

    – Choup
    Mar 8 at 9:40











  • I'm not sure expect would be appropriate here since I don't have any expectation, it's just a console in read mode without any prior questions.

    – Choup
    Mar 8 at 9:45






  • 2





    I've been trying to quit a running process in a clean manner – Well writen programs quit gracefully on SIGTERM (as opposed to SIGKILL).

    – Kamil Maciorowski
    Mar 8 at 9:51

















echo quit | reptyr will send quit as standard input to reptyr. Making it quit immediately sounds like a weird thing to do, though.

– tripleee
Mar 8 at 9:40





echo quit | reptyr will send quit as standard input to reptyr. Making it quit immediately sounds like a weird thing to do, though.

– tripleee
Mar 8 at 9:40




3




3





For general approach research expect. See this example.

– Kamil Maciorowski
Mar 8 at 9:40





For general approach research expect. See this example.

– Kamil Maciorowski
Mar 8 at 9:40













I'm not trying to quit reptyr but the process inside.

– Choup
Mar 8 at 9:40





I'm not trying to quit reptyr but the process inside.

– Choup
Mar 8 at 9:40













I'm not sure expect would be appropriate here since I don't have any expectation, it's just a console in read mode without any prior questions.

– Choup
Mar 8 at 9:45





I'm not sure expect would be appropriate here since I don't have any expectation, it's just a console in read mode without any prior questions.

– Choup
Mar 8 at 9:45




2




2





I've been trying to quit a running process in a clean manner – Well writen programs quit gracefully on SIGTERM (as opposed to SIGKILL).

– Kamil Maciorowski
Mar 8 at 9:51





I've been trying to quit a running process in a clean manner – Well writen programs quit gracefully on SIGTERM (as opposed to SIGKILL).

– Kamil Maciorowski
Mar 8 at 9:51










2 Answers
2






active

oldest

votes


















0














Run the program in tmux (or run reptyr in tmux to "hijack" already running process, if you need; e.g. tmux new-session reptyr "$pid_to_hijack"). Then you can do this from another terminal:



pane='$0:@0.1'
tmux send-keys -t "$pane" C-c # a way to send Ctrl+C
sleep 2 # let the program react (you may or may not need it)
tmux send-keys -t "$pane" Enter Enter Enter # "should I press enter a few times"
tmux send-keys -t "$pane" quit Enter # "I should write quit and press enter"
tmux send-keys -t "$pane" q Enter # "then q and enter"
tmux send-keys -t "$pane" n Enter # "n and enter"
tmux send-keys -t "$pane" Enter # "then lastly enter"


where the example $0:@0.1 identifies the pane in which your program runs (you must single-quote this string, otherwise the shell will expand $0). If you name your tmux session(s) and window(s), then this string may be like mysession:mywindow.0.






share|improve this answer

































    0














    As I understand you're trying to send command to a "subshell". Let's say something like running command quit() in a python shell? Or any other programs that waits for an input?



    If it's that what you want, I think you're looking for something like :



    $> python <<< "quit() python <<< "<many instructions here>" python <
    $> file_containing_instructions.py #with quit() at the end python << END
    $> <instructions> <on_many_lines> END


    Now if you're trying to keep a reference to a running process and then sent instructions to it later, then the only thing I can tell you at the very moment is coproc. You can learn a bit about it with help coproc, there is more in man bash and even more in info bash.



    Good luck



    EDIT: Just came back on that tab and saw the crapy formating so I fixed it. Also, I wanted to add that I haven't been able to make it work with Python but most likely it's doable, althogh with other shell like bash it works like that.



    $> coproc PRC { bash; } &
    $> echo 'echo $PATH' >&${PRC[1]}
    $> read -t1 -u ${PRC[0]} resp
    $> echo $resp
    /the/PATH/is/printed
    $> echo 'exit' >&${PRC[1]}
    $> jobs
    [1]+ Done





    share|improve this answer


























      Your Answer








      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "3"
      };
      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%2fsuperuser.com%2fquestions%2f1412370%2fusing-built-in-commands-from-running-process-in-script%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0














      Run the program in tmux (or run reptyr in tmux to "hijack" already running process, if you need; e.g. tmux new-session reptyr "$pid_to_hijack"). Then you can do this from another terminal:



      pane='$0:@0.1'
      tmux send-keys -t "$pane" C-c # a way to send Ctrl+C
      sleep 2 # let the program react (you may or may not need it)
      tmux send-keys -t "$pane" Enter Enter Enter # "should I press enter a few times"
      tmux send-keys -t "$pane" quit Enter # "I should write quit and press enter"
      tmux send-keys -t "$pane" q Enter # "then q and enter"
      tmux send-keys -t "$pane" n Enter # "n and enter"
      tmux send-keys -t "$pane" Enter # "then lastly enter"


      where the example $0:@0.1 identifies the pane in which your program runs (you must single-quote this string, otherwise the shell will expand $0). If you name your tmux session(s) and window(s), then this string may be like mysession:mywindow.0.






      share|improve this answer






























        0














        Run the program in tmux (or run reptyr in tmux to "hijack" already running process, if you need; e.g. tmux new-session reptyr "$pid_to_hijack"). Then you can do this from another terminal:



        pane='$0:@0.1'
        tmux send-keys -t "$pane" C-c # a way to send Ctrl+C
        sleep 2 # let the program react (you may or may not need it)
        tmux send-keys -t "$pane" Enter Enter Enter # "should I press enter a few times"
        tmux send-keys -t "$pane" quit Enter # "I should write quit and press enter"
        tmux send-keys -t "$pane" q Enter # "then q and enter"
        tmux send-keys -t "$pane" n Enter # "n and enter"
        tmux send-keys -t "$pane" Enter # "then lastly enter"


        where the example $0:@0.1 identifies the pane in which your program runs (you must single-quote this string, otherwise the shell will expand $0). If you name your tmux session(s) and window(s), then this string may be like mysession:mywindow.0.






        share|improve this answer




























          0












          0








          0







          Run the program in tmux (or run reptyr in tmux to "hijack" already running process, if you need; e.g. tmux new-session reptyr "$pid_to_hijack"). Then you can do this from another terminal:



          pane='$0:@0.1'
          tmux send-keys -t "$pane" C-c # a way to send Ctrl+C
          sleep 2 # let the program react (you may or may not need it)
          tmux send-keys -t "$pane" Enter Enter Enter # "should I press enter a few times"
          tmux send-keys -t "$pane" quit Enter # "I should write quit and press enter"
          tmux send-keys -t "$pane" q Enter # "then q and enter"
          tmux send-keys -t "$pane" n Enter # "n and enter"
          tmux send-keys -t "$pane" Enter # "then lastly enter"


          where the example $0:@0.1 identifies the pane in which your program runs (you must single-quote this string, otherwise the shell will expand $0). If you name your tmux session(s) and window(s), then this string may be like mysession:mywindow.0.






          share|improve this answer















          Run the program in tmux (or run reptyr in tmux to "hijack" already running process, if you need; e.g. tmux new-session reptyr "$pid_to_hijack"). Then you can do this from another terminal:



          pane='$0:@0.1'
          tmux send-keys -t "$pane" C-c # a way to send Ctrl+C
          sleep 2 # let the program react (you may or may not need it)
          tmux send-keys -t "$pane" Enter Enter Enter # "should I press enter a few times"
          tmux send-keys -t "$pane" quit Enter # "I should write quit and press enter"
          tmux send-keys -t "$pane" q Enter # "then q and enter"
          tmux send-keys -t "$pane" n Enter # "n and enter"
          tmux send-keys -t "$pane" Enter # "then lastly enter"


          where the example $0:@0.1 identifies the pane in which your program runs (you must single-quote this string, otherwise the shell will expand $0). If you name your tmux session(s) and window(s), then this string may be like mysession:mywindow.0.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Mar 8 at 10:44

























          answered Mar 8 at 10:14









          Kamil MaciorowskiKamil Maciorowski

          29.1k156288




          29.1k156288

























              0














              As I understand you're trying to send command to a "subshell". Let's say something like running command quit() in a python shell? Or any other programs that waits for an input?



              If it's that what you want, I think you're looking for something like :



              $> python <<< "quit() python <<< "<many instructions here>" python <
              $> file_containing_instructions.py #with quit() at the end python << END
              $> <instructions> <on_many_lines> END


              Now if you're trying to keep a reference to a running process and then sent instructions to it later, then the only thing I can tell you at the very moment is coproc. You can learn a bit about it with help coproc, there is more in man bash and even more in info bash.



              Good luck



              EDIT: Just came back on that tab and saw the crapy formating so I fixed it. Also, I wanted to add that I haven't been able to make it work with Python but most likely it's doable, althogh with other shell like bash it works like that.



              $> coproc PRC { bash; } &
              $> echo 'echo $PATH' >&${PRC[1]}
              $> read -t1 -u ${PRC[0]} resp
              $> echo $resp
              /the/PATH/is/printed
              $> echo 'exit' >&${PRC[1]}
              $> jobs
              [1]+ Done





              share|improve this answer






























                0














                As I understand you're trying to send command to a "subshell". Let's say something like running command quit() in a python shell? Or any other programs that waits for an input?



                If it's that what you want, I think you're looking for something like :



                $> python <<< "quit() python <<< "<many instructions here>" python <
                $> file_containing_instructions.py #with quit() at the end python << END
                $> <instructions> <on_many_lines> END


                Now if you're trying to keep a reference to a running process and then sent instructions to it later, then the only thing I can tell you at the very moment is coproc. You can learn a bit about it with help coproc, there is more in man bash and even more in info bash.



                Good luck



                EDIT: Just came back on that tab and saw the crapy formating so I fixed it. Also, I wanted to add that I haven't been able to make it work with Python but most likely it's doable, althogh with other shell like bash it works like that.



                $> coproc PRC { bash; } &
                $> echo 'echo $PATH' >&${PRC[1]}
                $> read -t1 -u ${PRC[0]} resp
                $> echo $resp
                /the/PATH/is/printed
                $> echo 'exit' >&${PRC[1]}
                $> jobs
                [1]+ Done





                share|improve this answer




























                  0












                  0








                  0







                  As I understand you're trying to send command to a "subshell". Let's say something like running command quit() in a python shell? Or any other programs that waits for an input?



                  If it's that what you want, I think you're looking for something like :



                  $> python <<< "quit() python <<< "<many instructions here>" python <
                  $> file_containing_instructions.py #with quit() at the end python << END
                  $> <instructions> <on_many_lines> END


                  Now if you're trying to keep a reference to a running process and then sent instructions to it later, then the only thing I can tell you at the very moment is coproc. You can learn a bit about it with help coproc, there is more in man bash and even more in info bash.



                  Good luck



                  EDIT: Just came back on that tab and saw the crapy formating so I fixed it. Also, I wanted to add that I haven't been able to make it work with Python but most likely it's doable, althogh with other shell like bash it works like that.



                  $> coproc PRC { bash; } &
                  $> echo 'echo $PATH' >&${PRC[1]}
                  $> read -t1 -u ${PRC[0]} resp
                  $> echo $resp
                  /the/PATH/is/printed
                  $> echo 'exit' >&${PRC[1]}
                  $> jobs
                  [1]+ Done





                  share|improve this answer















                  As I understand you're trying to send command to a "subshell". Let's say something like running command quit() in a python shell? Or any other programs that waits for an input?



                  If it's that what you want, I think you're looking for something like :



                  $> python <<< "quit() python <<< "<many instructions here>" python <
                  $> file_containing_instructions.py #with quit() at the end python << END
                  $> <instructions> <on_many_lines> END


                  Now if you're trying to keep a reference to a running process and then sent instructions to it later, then the only thing I can tell you at the very moment is coproc. You can learn a bit about it with help coproc, there is more in man bash and even more in info bash.



                  Good luck



                  EDIT: Just came back on that tab and saw the crapy formating so I fixed it. Also, I wanted to add that I haven't been able to make it work with Python but most likely it's doable, althogh with other shell like bash it works like that.



                  $> coproc PRC { bash; } &
                  $> echo 'echo $PATH' >&${PRC[1]}
                  $> read -t1 -u ${PRC[0]} resp
                  $> echo $resp
                  /the/PATH/is/printed
                  $> echo 'exit' >&${PRC[1]}
                  $> jobs
                  [1]+ Done






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Mar 19 at 10:14

























                  answered Mar 19 at 9:19









                  NausixNausix

                  112




                  112






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Super User!


                      • 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%2fsuperuser.com%2fquestions%2f1412370%2fusing-built-in-commands-from-running-process-in-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

                      Probability when a professor distributes a quiz and homework assignment to a class of n students.

                      Aardman Animations

                      Are they similar matrix