Can I send some text to the STDIN of an active process running in a screen session? Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Come Celebrate our 10 Year Anniversary!Writing to stdin of background processWrite to the stdin of a running process with the same effect/behaviour of directly writingWhy does writing to the console a process's STDIN is attached to doesn't send input to the application itself?Can I nohup/screen an already-started process?Moving an already-running process to ScreenCreate screen and run command without attachingRun (GNU) Screen from scriptCan I nohup/resume an existing tar process initiation in a ssh session?Write to the stdin of a running process with the same effect/behaviour of directly writingWrite to the stdin of a running process (without changing the way of starting the program)Write to stdin of a running process using pipeKilling screen session and starting a new one, cronObtain hardcopy from a screen session

How do I find out the mythology and history of my Fortress?

Do wooden building fires get hotter than 600°C?

Semigroups with no morphisms between them

Strange behavior of Object.defineProperty() in JavaScript

Project Euler #1 in C++

The Nth Gryphon Number

Intuitive explanation of the rank-nullity theorem

Putting class ranking in CV, but against dept guidelines

How does light 'choose' between wave and particle behaviour?

How were pictures turned from film to a big picture in a picture frame before digital scanning?

Is it fair for a professor to grade us on the possession of past papers?

Is there public access to the Meteor Crater in Arizona?

What does 丫 mean? 丫是什么意思?

Did Mueller's report provide an evidentiary basis for the claim of Russian govt election interference via social media?

How does Belgium enforce obligatory attendance in elections?

How would a mousetrap for use in space work?

How can I set the aperture on my DSLR when it's attached to a telescope instead of a lens?

How long can equipment go unused before powering up runs the risk of damage?

What makes a man succeed?

Trademark violation for app?

Would it be easier to apply for a UK visa if there is a host family to sponsor for you in going there?

Drawing spherical mirrors

Deconstruction is ambiguous

What initially awakened the Balrog?



Can I send some text to the STDIN of an active process running in a screen session?



Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern)
Come Celebrate our 10 Year Anniversary!Writing to stdin of background processWrite to the stdin of a running process with the same effect/behaviour of directly writingWhy does writing to the console a process's STDIN is attached to doesn't send input to the application itself?Can I nohup/screen an already-started process?Moving an already-running process to ScreenCreate screen and run command without attachingRun (GNU) Screen from scriptCan I nohup/resume an existing tar process initiation in a ssh session?Write to the stdin of a running process with the same effect/behaviour of directly writingWrite to the stdin of a running process (without changing the way of starting the program)Write to stdin of a running process using pipeKilling screen session and starting a new one, cronObtain hardcopy from a screen session



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








67















I have a long-running server process inside a screen session on my Linux server. It's a bit unstable (and sadly not my software so I can't fix that!), so I want to script a nightly restart of the process to help stability. The only way to make it do a graceful shutdown is to go to the screen process, switch to the window it's running in, and enter the string "stop" on its control console.



Are there any smart redirection contortions I can do to make a cronjob send that stop command at a fixed time every day?










