1

I have 5 processes running from one terminal (T1). All of them are running in background, but generate a lot of output.

Now from other terminal (T2), I want to kill one of them using KILL pid command. Then after 60 secs, I want to restart the same process (this will get a different pid obviously).

My script would look like

KILL 1524
sleep 60
myProcess

The problem is that after this, the terminal T2 also becomes unusable, due to outputs of the process. If I want to do the same thing again, I will have to start another terminal. Is it possible to something that would force the process to start in T1?

  • 1
    Some ideas: write the output to a logfile and tail it - so it does not matter in what terminal you started the processes. Else you could look at screen and/or tmux those could solve the problem as well. – Phillip -Zyan K Lee- Stockmann Jul 21 '16 at 20:44
  • 1
    Can you put a while :; do someprogram; sleep 60; done loop around the program so after a kill it restarts itself? – thrig Jul 21 '16 at 20:47
  • 1
    You can not spawn processes the way you want, all precesses run by a shell are always child of such shell, to accomplish what you want you can redirect the output to a file appendnting >my_file or even to /dev/null if you dont care for the output >/dev/null, that way you will be able to continue using the same terminal. – Dalvenjia Jul 21 '16 at 20:53
  • This may not be what the questioner needs, but can you redirect a processes output to another terminal using screen? – ctrl-alt-delor Jul 21 '16 at 21:48
3

So, you can have the output appear in the other terminal—though I doubt you really want to. To do so:

  1. Find the tty of the terminal you'd like the output to go to; the easiest way is to run tty. This should print something like: /dev/pts/42.

  2. In the other terminal, run: command > /dev/pts/42 &. If you want to do stderr as well as stdout: command > /dev/pts/42 2>&1 &

That will only work for the same user (due to permissions), and it doesn't redirect input (and redirecting input won't really work, as you'll be fighting the shell for it).

A much better solution is to redirect the output to a file (command > outfile), then you can use less, tail, etc. to watch it. Or, use screen/tmux to run multiple sessions inside one terminal.

  • see also nohup and sigstop/sigcont, – ctrl-alt-delor Jul 21 '16 at 21:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.