Stop All Processes With killall and kill Commands On FreeBSD

You may have faced a situation when you run an application, suddenly the application or your system becomes unresponsive. The solution, you try to restart the application and you have to close it first.

However, if the application you closed fails to stop or is turned off completely you will not be able to start it again. Yes, we have all faced situations like this at least once while running a computer system. To resolve such issues, you should terminate your application processes that are not running. But now the question is how? In this article, you will understand two different approaches to kill all processes on the FreeBSD operating system.

1. What is kill & killall

Before we dive into specific examples of using the kill and killal commands, it's a good idea to understand the basic concepts of these two commands.

When an application fails to terminate, it continues to consume memory and processing power. Fortunately, FreeBSD provides utilities or utility commands that help you stop processes that won't stop. Alright, now let's dig into the two different Linux kill process commands and the killall command, and how they work.

kill is a basic command line utility on FreeBSD that can send a signal to a process to terminate it. there is FreeBSD, by default kill sends a TERM signal. This signal prompts the process to terminate itself gracefully. If a process does not respond to the TERM signal, it can send a KILL signal, which immediately terminates the process.

killall is a tool to kill processes running on your FreeBSD system by name. Meanwhile, kill will stop the process based on the Process ID number (PID). kill and killall can also send certain system signals to the application that will be killed.

Use killall and kill in conjunction with tools including Process Status, ps, to manage and terminate stuck or unresponsive processes.

2. System Signals FreeBSD

On FreeBSD signals were originally designed to model extraordinary events, such as a user's attempt to terminate a program that was not working or was difficult to kill. Signals are not intended to be used as a general interprocess communication mechanism, and thus no effort is made to make them reliable. In previous systems, every time a signal was caught, the action was reset to the default action. The introduction of job control caused signal usage to become much more frequent and created a more severe problem that was also exacerbated by faster processors.

If two signals are sent simultaneously and done quickly, the second signal can cause the process to stop, even though the signal controller has been set to catch the first signal. At this point, reliability becomes desirable, so developers design new frameworks that contain the old capabilities in part while accommodating the new mechanisms.

Signals allow manipulation of a process from outside its domain as well as allowing the process to manipulate itself or copy itself. There are two general types of signals:
  1. Signals that cause the termination of a process and,
  2. Signals that do not cause process termination.
The signal that causes a program to terminate may be due to an uncorrectable error or may be caused by the terminal user typing the `interrupt' character.

Signals are used when a process is stopped because it wants to access its control terminal while in the background. Signals are optionally generated when a process is resumed after being stopped, when the state of a child process changes, or when input is ready at the control terminal. Most signals result in the termination of their reception process if no action is taken; some signals actually cause the reception process to stop, or are simply discarded if the process does not request otherwise. Except for the SIGKILL and SIGSTOP signals, the signal function allows signals to be captured, ignored, or generate an interrupt. These signals are defined in the file "signal.h".

Num   Name                Default Action                Description
1         SIGHUP              terminate process            terminal line hangup
2         SIGINT               terminate process             interrupt program
3         SIGQUIT            create core image             quit program
4         SIGILL                create core image             illegal instruction
5         SIGTRAP            create core image             trace trap
6         SIGABRT           create core image              abort program (formerly SIGIOT)
7         SIGEMT             create core image              emulate instruction executed

3. How to Use Kill

The Terminate process command is an important utility that every FreeBSD system administrtor should know about. If you fail to stop any daemons, the FreeBSD kill process and the FreeBSD killall command will help you stop any daemons that are difficult to kill.

The kill command can stop each individual process as determined by its PID.

The command takes the following form:

# kill [-s signal_name] pid
# kill -l [exit_status]
# kill -signal_name pid
# kill -signal_number pid

The kill utility sends a signal to the process specified by the pid number. Killing processes can be done by any user, but only superusers can send signals to other users' processes.

The options are as follows:

-s signal_name A symbolic signal name specifying the signal to be sent instead of the default TERM.
-l [exit_status] If no operand is given, list the signal names; otherwise, write the signal name corresponding to exit_status.
-signal_name  A symbolic signal name specifying the signal to be sent instead of the default TERM.
-signal_number A non-negative decimal integer, specifying the signal to be sent instead of the default TERM.

The following PIDs have special meanings:
-1 If  superuser, broadcast the signal to all processes; otherwise broadcast to all processes belonging to the user.

Some of the more commonly used signals:
  • HUP (hang up).
  • INT (interrupt).
  • QUIT (quit).
  • ABRT (abort).
  • KILL (non-catchable, non-ignorable kill).
  • ALRM (alarm clock).
  • TERM (software termination signal).
Below we provide an example of how to use the kill command. In this example we will try to stop the Redis application daemon.
root@ns7:~ # ps -auwx | grep redis
redis 3046   0.0  0.5   34604   9620  -  Ss   07:33     0:00.01 redis-server: /usr/local/bin/redis-server (redis-server)
root  3048   0.0  0.1   12812   2368  0  S+   07:33     0:00.00 grep redis
In the command above the Redis PID number is 3046, now we will force the Redis daemon to stop with the kill command.
root@ns7:~ # kill -QUIT 3046
root@ns7:~ # kill -s QUIT 3046
If your computer does not respond to Redis signals, you can use HUP to stop the Redis daemon
root@ns7:~ # kill -HUP 3046
root@ns7:~ # kill -s HUP 3046

4. How to Use Killall

On FreeBSD, the killall utility is used to kill processes selected by name, rather than selection by PID as is done with kill. By default, killall will send a TERM signal to all processes with a real UID identical to the caller of killall that matches the procname. Only superusers are allowed to terminate any process.

In general, the killal command can be written as follows:

# killall procname

Below we provide an example of how to stop the mysql-server daemon. As we know, on FreeBSD by default mysql-server has a username and group called "mysql".

Use the ps -auwx command to see the PID of the running mysql-server.
root@ns7:~ # ps -auwx | grep mysql
mysql 6940  78.2 31.5 2304128 563976  -  S    08:16     0:02.67 /usr/local/libexec/mysqld --defaults-extra-file=/usr/local/etc/mysql/my.cnf --basedir=/usr/local --datadir=/var/
mysql 5785   0.0  0.2   13580   3164  -  Ss   08:16     0:00.05 /bin/sh /usr/local/bin/mysqld_safe --defaults-extra-file=/usr/local/etc/mysql/my.cnf --basedir=/usr/local --data
root  6947   0.0  0.1   12812   2356  0  S+   08:16     0:00.00 grep mysql
In the above command the mysql-server daemon is named "mysqld". Now we try to turn off the mysql-server daemon with the killall command.
root@ns7:~ # killall mysqld
Let's see the results with the ps -auwx command.
root@ns7:~ # ps -auwx | grep mysql
root  6970   0.0  0.1 12812 2364  0  S+   08:20     0:00.00 grep mysql
In the command above, the mysql-server daemon has been successfully shut down.

We can also turn off the mysql-server daemon by combining "username" and "procname".
root@ns7:~ # killall -u mysql mysqld
In the command above "mysql" is the username of mysql-server, and "mysqld" is the daemon of mysql-server.

Understanding the kill and killall commands will be very helpful for a system administrator in carrying out his work. You must master these two commands, if you want to learn FreeBSD.
Iwan Setiawan

I Like Adventure: Mahameru Mount, Rinjani Mount I Like Writer FreeBSD

Post a Comment

Previous Post Next Post