share|improve this question






























    67















    I have a long-running server process inside a screen session on my Linux server. It's a bit unstable (and sadly not my software so I can't fix that!), so I want to script a nightly restart of the process to help stability. The only way to make it do a graceful shutdown is to go to the screen process, switch to the window it's running in, and enter the string "stop" on its control console.



    Are there any smart redirection contortions I can do to make a cronjob send that stop command at a fixed time every day?










    share|improve this question


























      67












      67








      67


      36






      I have a long-running server process inside a screen session on my Linux server. It's a bit unstable (and sadly not my software so I can't fix that!), so I want to script a nightly restart of the process to help stability. The only way to make it do a graceful shutdown is to go to the screen process, switch to the window it's running in, and enter the string "stop" on its control console.



      Are there any smart redirection contortions I can do to make a cronjob send that stop command at a fixed time every day?










      share|improve this question
















      I have a long-running server process inside a screen session on my Linux server. It's a bit unstable (and sadly not my software so I can't fix that!), so I want to script a nightly restart of the process to help stability. The only way to make it do a graceful shutdown is to go to the screen process, switch to the window it's running in, and enter the string "stop" on its control console.



      Are there any smart redirection contortions I can do to make a cronjob send that stop command at a fixed time every day?







      linux gnu-screen stdin






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Sep 6 '10 at 11:57

























      asked Sep 6 '10 at 11:42







      Richard Gaywood



























          6 Answers
          6






          active

          oldest

          votes


















          81














          This answer doesn't solve the problem, but it's left here because 30+ people found it useful, otherwise I would have deleted it long time ago.



          Write to /proc/*pid of the program*/fd/0. The fd subdirectory contains the descriptors of all the opened files and file descriptor 0 is the standard input (1 is stdout and 2 is stderr).



          You can use this to output messages on the tty where a program is running, though it does not allow you to write to the program itself.



          Example



          Terminal 1:



          [ciupicri@hermes ~]$ cat
          shows on the tty but bypasses cat


          Terminal 2:



          [ciupicri@hermes ~]$ pidof cat
          7417
          [ciupicri@hermes ~]$ echo "shows on the tty but bypasses cat" > /proc/7417/fd/0





          share|improve this answer




















          • 3





            @James Lawrie: then have a look at proc(5) and proc.txt.

            – Cristian Ciupitu
            Sep 6 '10 at 12:46







          • 2





            +2 no matter how much you think you know, there's always more to learn :) slick.

            – troyengel
            Sep 6 '10 at 14:55






          • 3





            Be aware though that the proc fd only redirects to what is used as a source of stdin. In your example, if you enter something in terminal 1 it will print it out again (it is send to cats stdin and cat prints it), thus resulting in you seeing it twice. On the other hand if you send something to fd/0 it will be sent to the console but not to cat, and thus only displayed once. As cat simply prints input out again with this example you can not really see if your input or output is being printed, thus this misconception. /fd/0 points to the console /pts; see ls -l /proc/7417/fd/0.

            – Kissaki
            Jul 2 '13 at 10:09






          • 4





            real-world example: I have started gphoto2 --get-all-files and it asks for a confirmation 100 times. When I echo "y" >/proc/PID/fd/0, gphoto2 does not proceed, however, "y" is printed in the terminal.

            – Thorsten Staerk
            May 25 '15 at 7:50






          • 2





            @ThorstenStaerk, I know, that's why I added that note. You're writing only to the device file corresponding to the terminal on which the gphoto2 runs (e.g. /dev/pts/19), the y character doesn't reach the application itself. It's the similar to what happens when you use the write(1) command. Anyway, either try my other answer or a graphical automation tool like xdotool.

            – Cristian Ciupitu
            May 25 '15 at 20:05



















          36














          Screen based solution



          Start the server like this:



          # screen -d -m -S ServerFault tr a-z A-Z # replace with your server


          screen will start in detached mode, so if you want to see what's going on, run:



          # screen -r ServerFault


          Control the server like this:



          # screen -S ServerFault -p 0 -X stuff "stop^M"
          # screen -S ServerFault -p 0 -X stuff "start^M"
          # screen -S ServerFault -p 0 -X stuff "^D" # send EOF


          (this answer is based on sending text input to a detached screen from the Unix & Linux sibling site)



          Explanation of the parameters:




          -d -m
          Start screen in "detached" mode. This creates a new session but doesn't
          attach to it. This is useful for system startup scripts.
          -S sessionname
          When creating a new session, this option can be used to specify a meaningful
          name for the session.
          -r [pid.tty.host]
          -r sessionowner/[pid.tty.host]
          resumes a detached screen session.
          -p number_or_name|-|=|+
          Preselect a window. This is useful when you want to reattach to a specific
          window or you want to send a command via the "-X" option to a specific
          window.
          -X
          Send the specified command to a running screen session e.g. stuff.


          stuff [string]



           Stuff the string string in the input buffer of the current window.
          This is like the "paste" command but with much less overhead. Without
          a parameter, screen will prompt for a string to stuff.



          tmux based solution



          Start the server like this:



          # tmux new-session -d -s ServerFault 'tr a-z A-Z' # replace with your server


          tmux will start in detached mode, so if you want to see what's going on, run:



          # tmux attach-session -t ServerFault


          Control the server like this:



          # tmux send-keys -t ServerFault -l stop
          # tmux send-keys -t ServerFault Enter
          # tmux send-keys -t ServerFault -l start
          # tmux send-keys -t ServerFault Enter
          # tmux send-keys -t ServerFault C-d # send EOF


          Explanation of the parameters:




           new-session [-AdDP] [-c start-directory] [-F format] [-n window-name] [-s
          session-name] [-t target-session] [-x width] [-y height]
          [shell-command]
          Create a new session with name session-name.

          The new session is attached to the current terminal unless -d is
          given. window-name and shell-command are the name of and shell
          command to execute in the initial window. If -d is used, -x and
          -y specify the size of the initial window (80 by 24 if not
          given).

          send-keys [-lR] [-t target-pane] key ...
          (alias: send)
          Send a key or keys to a window. Each argument key is the name of
          the key (such as `C-a' or `npage' ) to send; if the string is not
          recognised as a key, it is sent as a series of characters. The
          -l flag disables key name lookup and sends the keys literally.






          share|improve this answer
































            4














            Try this to start:



            # screen
            # cd /path/to/wd
            # mkfifo cmd
            # my_cmd <cmd
            C-A d


            And this to kill:



            # cd /path/to/wd
            # echo "stop" > cmd
            # rm cmd





            share|improve this answer


















            • 3





              This is good, but it might have the disadvantage of not being able to send other commands while the program is running. If the program stops when it hits EOF on stdin then on the first echo "xxx" > cmd the program will stop (because the pipe will be closed). Though some programs are smart enough to reopen (rewind(3)) their stdin when they encounter EOF.

              – Cristian Ciupitu
              Sep 6 '10 at 12:30



















            1














            In case it helps anyone:

            I had a similar problem, and as the process I was using wasn't under screen or tmux, I had to take a different approach.



            I attached gdb to the xterm that my process was running in, and used call write(5, "stopn", 5) from gdb to write to the master pty file descriptor.

            I found out which file descriptor to send the data to by looking at /proc/<pid>/fd for a link to /dev/ptmx and then trial and error between the two options (sending my string to both matching file descriptors seemed to cause no harm).



            EDIT

            It turned out that the xterm process I had attached to was spawned with the spawn-new-terminal() xterm action from a keybinding, and the second ptmx file descriptor open was simply the ptmx of the parent xterm process that hadn't been closed.

            Hence the trial and error calls had sent output to that other terminal.

            Most xterm processes don't have two ptmx file descriptors.



            END EDIT

            This effectively typed that string into the terminal, and hence sent it along to the process running under it.



            n.b. you may need to allow attaching to a running process with something like
            sudo bash -c "echo 0 > /proc/sys/kernel/yama/ptrace_scope"






            share|improve this answer
































              1














              It is possible to send input text to a running process without running the screen utility, or any other fancy utility. And it can be done by sending this input text to the process' standard input "file" /proc/PID#/fd/0.



              However, the input text needs to be sent in a special way to be read by the process. Sending the input text via the regular file write method will not cause the process to receive the text. This is because doing so will only append to that "file", but will not trigger the process to read the bytes.



              To trigger the process to read the bytes, it is necessary to do an IOCTL operation of type TIOCSTI for every single byte to be sent. This will place the byte into the process' standard input queue.



              This is discussed here with some examples in C, Perl, and Python:



              https://unix.stackexchange.com/questions/48103/construct-a-command-by-putting-a-string-into-a-tty/48221



              --



              So to answer the original question asked almost 9 years ago, the cron job would need to run some small utility script / program similar to the examples people wrote for that other question, which would send the string "stopn" to that server process in the question, by sending each of the 5 bytes via an IOCTL operation of type TIOCSTI.



              Of course this will only work on systems that support the TIOCSTI IOCTL operation type (like Linux), and only from the root user account, as these "files" under /proc/ are "owned" by root.






              share|improve this answer










              New contributor




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



























                0














                Since I cannot comment the most accepted answer of Cristian Ciupitu (of 2010), I have to put this in a separate answer:



                This question was already solved in this thread: https://stackoverflow.com/questions/5374255/how-to-write-data-to-existing-processs-stdin-from-external-process



                In short:



                You have to start your process with a pipe for stdin which does not block nor close when the current input was written through. This can be implemented by a simple endless loop which will be piped to the process in question:



                $ (while [ 1 ]; do sleep 1; done) | yourProgramToStart


                I can confirm that this is different of krissi's way to open a pipe which was not working in my case. The shown solution did work instead.



                You can then write to the .../fd/0 file of the process to send instructions to it. The only drawback is that you need to terminate the bash process as well which is executing the endless-loop after the server did shut down.






                share|improve this answer























                  Your Answer








                  StackExchange.ready(function()
                  var channelOptions =
                  tags: "".split(" "),
                  id: "2"
                  ;
                  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%2fserverfault.com%2fquestions%2f178457%2fcan-i-send-some-text-to-the-stdin-of-an-active-process-running-in-a-screen-sessi%23new-answer', 'question_page');

                  );

                  Post as a guest















                  Required, but never shown
























                  6 Answers
                  6






                  active

                  oldest

                  votes








                  6 Answers
                  6






                  active

                  oldest

                  votes









                  active

                  oldest

                  votes






                  active

                  oldest

                  votes









                  81














                  This answer doesn't solve the problem, but it's left here because 30+ people found it useful, otherwise I would have deleted it long time ago.



                  Write to /proc/*pid of the program*/fd/0. The fd subdirectory contains the descriptors of all the opened files and file descriptor 0 is the standard input (1 is stdout and 2 is stderr).



                  You can use this to output messages on the tty where a program is running, though it does not allow you to write to the program itself.



                  Example



                  Terminal 1:



                  [ciupicri@hermes ~]$ cat
                  shows on the tty but bypasses cat


                  Terminal 2:



                  [ciupicri@hermes ~]$ pidof cat
                  7417
                  [ciupicri@hermes ~]$ echo "shows on the tty but bypasses cat" > /proc/7417/fd/0





                  share|improve this answer




















                  • 3





                    @James Lawrie: then have a look at proc(5) and proc.txt.

                    – Cristian Ciupitu
                    Sep 6 '10 at 12:46







                  • 2





                    +2 no matter how much you think you know, there's always more to learn :) slick.

                    – troyengel
                    Sep 6 '10 at 14:55






                  • 3





                    Be aware though that the proc fd only redirects to what is used as a source of stdin. In your example, if you enter something in terminal 1 it will print it out again (it is send to cats stdin and cat prints it), thus resulting in you seeing it twice. On the other hand if you send something to fd/0 it will be sent to the console but not to cat, and thus only displayed once. As cat simply prints input out again with this example you can not really see if your input or output is being printed, thus this misconception. /fd/0 points to the console /pts; see ls -l /proc/7417/fd/0.

                    – Kissaki
                    Jul 2 '13 at 10:09






                  • 4





                    real-world example: I have started gphoto2 --get-all-files and it asks for a confirmation 100 times. When I echo "y" >/proc/PID/fd/0, gphoto2 does not proceed, however, "y" is printed in the terminal.

                    – Thorsten Staerk
                    May 25 '15 at 7:50






                  • 2





                    @ThorstenStaerk, I know, that's why I added that note. You're writing only to the device file corresponding to the terminal on which the gphoto2 runs (e.g. /dev/pts/19), the y character doesn't reach the application itself. It's the similar to what happens when you use the write(1) command. Anyway, either try my other answer or a graphical automation tool like xdotool.

                    – Cristian Ciupitu
                    May 25 '15 at 20:05
















                  81














                  This answer doesn't solve the problem, but it's left here because 30+ people found it useful, otherwise I would have deleted it long time ago.



                  Write to /proc/*pid of the program*/fd/0. The fd subdirectory contains the descriptors of all the opened files and file descriptor 0 is the standard input (1 is stdout and 2 is stderr).



                  You can use this to output messages on the tty where a program is running, though it does not allow you to write to the program itself.



                  Example



                  Terminal 1:



                  [ciupicri@hermes ~]$ cat
                  shows on the tty but bypasses cat


                  Terminal 2:



                  [ciupicri@hermes ~]$ pidof cat
                  7417
                  [ciupicri@hermes ~]$ echo "shows on the tty but bypasses cat" > /proc/7417/fd/0





                  share|improve this answer




















                  • 3





                    @James Lawrie: then have a look at proc(5) and proc.txt.

                    – Cristian Ciupitu
                    Sep 6 '10 at 12:46







                  • 2





                    +2 no matter how much you think you know, there's always more to learn :) slick.

                    – troyengel
                    Sep 6 '10 at 14:55






                  • 3





                    Be aware though that the proc fd only redirects to what is used as a source of stdin. In your example, if you enter something in terminal 1 it will print it out again (it is send to cats stdin and cat prints it), thus resulting in you seeing it twice. On the other hand if you send something to fd/0 it will be sent to the console but not to cat, and thus only displayed once. As cat simply prints input out again with this example you can not really see if your input or output is being printed, thus this misconception. /fd/0 points to the console /pts; see ls -l /proc/7417/fd/0.

                    – Kissaki
                    Jul 2 '13 at 10:09






                  • 4





                    real-world example: I have started gphoto2 --get-all-files and it asks for a confirmation 100 times. When I echo "y" >/proc/PID/fd/0, gphoto2 does not proceed, however, "y" is printed in the terminal.

                    – Thorsten Staerk
                    May 25 '15 at 7:50






                  • 2





                    @ThorstenStaerk, I know, that's why I added that note. You're writing only to the device file corresponding to the terminal on which the gphoto2 runs (e.g. /dev/pts/19), the y character doesn't reach the application itself. It's the similar to what happens when you use the write(1) command. Anyway, either try my other answer or a graphical automation tool like xdotool.

                    – Cristian Ciupitu
                    May 25 '15 at 20:05














                  81












                  81








                  81







                  This answer doesn't solve the problem, but it's left here because 30+ people found it useful, otherwise I would have deleted it long time ago.



                  Write to /proc/*pid of the program*/fd/0. The fd subdirectory contains the descriptors of all the opened files and file descriptor 0 is the standard input (1 is stdout and 2 is stderr).



                  You can use this to output messages on the tty where a program is running, though it does not allow you to write to the program itself.



                  Example



                  Terminal 1:



                  [ciupicri@hermes ~]$ cat
                  shows on the tty but bypasses cat


                  Terminal 2:



                  [ciupicri@hermes ~]$ pidof cat
                  7417
                  [ciupicri@hermes ~]$ echo "shows on the tty but bypasses cat" > /proc/7417/fd/0





                  share|improve this answer















                  This answer doesn't solve the problem, but it's left here because 30+ people found it useful, otherwise I would have deleted it long time ago.



                  Write to /proc/*pid of the program*/fd/0. The fd subdirectory contains the descriptors of all the opened files and file descriptor 0 is the standard input (1 is stdout and 2 is stderr).



                  You can use this to output messages on the tty where a program is running, though it does not allow you to write to the program itself.



                  Example



                  Terminal 1:



                  [ciupicri@hermes ~]$ cat
                  shows on the tty but bypasses cat


                  Terminal 2:



                  [ciupicri@hermes ~]$ pidof cat
                  7417
                  [ciupicri@hermes ~]$ echo "shows on the tty but bypasses cat" > /proc/7417/fd/0






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Jun 27 '18 at 9:16









                  MadHatter

                  70.5k11146207




                  70.5k11146207










                  answered Sep 6 '10 at 12:06









                  Cristian CiupituCristian Ciupitu

                  5,45013351




                  5,45013351







                  • 3





                    @James Lawrie: then have a look at proc(5) and proc.txt.

                    – Cristian Ciupitu
                    Sep 6 '10 at 12:46







                  • 2





                    +2 no matter how much you think you know, there's always more to learn :) slick.

                    – troyengel
                    Sep 6 '10 at 14:55






                  • 3





                    Be aware though that the proc fd only redirects to what is used as a source of stdin. In your example, if you enter something in terminal 1 it will print it out again (it is send to cats stdin and cat prints it), thus resulting in you seeing it twice. On the other hand if you send something to fd/0 it will be sent to the console but not to cat, and thus only displayed once. As cat simply prints input out again with this example you can not really see if your input or output is being printed, thus this misconception. /fd/0 points to the console /pts; see ls -l /proc/7417/fd/0.

                    – Kissaki
                    Jul 2 '13 at 10:09






                  • 4





                    real-world example: I have started gphoto2 --get-all-files and it asks for a confirmation 100 times. When I echo "y" >/proc/PID/fd/0, gphoto2 does not proceed, however, "y" is printed in the terminal.

                    – Thorsten Staerk
                    May 25 '15 at 7:50






                  • 2





                    @ThorstenStaerk, I know, that's why I added that note. You're writing only to the device file corresponding to the terminal on which the gphoto2 runs (e.g. /dev/pts/19), the y character doesn't reach the application itself. It's the similar to what happens when you use the write(1) command. Anyway, either try my other answer or a graphical automation tool like xdotool.

                    – Cristian Ciupitu
                    May 25 '15 at 20:05













                  • 3





                    @James Lawrie: then have a look at proc(5) and proc.txt.

                    – Cristian Ciupitu
                    Sep 6 '10 at 12:46







                  • 2





                    +2 no matter how much you think you know, there's always more to learn :) slick.

                    – troyengel
                    Sep 6 '10 at 14:55






                  • 3





                    Be aware though that the proc fd only redirects to what is used as a source of stdin. In your example, if you enter something in terminal 1 it will print it out again (it is send to cats stdin and cat prints it), thus resulting in you seeing it twice. On the other hand if you send something to fd/0 it will be sent to the console but not to cat, and thus only displayed once. As cat simply prints input out again with this example you can not really see if your input or output is being printed, thus this misconception. /fd/0 points to the console /pts; see ls -l /proc/7417/fd/0.

                    – Kissaki
                    Jul 2 '13 at 10:09






                  • 4





                    real-world example: I have started gphoto2 --get-all-files and it asks for a confirmation 100 times. When I echo "y" >/proc/PID/fd/0, gphoto2 does not proceed, however, "y" is printed in the terminal.

                    – Thorsten Staerk
                    May 25 '15 at 7:50






                  • 2





                    @ThorstenStaerk, I know, that's why I added that note. You're writing only to the device file corresponding to the terminal on which the gphoto2 runs (e.g. /dev/pts/19), the y character doesn't reach the application itself. It's the similar to what happens when you use the write(1) command. Anyway, either try my other answer or a graphical automation tool like xdotool.

                    – Cristian Ciupitu
                    May 25 '15 at 20:05








                  3




                  3





                  @James Lawrie: then have a look at proc(5) and proc.txt.

                  – Cristian Ciupitu
                  Sep 6 '10 at 12:46






                  @James Lawrie: then have a look at proc(5) and proc.txt.

                  – Cristian Ciupitu
                  Sep 6 '10 at 12:46





                  2




                  2





                  +2 no matter how much you think you know, there's always more to learn :) slick.

                  – troyengel
                  Sep 6 '10 at 14:55





                  +2 no matter how much you think you know, there's always more to learn :) slick.

                  – troyengel
                  Sep 6 '10 at 14:55




                  3




                  3





                  Be aware though that the proc fd only redirects to what is used as a source of stdin. In your example, if you enter something in terminal 1 it will print it out again (it is send to cats stdin and cat prints it), thus resulting in you seeing it twice. On the other hand if you send something to fd/0 it will be sent to the console but not to cat, and thus only displayed once. As cat simply prints input out again with this example you can not really see if your input or output is being printed, thus this misconception. /fd/0 points to the console /pts; see ls -l /proc/7417/fd/0.

                  – Kissaki
                  Jul 2 '13 at 10:09





                  Be aware though that the proc fd only redirects to what is used as a source of stdin. In your example, if you enter something in terminal 1 it will print it out again (it is send to cats stdin and cat prints it), thus resulting in you seeing it twice. On the other hand if you send something to fd/0 it will be sent to the console but not to cat, and thus only displayed once. As cat simply prints input out again with this example you can not really see if your input or output is being printed, thus this misconception. /fd/0 points to the console /pts; see ls -l /proc/7417/fd/0.

                  – Kissaki
                  Jul 2 '13 at 10:09




                  4




                  4





                  real-world example: I have started gphoto2 --get-all-files and it asks for a confirmation 100 times. When I echo "y" >/proc/PID/fd/0, gphoto2 does not proceed, however, "y" is printed in the terminal.

                  – Thorsten Staerk
                  May 25 '15 at 7:50





                  real-world example: I have started gphoto2 --get-all-files and it asks for a confirmation 100 times. When I echo "y" >/proc/PID/fd/0, gphoto2 does not proceed, however, "y" is printed in the terminal.

                  – Thorsten Staerk
                  May 25 '15 at 7:50




                  2




                  2





                  @ThorstenStaerk, I know, that's why I added that note. You're writing only to the device file corresponding to the terminal on which the gphoto2 runs (e.g. /dev/pts/19), the y character doesn't reach the application itself. It's the similar to what happens when you use the write(1) command. Anyway, either try my other answer or a graphical automation tool like xdotool.

                  – Cristian Ciupitu
                  May 25 '15 at 20:05






                  @ThorstenStaerk, I know, that's why I added that note. You're writing only to the device file corresponding to the terminal on which the gphoto2 runs (e.g. /dev/pts/19), the y character doesn't reach the application itself. It's the similar to what happens when you use the write(1) command. Anyway, either try my other answer or a graphical automation tool like xdotool.

                  – Cristian Ciupitu
                  May 25 '15 at 20:05














                  36














                  Screen based solution



                  Start the server like this:



                  # screen -d -m -S ServerFault tr a-z A-Z # replace with your server


                  screen will start in detached mode, so if you want to see what's going on, run:



                  # screen -r ServerFault


                  Control the server like this:



                  # screen -S ServerFault -p 0 -X stuff "stop^M"
                  # screen -S ServerFault -p 0 -X stuff "start^M"
                  # screen -S ServerFault -p 0 -X stuff "^D" # send EOF


                  (this answer is based on sending text input to a detached screen from the Unix & Linux sibling site)



                  Explanation of the parameters:




                  -d -m
                  Start screen in "detached" mode. This creates a new session but doesn't
                  attach to it. This is useful for system startup scripts.
                  -S sessionname
                  When creating a new session, this option can be used to specify a meaningful
                  name for the session.
                  -r [pid.tty.host]
                  -r sessionowner/[pid.tty.host]
                  resumes a detached screen session.
                  -p number_or_name|-|=|+
                  Preselect a window. This is useful when you want to reattach to a specific
                  window or you want to send a command via the "-X" option to a specific
                  window.
                  -X
                  Send the specified command to a running screen session e.g. stuff.


                  stuff [string]



                   Stuff the string string in the input buffer of the current window.
                  This is like the "paste" command but with much less overhead. Without
                  a parameter, screen will prompt for a string to stuff.



                  tmux based solution



                  Start the server like this:



                  # tmux new-session -d -s ServerFault 'tr a-z A-Z' # replace with your server


                  tmux will start in detached mode, so if you want to see what's going on, run:



                  # tmux attach-session -t ServerFault


                  Control the server like this:



                  # tmux send-keys -t ServerFault -l stop
                  # tmux send-keys -t ServerFault Enter
                  # tmux send-keys -t ServerFault -l start
                  # tmux send-keys -t ServerFault Enter
                  # tmux send-keys -t ServerFault C-d # send EOF


                  Explanation of the parameters:




                   new-session [-AdDP] [-c start-directory] [-F format] [-n window-name] [-s
                  session-name] [-t target-session] [-x width] [-y height]
                  [shell-command]
                  Create a new session with name session-name.

                  The new session is attached to the current terminal unless -d is
                  given. window-name and shell-command are the name of and shell
                  command to execute in the initial window. If -d is used, -x and
                  -y specify the size of the initial window (80 by 24 if not
                  given).

                  send-keys [-lR] [-t target-pane] key ...
                  (alias: send)
                  Send a key or keys to a window. Each argument key is the name of
                  the key (such as `C-a' or `npage' ) to send; if the string is not
                  recognised as a key, it is sent as a series of characters. The
                  -l flag disables key name lookup and sends the keys literally.






                  share|improve this answer





























                    36














                    Screen based solution



                    Start the server like this:



                    # screen -d -m -S ServerFault tr a-z A-Z # replace with your server


                    screen will start in detached mode, so if you want to see what's going on, run:



                    # screen -r ServerFault


                    Control the server like this:



                    # screen -S ServerFault -p 0 -X stuff "stop^M"
                    # screen -S ServerFault -p 0 -X stuff "start^M"
                    # screen -S ServerFault -p 0 -X stuff "^D" # send EOF


                    (this answer is based on sending text input to a detached screen from the Unix & Linux sibling site)



                    Explanation of the parameters:




                    -d -m
                    Start screen in "detached" mode. This creates a new session but doesn't
                    attach to it. This is useful for system startup scripts.
                    -S sessionname
                    When creating a new session, this option can be used to specify a meaningful
                    name for the session.
                    -r [pid.tty.host]
                    -r sessionowner/[pid.tty.host]
                    resumes a detached screen session.
                    -p number_or_name|-|=|+
                    Preselect a window. This is useful when you want to reattach to a specific
                    window or you want to send a command via the "-X" option to a specific
                    window.
                    -X
                    Send the specified command to a running screen session e.g. stuff.


                    stuff [string]



                     Stuff the string string in the input buffer of the current window.
                    This is like the "paste" command but with much less overhead. Without
                    a parameter, screen will prompt for a string to stuff.



                    tmux based solution



                    Start the server like this:



                    # tmux new-session -d -s ServerFault 'tr a-z A-Z' # replace with your server


                    tmux will start in detached mode, so if you want to see what's going on, run:



                    # tmux attach-session -t ServerFault


                    Control the server like this:



                    # tmux send-keys -t ServerFault -l stop
                    # tmux send-keys -t ServerFault Enter
                    # tmux send-keys -t ServerFault -l start
                    # tmux send-keys -t ServerFault Enter
                    # tmux send-keys -t ServerFault C-d # send EOF


                    Explanation of the parameters:




                     new-session [-AdDP] [-c start-directory] [-F format] [-n window-name] [-s
                    session-name] [-t target-session] [-x width] [-y height]
                    [shell-command]
                    Create a new session with name session-name.

                    The new session is attached to the current terminal unless -d is
                    given. window-name and shell-command are the name of and shell
                    command to execute in the initial window. If -d is used, -x and
                    -y specify the size of the initial window (80 by 24 if not
                    given).

                    send-keys [-lR] [-t target-pane] key ...
                    (alias: send)
                    Send a key or keys to a window. Each argument key is the name of
                    the key (such as `C-a' or `npage' ) to send; if the string is not
                    recognised as a key, it is sent as a series of characters. The
                    -l flag disables key name lookup and sends the keys literally.






                    share|improve this answer



























                      36












                      36








                      36







                      Screen based solution



                      Start the server like this:



                      # screen -d -m -S ServerFault tr a-z A-Z # replace with your server


                      screen will start in detached mode, so if you want to see what's going on, run:



                      # screen -r ServerFault


                      Control the server like this:



                      # screen -S ServerFault -p 0 -X stuff "stop^M"
                      # screen -S ServerFault -p 0 -X stuff "start^M"
                      # screen -S ServerFault -p 0 -X stuff "^D" # send EOF


                      (this answer is based on sending text input to a detached screen from the Unix & Linux sibling site)



                      Explanation of the parameters:




                      -d -m
                      Start screen in "detached" mode. This creates a new session but doesn't
                      attach to it. This is useful for system startup scripts.
                      -S sessionname
                      When creating a new session, this option can be used to specify a meaningful
                      name for the session.
                      -r [pid.tty.host]
                      -r sessionowner/[pid.tty.host]
                      resumes a detached screen session.
                      -p number_or_name|-|=|+
                      Preselect a window. This is useful when you want to reattach to a specific
                      window or you want to send a command via the "-X" option to a specific
                      window.
                      -X
                      Send the specified command to a running screen session e.g. stuff.


                      stuff [string]



                       Stuff the string string in the input buffer of the current window.
                      This is like the "paste" command but with much less overhead. Without
                      a parameter, screen will prompt for a string to stuff.



                      tmux based solution



                      Start the server like this:



                      # tmux new-session -d -s ServerFault 'tr a-z A-Z' # replace with your server


                      tmux will start in detached mode, so if you want to see what's going on, run:



                      # tmux attach-session -t ServerFault


                      Control the server like this:



                      # tmux send-keys -t ServerFault -l stop
                      # tmux send-keys -t ServerFault Enter
                      # tmux send-keys -t ServerFault -l start
                      # tmux send-keys -t ServerFault Enter
                      # tmux send-keys -t ServerFault C-d # send EOF


                      Explanation of the parameters:




                       new-session [-AdDP] [-c start-directory] [-F format] [-n window-name] [-s
                      session-name] [-t target-session] [-x width] [-y height]
                      [shell-command]
                      Create a new session with name session-name.

                      The new session is attached to the current terminal unless -d is
                      given. window-name and shell-command are the name of and shell
                      command to execute in the initial window. If -d is used, -x and
                      -y specify the size of the initial window (80 by 24 if not
                      given).

                      send-keys [-lR] [-t target-pane] key ...
                      (alias: send)
                      Send a key or keys to a window. Each argument key is the name of
                      the key (such as `C-a' or `npage' ) to send; if the string is not
                      recognised as a key, it is sent as a series of characters. The
                      -l flag disables key name lookup and sends the keys literally.






                      share|improve this answer















                      Screen based solution



                      Start the server like this:



                      # screen -d -m -S ServerFault tr a-z A-Z # replace with your server


                      screen will start in detached mode, so if you want to see what's going on, run:



                      # screen -r ServerFault


                      Control the server like this:



                      # screen -S ServerFault -p 0 -X stuff "stop^M"
                      # screen -S ServerFault -p 0 -X stuff "start^M"
                      # screen -S ServerFault -p 0 -X stuff "^D" # send EOF


                      (this answer is based on sending text input to a detached screen from the Unix & Linux sibling site)



                      Explanation of the parameters:




                      -d -m
                      Start screen in "detached" mode. This creates a new session but doesn't
                      attach to it. This is useful for system startup scripts.
                      -S sessionname
                      When creating a new session, this option can be used to specify a meaningful
                      name for the session.
                      -r [pid.tty.host]
                      -r sessionowner/[pid.tty.host]
                      resumes a detached screen session.
                      -p number_or_name|-|=|+
                      Preselect a window. This is useful when you want to reattach to a specific
                      window or you want to send a command via the "-X" option to a specific
                      window.
                      -X
                      Send the specified command to a running screen session e.g. stuff.


                      stuff [string]



                       Stuff the string string in the input buffer of the current window.
                      This is like the "paste" command but with much less overhead. Without
                      a parameter, screen will prompt for a string to stuff.



                      tmux based solution



                      Start the server like this:



                      # tmux new-session -d -s ServerFault 'tr a-z A-Z' # replace with your server


                      tmux will start in detached mode, so if you want to see what's going on, run:



                      # tmux attach-session -t ServerFault


                      Control the server like this:



                      # tmux send-keys -t ServerFault -l stop
                      # tmux send-keys -t ServerFault Enter
                      # tmux send-keys -t ServerFault -l start
                      # tmux send-keys -t ServerFault Enter
                      # tmux send-keys -t ServerFault C-d # send EOF


                      Explanation of the parameters:




                       new-session [-AdDP] [-c start-directory] [-F format] [-n window-name] [-s
                      session-name] [-t target-session] [-x width] [-y height]
                      [shell-command]
                      Create a new session with name session-name.

                      The new session is attached to the current terminal unless -d is
                      given. window-name and shell-command are the name of and shell
                      command to execute in the initial window. If -d is used, -x and
                      -y specify the size of the initial window (80 by 24 if not
                      given).

                      send-keys [-lR] [-t target-pane] key ...
                      (alias: send)
                      Send a key or keys to a window. Each argument key is the name of
                      the key (such as `C-a' or `npage' ) to send; if the string is not
                      recognised as a key, it is sent as a series of characters. The
                      -l flag disables key name lookup and sends the keys literally.







                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Apr 13 '17 at 12:37









                      Community

                      1




                      1










                      answered Oct 19 '13 at 1:08









                      Cristian CiupituCristian Ciupitu

                      5,45013351




                      5,45013351





















                          4














                          Try this to start:



                          # screen
                          # cd /path/to/wd
                          # mkfifo cmd
                          # my_cmd <cmd
                          C-A d


                          And this to kill:



                          # cd /path/to/wd
                          # echo "stop" > cmd
                          # rm cmd





                          share|improve this answer


















                          • 3





                            This is good, but it might have the disadvantage of not being able to send other commands while the program is running. If the program stops when it hits EOF on stdin then on the first echo "xxx" > cmd the program will stop (because the pipe will be closed). Though some programs are smart enough to reopen (rewind(3)) their stdin when they encounter EOF.

                            – Cristian Ciupitu
                            Sep 6 '10 at 12:30
















                          4














                          Try this to start:



                          # screen
                          # cd /path/to/wd
                          # mkfifo cmd
                          # my_cmd <cmd
                          C-A d


                          And this to kill:



                          # cd /path/to/wd
                          # echo "stop" > cmd
                          # rm cmd





                          share|improve this answer


















                          • 3





                            This is good, but it might have the disadvantage of not being able to send other commands while the program is running. If the program stops when it hits EOF on stdin then on the first echo "xxx" > cmd the program will stop (because the pipe will be closed). Though some programs are smart enough to reopen (rewind(3)) their stdin when they encounter EOF.

                            – Cristian Ciupitu
                            Sep 6 '10 at 12:30














                          4












                          4








                          4







                          Try this to start:



                          # screen
                          # cd /path/to/wd
                          # mkfifo cmd
                          # my_cmd <cmd
                          C-A d


                          And this to kill:



                          # cd /path/to/wd
                          # echo "stop" > cmd
                          # rm cmd





                          share|improve this answer













                          Try this to start:



                          # screen
                          # cd /path/to/wd
                          # mkfifo cmd
                          # my_cmd <cmd
                          C-A d


                          And this to kill:



                          # cd /path/to/wd
                          # echo "stop" > cmd
                          # rm cmd






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Sep 6 '10 at 12:03









                          krissikrissi

                          2,82611321




                          2,82611321







                          • 3





                            This is good, but it might have the disadvantage of not being able to send other commands while the program is running. If the program stops when it hits EOF on stdin then on the first echo "xxx" > cmd the program will stop (because the pipe will be closed). Though some programs are smart enough to reopen (rewind(3)) their stdin when they encounter EOF.

                            – Cristian Ciupitu
                            Sep 6 '10 at 12:30













                          • 3





                            This is good, but it might have the disadvantage of not being able to send other commands while the program is running. If the program stops when it hits EOF on stdin then on the first echo "xxx" > cmd the program will stop (because the pipe will be closed). Though some programs are smart enough to reopen (rewind(3)) their stdin when they encounter EOF.

                            – Cristian Ciupitu
                            Sep 6 '10 at 12:30








                          3




                          3





                          This is good, but it might have the disadvantage of not being able to send other commands while the program is running. If the program stops when it hits EOF on stdin then on the first echo "xxx" > cmd the program will stop (because the pipe will be closed). Though some programs are smart enough to reopen (rewind(3)) their stdin when they encounter EOF.

                          – Cristian Ciupitu
                          Sep 6 '10 at 12:30






                          This is good, but it might have the disadvantage of not being able to send other commands while the program is running. If the program stops when it hits EOF on stdin then on the first echo "xxx" > cmd the program will stop (because the pipe will be closed). Though some programs are smart enough to reopen (rewind(3)) their stdin when they encounter EOF.

                          – Cristian Ciupitu
                          Sep 6 '10 at 12:30












                          1














                          In case it helps anyone:

                          I had a similar problem, and as the process I was using wasn't under screen or tmux, I had to take a different approach.



                          I attached gdb to the xterm that my process was running in, and used call write(5, "stopn", 5) from gdb to write to the master pty file descriptor.

                          I found out which file descriptor to send the data to by looking at /proc/<pid>/fd for a link to /dev/ptmx and then trial and error between the two options (sending my string to both matching file descriptors seemed to cause no harm).



                          EDIT

                          It turned out that the xterm process I had attached to was spawned with the spawn-new-terminal() xterm action from a keybinding, and the second ptmx file descriptor open was simply the ptmx of the parent xterm process that hadn't been closed.

                          Hence the trial and error calls had sent output to that other terminal.

                          Most xterm processes don't have two ptmx file descriptors.



                          END EDIT

                          This effectively typed that string into the terminal, and hence sent it along to the process running under it.



                          n.b. you may need to allow attaching to a running process with something like
                          sudo bash -c "echo 0 > /proc/sys/kernel/yama/ptrace_scope"






                          share|improve this answer





























                            1














                            In case it helps anyone:

                            I had a similar problem, and as the process I was using wasn't under screen or tmux, I had to take a different approach.



                            I attached gdb to the xterm that my process was running in, and used call write(5, "stopn", 5) from gdb to write to the master pty file descriptor.

                            I found out which file descriptor to send the data to by looking at /proc/<pid>/fd for a link to /dev/ptmx and then trial and error between the two options (sending my string to both matching file descriptors seemed to cause no harm).



                            EDIT

                            It turned out that the xterm process I had attached to was spawned with the spawn-new-terminal() xterm action from a keybinding, and the second ptmx file descriptor open was simply the ptmx of the parent xterm process that hadn't been closed.

                            Hence the trial and error calls had sent output to that other terminal.

                            Most xterm processes don't have two ptmx file descriptors.



                            END EDIT

                            This effectively typed that string into the terminal, and hence sent it along to the process running under it.



                            n.b. you may need to allow attaching to a running process with something like
                            sudo bash -c "echo 0 > /proc/sys/kernel/yama/ptrace_scope"






                            share|improve this answer



























                              1












                              1








                              1







                              In case it helps anyone:

                              I had a similar problem, and as the process I was using wasn't under screen or tmux, I had to take a different approach.



                              I attached gdb to the xterm that my process was running in, and used call write(5, "stopn", 5) from gdb to write to the master pty file descriptor.

                              I found out which file descriptor to send the data to by looking at /proc/<pid>/fd for a link to /dev/ptmx and then trial and error between the two options (sending my string to both matching file descriptors seemed to cause no harm).



                              EDIT

                              It turned out that the xterm process I had attached to was spawned with the spawn-new-terminal() xterm action from a keybinding, and the second ptmx file descriptor open was simply the ptmx of the parent xterm process that hadn't been closed.

                              Hence the trial and error calls had sent output to that other terminal.

                              Most xterm processes don't have two ptmx file descriptors.



                              END EDIT

                              This effectively typed that string into the terminal, and hence sent it along to the process running under it.



                              n.b. you may need to allow attaching to a running process with something like
                              sudo bash -c "echo 0 > /proc/sys/kernel/yama/ptrace_scope"






                              share|improve this answer















                              In case it helps anyone:

                              I had a similar problem, and as the process I was using wasn't under screen or tmux, I had to take a different approach.



                              I attached gdb to the xterm that my process was running in, and used call write(5, "stopn", 5) from gdb to write to the master pty file descriptor.

                              I found out which file descriptor to send the data to by looking at /proc/<pid>/fd for a link to /dev/ptmx and then trial and error between the two options (sending my string to both matching file descriptors seemed to cause no harm).



                              EDIT

                              It turned out that the xterm process I had attached to was spawned with the spawn-new-terminal() xterm action from a keybinding, and the second ptmx file descriptor open was simply the ptmx of the parent xterm process that hadn't been closed.

                              Hence the trial and error calls had sent output to that other terminal.

                              Most xterm processes don't have two ptmx file descriptors.



                              END EDIT

                              This effectively typed that string into the terminal, and hence sent it along to the process running under it.



                              n.b. you may need to allow attaching to a running process with something like
                              sudo bash -c "echo 0 > /proc/sys/kernel/yama/ptrace_scope"







                              share|improve this answer














                              share|improve this answer



                              share|improve this answer








                              edited Dec 31 '17 at 9:52

























                              answered Dec 31 '17 at 9:42









                              AppleApple

                              112




                              112





















                                  1














                                  It is possible to send input text to a running process without running the screen utility, or any other fancy utility. And it can be done by sending this input text to the process' standard input "file" /proc/PID#/fd/0.



                                  However, the input text needs to be sent in a special way to be read by the process. Sending the input text via the regular file write method will not cause the process to receive the text. This is because doing so will only append to that "file", but will not trigger the process to read the bytes.



                                  To trigger the process to read the bytes, it is necessary to do an IOCTL operation of type TIOCSTI for every single byte to be sent. This will place the byte into the process' standard input queue.



                                  This is discussed here with some examples in C, Perl, and Python:



                                  https://unix.stackexchange.com/questions/48103/construct-a-command-by-putting-a-string-into-a-tty/48221



                                  --



                                  So to answer the original question asked almost 9 years ago, the cron job would need to run some small utility script / program similar to the examples people wrote for that other question, which would send the string "stopn" to that server process in the question, by sending each of the 5 bytes via an IOCTL operation of type TIOCSTI.



                                  Of course this will only work on systems that support the TIOCSTI IOCTL operation type (like Linux), and only from the root user account, as these "files" under /proc/ are "owned" by root.






                                  share|improve this answer










                                  New contributor




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
























                                    1














                                    It is possible to send input text to a running process without running the screen utility, or any other fancy utility. And it can be done by sending this input text to the process' standard input "file" /proc/PID#/fd/0.



                                    However, the input text needs to be sent in a special way to be read by the process. Sending the input text via the regular file write method will not cause the process to receive the text. This is because doing so will only append to that "file", but will not trigger the process to read the bytes.



                                    To trigger the process to read the bytes, it is necessary to do an IOCTL operation of type TIOCSTI for every single byte to be sent. This will place the byte into the process' standard input queue.



                                    This is discussed here with some examples in C, Perl, and Python:



                                    https://unix.stackexchange.com/questions/48103/construct-a-command-by-putting-a-string-into-a-tty/48221



                                    --



                                    So to answer the original question asked almost 9 years ago, the cron job would need to run some small utility script / program similar to the examples people wrote for that other question, which would send the string "stopn" to that server process in the question, by sending each of the 5 bytes via an IOCTL operation of type TIOCSTI.



                                    Of course this will only work on systems that support the TIOCSTI IOCTL operation type (like Linux), and only from the root user account, as these "files" under /proc/ are "owned" by root.






                                    share|improve this answer










                                    New contributor




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






















                                      1












                                      1








                                      1







                                      It is possible to send input text to a running process without running the screen utility, or any other fancy utility. And it can be done by sending this input text to the process' standard input "file" /proc/PID#/fd/0.



                                      However, the input text needs to be sent in a special way to be read by the process. Sending the input text via the regular file write method will not cause the process to receive the text. This is because doing so will only append to that "file", but will not trigger the process to read the bytes.



                                      To trigger the process to read the bytes, it is necessary to do an IOCTL operation of type TIOCSTI for every single byte to be sent. This will place the byte into the process' standard input queue.



                                      This is discussed here with some examples in C, Perl, and Python:



                                      https://unix.stackexchange.com/questions/48103/construct-a-command-by-putting-a-string-into-a-tty/48221



                                      --



                                      So to answer the original question asked almost 9 years ago, the cron job would need to run some small utility script / program similar to the examples people wrote for that other question, which would send the string "stopn" to that server process in the question, by sending each of the 5 bytes via an IOCTL operation of type TIOCSTI.



                                      Of course this will only work on systems that support the TIOCSTI IOCTL operation type (like Linux), and only from the root user account, as these "files" under /proc/ are "owned" by root.






                                      share|improve this answer










                                      New contributor




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










                                      It is possible to send input text to a running process without running the screen utility, or any other fancy utility. And it can be done by sending this input text to the process' standard input "file" /proc/PID#/fd/0.



                                      However, the input text needs to be sent in a special way to be read by the process. Sending the input text via the regular file write method will not cause the process to receive the text. This is because doing so will only append to that "file", but will not trigger the process to read the bytes.



                                      To trigger the process to read the bytes, it is necessary to do an IOCTL operation of type TIOCSTI for every single byte to be sent. This will place the byte into the process' standard input queue.



                                      This is discussed here with some examples in C, Perl, and Python:



                                      https://unix.stackexchange.com/questions/48103/construct-a-command-by-putting-a-string-into-a-tty/48221



                                      --



                                      So to answer the original question asked almost 9 years ago, the cron job would need to run some small utility script / program similar to the examples people wrote for that other question, which would send the string "stopn" to that server process in the question, by sending each of the 5 bytes via an IOCTL operation of type TIOCSTI.



                                      Of course this will only work on systems that support the TIOCSTI IOCTL operation type (like Linux), and only from the root user account, as these "files" under /proc/ are "owned" by root.







                                      share|improve this answer










                                      New contributor




                                      maratbn 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 answer



                                      share|improve this answer








                                      edited Apr 14 at 4:56





















                                      New contributor




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









                                      answered Apr 14 at 4:40









                                      maratbnmaratbn

                                      112




                                      112




                                      New contributor




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





                                      New contributor





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






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





















                                          0














                                          Since I cannot comment the most accepted answer of Cristian Ciupitu (of 2010), I have to put this in a separate answer:



                                          This question was already solved in this thread: https://stackoverflow.com/questions/5374255/how-to-write-data-to-existing-processs-stdin-from-external-process



                                          In short:



                                          You have to start your process with a pipe for stdin which does not block nor close when the current input was written through. This can be implemented by a simple endless loop which will be piped to the process in question:



                                          $ (while [ 1 ]; do sleep 1; done) | yourProgramToStart


                                          I can confirm that this is different of krissi's way to open a pipe which was not working in my case. The shown solution did work instead.



                                          You can then write to the .../fd/0 file of the process to send instructions to it. The only drawback is that you need to terminate the bash process as well which is executing the endless-loop after the server did shut down.






                                          share|improve this answer



























                                            0














                                            Since I cannot comment the most accepted answer of Cristian Ciupitu (of 2010), I have to put this in a separate answer:



                                            This question was already solved in this thread: https://stackoverflow.com/questions/5374255/how-to-write-data-to-existing-processs-stdin-from-external-process



                                            In short:



                                            You have to start your process with a pipe for stdin which does not block nor close when the current input was written through. This can be implemented by a simple endless loop which will be piped to the process in question:



                                            $ (while [ 1 ]; do sleep 1; done) | yourProgramToStart


                                            I can confirm that this is different of krissi's way to open a pipe which was not working in my case. The shown solution did work instead.



                                            You can then write to the .../fd/0 file of the process to send instructions to it. The only drawback is that you need to terminate the bash process as well which is executing the endless-loop after the server did shut down.






                                            share|improve this answer

























                                              0












                                              0








                                              0







                                              Since I cannot comment the most accepted answer of Cristian Ciupitu (of 2010), I have to put this in a separate answer:



                                              This question was already solved in this thread: https://stackoverflow.com/questions/5374255/how-to-write-data-to-existing-processs-stdin-from-external-process



                                              In short:



                                              You have to start your process with a pipe for stdin which does not block nor close when the current input was written through. This can be implemented by a simple endless loop which will be piped to the process in question:



                                              $ (while [ 1 ]; do sleep 1; done) | yourProgramToStart


                                              I can confirm that this is different of krissi's way to open a pipe which was not working in my case. The shown solution did work instead.



                                              You can then write to the .../fd/0 file of the process to send instructions to it. The only drawback is that you need to terminate the bash process as well which is executing the endless-loop after the server did shut down.






                                              share|improve this answer













                                              Since I cannot comment the most accepted answer of Cristian Ciupitu (of 2010), I have to put this in a separate answer:



                                              This question was already solved in this thread: https://stackoverflow.com/questions/5374255/how-to-write-data-to-existing-processs-stdin-from-external-process



                                              In short:



                                              You have to start your process with a pipe for stdin which does not block nor close when the current input was written through. This can be implemented by a simple endless loop which will be piped to the process in question:



                                              $ (while [ 1 ]; do sleep 1; done) | yourProgramToStart


                                              I can confirm that this is different of krissi's way to open a pipe which was not working in my case. The shown solution did work instead.



                                              You can then write to the .../fd/0 file of the process to send instructions to it. The only drawback is that you need to terminate the bash process as well which is executing the endless-loop after the server did shut down.







                                              share|improve this answer












                                              share|improve this answer



                                              share|improve this answer










                                              answered Mar 25 at 16:23









                                              user3471872user3471872

                                              1




                                              1



























                                                  draft saved

                                                  draft discarded
















































                                                  Thanks for contributing an answer to Server Fault!


                                                  • 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%2fserverfault.com%2fquestions%2f178457%2fcan-i-send-some-text-to-the-stdin-of-an-active-process-running-in-a-screen-sessi%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

                                                  Club Baloncesto Breogán Índice Historia | Pavillón | Nome | O Breogán na cultura popular | Xogadores | Adestradores | Presidentes | Palmarés | Historial | Líderes | Notas | Véxase tamén | Menú de navegacióncbbreogan.galCadroGuía oficial da ACB 2009-10, páxina 201Guía oficial ACB 1992, páxina 183. Editorial DB.É de 6.500 espectadores sentados axeitándose á última normativa"Estudiantes Junior, entre as mellores canteiras"o orixinalHemeroteca El Mundo Deportivo, 16 setembro de 1970, páxina 12Historia do BreogánAlfredo Pérez, o último canoneiroHistoria C.B. BreogánHemeroteca de El Mundo DeportivoJimmy Wright, norteamericano do Breogán deixará Lugo por ameazas de morteResultados de Breogán en 1986-87Resultados de Breogán en 1990-91Ficha de Velimir Perasović en acb.comResultados de Breogán en 1994-95Breogán arrasa al Barça. "El Mundo Deportivo", 27 de setembro de 1999, páxina 58CB Breogán - FC BarcelonaA FEB invita a participar nunha nova Liga EuropeaCharlie Bell na prensa estatalMáximos anotadores 2005Tempada 2005-06 : Tódolos Xogadores da Xornada""Non quero pensar nunha man negra, mais pregúntome que está a pasar""o orixinalRaúl López, orgulloso dos xogadores, presume da boa saúde económica do BreogánJulio González confirma que cesa como presidente del BreogánHomenaxe a Lisardo GómezA tempada do rexurdimento celesteEntrevista a Lisardo GómezEl COB dinamita el Pazo para forzar el quinto (69-73)Cafés Candelas, patrocinador del CB Breogán"Suso Lázare, novo presidente do Breogán"o orixinalCafés Candelas Breogán firma el mayor triunfo de la historiaEl Breogán realizará 17 homenajes por su cincuenta aniversario"O Breogán honra ao seu fundador e primeiro presidente"o orixinalMiguel Giao recibiu a homenaxe do PazoHomenaxe aos primeiros gladiadores celestesO home que nos amosa como ver o Breo co corazónTita Franco será homenaxeada polos #50anosdeBreoJulio Vila recibirá unha homenaxe in memoriam polos #50anosdeBreo"O Breogán homenaxeará aos seus aboados máis veteráns"Pechada ovación a «Capi» Sanmartín e Ricardo «Corazón de González»Homenaxe por décadas de informaciónPaco García volve ao Pazo con motivo do 50 aniversario"Resultados y clasificaciones""O Cafés Candelas Breogán, campión da Copa Princesa""O Cafés Candelas Breogán, equipo ACB"C.B. Breogán"Proxecto social"o orixinal"Centros asociados"o orixinalFicha en imdb.comMario Camus trata la recuperación del amor en 'La vieja música', su última película"Páxina web oficial""Club Baloncesto Breogán""C. B. Breogán S.A.D."eehttp://www.fegaba.com

                                                  Vilaño, A Laracha Índice Patrimonio | Lugares e parroquias | Véxase tamén | Menú de navegación43°14′52″N 8°36′03″O / 43.24775, -8.60070

                                                  Cegueira Índice Epidemioloxía | Deficiencia visual | Tipos de cegueira | Principais causas de cegueira | Tratamento | Técnicas de adaptación e axudas | Vida dos cegos | Primeiros auxilios | Crenzas respecto das persoas cegas | Crenzas das persoas cegas | O neno deficiente visual | Aspectos psicolóxicos da cegueira | Notas | Véxase tamén | Menú de navegación54.054.154.436928256blindnessDicionario da Real Academia GalegaPortal das Palabras"International Standards: Visual Standards — Aspects and Ranges of Vision Loss with Emphasis on Population Surveys.""Visual impairment and blindness""Presentan un plan para previr a cegueira"o orixinalACCDV Associació Catalana de Cecs i Disminuïts Visuals - PMFTrachoma"Effect of gene therapy on visual function in Leber's congenital amaurosis"1844137110.1056/NEJMoa0802268Cans guía - os mellores amigos dos cegosArquivadoEscola de cans guía para cegos en Mortágua, PortugalArquivado"Tecnología para ciegos y deficientes visuales. Recopilación de recursos gratuitos en la Red""Colorino""‘COL.diesis’, escuchar los sonidos del color""COL.diesis: Transforming Colour into Melody and Implementing the Result in a Colour Sensor Device"o orixinal"Sistema de desarrollo de sinestesia color-sonido para invidentes utilizando un protocolo de audio""Enseñanza táctil - geometría y color. Juegos didácticos para niños ciegos y videntes""Sistema Constanz"L'ocupació laboral dels cecs a l'Estat espanyol està pràcticament equiparada a la de les persones amb visió, entrevista amb Pedro ZuritaONCE (Organización Nacional de Cegos de España)Prevención da cegueiraDescrición de deficiencias visuais (Disc@pnet)Braillín, un boneco atractivo para calquera neno, con ou sen discapacidade, que permite familiarizarse co sistema de escritura e lectura brailleAxudas Técnicas36838ID00897494007150-90057129528256DOID:1432HP:0000618D001766C10.597.751.941.162C97109C0155020