UNIX Kill Flags
To start, let's go over the flags that are available in UNIX's kill command. When enter the following command in a UNIX shell, you get the following list of flags that can be sent to a process:
kill -l
The most relevant flags are the ones from 1 → 15. However, we won't go over them all. To summarize some:
SIGINT
Equivalent to pressing CTRL + C in a shell. It is used to terminate a process gracefully.
SIGQUIT
Equivalent to pressing CTRL + / in a shell.
SIGKILL
Forcibly terminate the process without waiting for the resources to be set free or the children processes to be killed.
SIGTERM
Ensures that the process will be killed alongside its children processes. To note here, that Java's UNIX implementation of java.lang.Process.destroy() uses this flag.
How to Send a Flag to a Process
To send one of the previous flags to a process which you know its pid (process ID), you can do one of the following:
kill -{number} pid # e.g. kill -2 16342
kill -SIG{signal} pid # e.g. kill -SIGINT 16342
kill -{signal} pid # e.g. kill -INT 16342
However, not all flags can be used with any process! For example, SIGINT, which is the equivalent of CTRL + C for graceful termination, cannot be used with background processes.
How to Know if a Process is a Background Process
When you look for processes in UNIX, using the following command:
ps aux
You will get a list similar to this:
In the 8th column, the one with the label STAT, gives us a summary of the status of the process. Foreground processes contain the plus "+" sign in their status, while background processes do not. For more information about the translation of the characters in the process status, check this article.
How to Send SIGINT Flag to a Background Process?
Although it is hard to do so, you may still be able to send the SIGINT flag to a background process in one of the following ways:
1. If the process has a process group ID (pgid), you can send the flag directly to the group, and it gets delegated to all the processes in this group. You can do so as follows:
kill -SIGINT -{pgid} # e.g. kill -SIGINT -87341
2. You can move a background process to the foreground if you are in the same session where the job got started from. To do you will need to use the fg UNIX command.