0

When I run a cat command as a background process,

$ cat &

and then try to kill it,

$ killall -v cat

it says that it killed cat with signal 15, but also that cat was only stopped. Upon running $ ps, I see that it is still running. It isn't until I call $ fg that it is finally terminated.

It appears to be related to waiting for input, because when simulating cat in c++,

string line;
while(getline(cin, line))
   cout << line << endl;

the same thing happens, but for a simple while(true); loop, the process is killed successfully.

It also appears to be necessary that it is a background process because when I run $ cat and in another terminal $ killall -v cat it also works fine.

What is going on here? Thanks in advance.

1 Answers1

3

When you run

cat &

you should see a message saying

[1]+  Stopped                 cat

shortly thereafter (perhaps with a different job number). This is because cat tries to read from its standard input, so when it’s run in the background, it ends up being stopped.

Stopped processes don’t receive signals other than SIGKILL until they are resumed (continued). This is what is happening here: your SIGTERM is only delivered when the process is continued, which happens when you return it to the foreground.

Stephen Kitt
  • 434,908