2

Here https://unix.stackexchange.com/a/104825/109539 man says that to stop background process kill + PID must be used. However I can't stop background process using kill +PID, only kill + JOB ID

[KPE@home Temp]$ jobs
[KPE@home Temp]$ ps
  PID TTY          TIME CMD
13270 pts/0    00:00:00 bash
23257 pts/0    00:00:00 ps
[KPE@home Temp]$ mc &
[1] 23258
[KPE@home Temp]$ ps
                             PID TTY          TIME CMD
13270 pts/0    00:00:00 bash
23258 pts/0    00:00:00 bash
23262 pts/0    00:00:00 mc
23264 pts/0    00:00:00 ps

[1]+  Stopped                 . /usr/libexec/mc/mc-wrapper.sh
[KPE@home Temp]$ kill -s 15 23262
[KPE@home Temp]$ ps
  PID TTY          TIME CMD
13270 pts/0    00:00:00 bash
23258 pts/0    00:00:00 bash
23262 pts/0    00:00:00 mc
23266 pts/0    00:00:00 ps
[KPE@home Temp]$ kill %1
[1]+  Terminated              . /usr/libexec/mc/mc-wrapper.sh
[KPE@home Temp]$ ps
  PID TTY          TIME CMD
13270 pts/0    00:00:00 bash
23267 pts/0    00:00:00 ps
[KPE@home Temp]$ 

So the question - how to stop bg process via kill by pid

4

If you C-Z the mc program (=send it a SIGSTP or SIGSTOP = suspend it (will show as "Stopped" in the shell)), it won't be immediately receptive to any more signals (other than SIGKILL, but using that one isn't very nice) until it is resumed. Once you resume it with SIGCONT, it will accept your SIGTERM signal (and the signals that queued up for it while it was suspended).

kill -CONT $!; kill -TERM $! # $! refers to the pid of the last-spawned job

kill %1 works because shell's built-in kill probably does these two steps under the hood.

1

First get the pid

ps aux | grep process_name works for me

You should get output like:

root      2545  0.0  0.0  85128  3128 ?        Ss   Apr19   0:07 /usr/sbin/apache2 -k start
www-data  2569  0.0  0.0 2011388 10132 ?       Sl   Apr19   1:19 /usr/sbin/apache2 -k start
www-data  2570  0.0  0.0 2011380 10108 ?       Sl   Apr19   1:22 /usr/sbin/apache2 -k start
coteyr   23344  0.0  0.0  11748   928 pts/3    S+   10:12   0:00 grep --colour=auto apache

Then you can kill 2545 or kill -9 2545 or what ever you like.

That said, you want to avoid using kill if you can. Use the "proper" method for killing the process. There usually is some kind of clean up a background process does that you could be missing. Still sometimes there is no other way.

  • It looks like on your example something is not terminating on signal 15. SIGTERM is a "suggestion" to terminate, and gives the receiving thread the ability to handle cleanup etc. major.io/2010/03/18/sigterm-vs-sigkill You may want to try SIGKILL instead. – coteyr Apr 22 '15 at 14:19

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.