Ajish Alfred
Ajish Alfred

Reputation: 155

Resuming a stopped process in another terminal window

I've executed the following C code in Linux CentOS to create a process.

#include <stdio.h>
#include <unistd.h>

int main ()
{
        int i = 0;

        while ( 1 )
        {
              printf ( "\nhello %d\n", i ++ );
              sleep ( 2 );
        }
}

I've compiled it to hello_count. When I do ./hello_count in a terminal window, The output is like this:

hello 0
hello 1
hello 2
...

When I enter the command in another terminal window

ps -e

the process 2956 ./hello_count is listed there. In the same window, I've stopped the process using:

kill -s SIGSTOP 2956

When I enter the following command again,

ps -e

the process 2956 ./hello_count is still listed.

Then I've entered the following command to resume the process in the same window itself.

kill -s SIGCONT 2956

However, the process resumed in the previous window in which it was executing and giving output.

Is there any command or any method to resume (not to restart) the process with pid 2956 in a different terminal window?

I mean, I need the output like,

hello 8
hello 9
...

in a window other than the one in which I was getting the above output before I've stopped the process.

Upvotes: 3

Views: 2943

Answers (1)

Benj
Benj

Reputation: 32398

The problem you're having is that your process is attached to a particular tty and switching tty once a process is started isn't normally possible. See this question.

There are some hacky methods you could consider mind you.

For real world command line scenarios, using screen would allow you to start a command in a virtual terminal and then connect to that terminal from any other. But this isn't a programatic solution which your question seems to indicate you're looking for.

Upvotes: 2

Related Questions