On my previous article, I've talked about Linux Signals and how you can send a specific signal with the
On this article, I will talk a bit more about job control. How you can see running processes and how to use the knowledge we gained with the Linux signals article to send signals to a process.
Everything running in a Linux system is considered a process. Every time a process is started, the operating system will assign a unique PID (Process Identification Number) to that process.
This is important to know because when a process doesn't terminate as you expect, or something causes it to hang and keep consuming resources, you might need to run the
kill command followed by the PID of that process.
But How do you get a list of all running commands?
Two useful commands can help you get a list of the running processes - the
ps command and the
Let's get started with the
ps command will show you information about running processes. If you want to get a list of all processes you can run
ps aux - these will include processes started by other users including root.
ps aux gives you so much information, it's often a good idea to pipe a
grep to your command, to get only processes that match the thing that you are looking for.
For example, this command will only show running processes that contain the word opsdroid:
> ps aux | grep opsdroid
Now let's have a look at the
This command is not a command that comes with Linux but is instead a bash builtin. It allows you to get information about processes spawned by the current shell.
jobs shows you only processes started by the current shell, it can be easier to navigate and to find a specific process.
Let's assume that you have a few processes running in the background, you can run the
jobs command to get a list of all the process, their status (running or stopped) and which command made that process run.
If you want to get the PID of these processes you can use the
-l flag with the command.
> jobs  + running python3 -m opsdroid start
Getting the PID of the process with the
> jobs -l  + 3258 running python3 -m opsdroid start
Now that we know how to get a list of processes and the respective PID's, let's talk a bit about the
kill command. This command is an interesting one because what this command does is sending a signal to a process.
The default signal of
kill is the
SIGTERM signal, which is a gentle request to terminate a specific process. But since this command is used to send signals to a process, we could easily send a stop signal with the command
If you want to know what signals you can send with
kill you can run the command with the
For example, running the command on my ubuntu machine gives me the following:
❯ kill -l HUP INT QUIT ILL TRAP ABRT BUS FPE KILL USR1 SEGV USR2 PIPE ALRM TERM STKFLT CHLD CONT STOP TSTP TTIN TTOU URG XCPU XFSZ VTALRM PROF WINCH POLL PWR SYS
Note: Sending a
SIGKILLto a process will terminate a program no matter what, but that might leave orphan processes running, which might cause some odd behaviours. This means running
kill -9 <PID>or
kill -KILL <PID>might not be a good idea.
Why knowing about processes matter?
You might be wondering, why I talked about processes before talking about the
kill command. Well,
kill needs the PID of the process that you want to send the signal to.
So knowing how to get that PID comes in handy. Another good thing that is worth mention is that you can pass more than one PID to
kill, this allows you to send a signal to multiple processes.
Using a process name to send signals
You might be thinking that fetching a process PID and remembering it is too much work. Luckily, you can also send signals to a process by using its name. The way to do that is by using the
The thing to remember here is that you need to pass the exact name of the process that you want to kill.
If you want to use just part of the name, there is another command for that - you can use the
But there is something to keep in mind.
Let's say that you want to terminate a process and you only know part of its name. Using
pkill opsdroid will kill any process that contains opsdroid in its name. This could terminate a process that you might not want to because it contains opsdroid on its name.
This is why using the exact name or the unique PID might be a better way to send signals to a process.
I hope this article helped you learn more about processes and how you can send signals to a specific process. Let me know if you have a favourite way to search for a process and what you use to send signals to a process.