استفاده از دستورات kill, pkill و killall
دستور kill یکی از دستورات لینوکسیه که کاربردهای بسیاری داره، مخصوصاَ در مواقعی که میخواهید یه برنامهی خاص یا یه پروسه رو ببندید یا از حافظه خارجش کنید.
در لینوکس هر پروسه یه شماره یا Process ID داره که از طریق اون شماره میشه بهش دسترسی داشت. هر بار که برنامهای اجرا میشه برای اون برنامه یه Process ID منحصر به فرد تولید میشه. مثلاَ اولین فرایندی که هنگام بوت لینوکس شروع میشه پروسهی init هست که به اون شمارهی Pid یک اختصاص داده میشه. در سیستمهای جدید init با systemd جایگزین شده.
1- Kill
kill با Pid ها کار میکنه پس برای کشتن یه برنامه باید شماره Pid اون برنامه رو بدونیم. کار دستور kill اینه که به یه پروسهی خاص یه سیگنال میفرسته تا عملیاتی بر روی اون پروسه انجام بشه. مثلاَ اگه سیگنال شماره ۹ رو به یه پروسه بفرستید، پروسه kill میشه یا از بین میره. برای kill کردن یه پروسه میشه از یکی از سه سیگنال زیر استفاده کرد.
Signal Name Signal Value Behaviour
SIGHUP 1 Hangup
SIGKILL 9 Kill Signal
SIGTERM 15 Terminate
استفاده از سیگنال SIGTERM امن ترین روش kill کردن یه پروسه و SIGHUP و SIGKILL به ترتیب دارای امنیت کمتری هستند. مثلاَ SIGKILL در هنگام بستن یه پروسه هیچ دادهای رو در مورد اون پروسه ذخیره نمیکنه و ممکنه که بخشی از اطلاعات از بین برن برای همین در استفاده از این سیگنال دقت کنید.
برای اینکه شماره Pid هر پروسه رو بدست بیارید از دستور ps استفاده کنید.
% ps -A
PID TTY TIME CMD
1 ? 00:00:01 systemd
2 ? 00:00:00 kthreadd
3 ? 00:00:00 ksoftirqd/0
5 ? 00:00:00 kworker/0:0H
7 ? 00:00:00 migration/0
8 ? 00:00:00 rcu_bh
9 ? 00:00:01 rcu_sched
10 ? 00:00:00 watchdog/0
11 ? 00:00:00 watchdog/1
12 ? 00:00:00 migration/1
13 ? 00:00:00 ksoftirqd/1
15 ? 00:00:00 kworker/1:0H
16 ? 00:00:00 watchdog/2
17 ? 00:00:00 migration/2
18 ? 00:00:00 ksoftirqd/2
20 ? 00:00:00 kworker/2:0H
21 ? 00:00:00 watchdog/3
22 ? 00:00:00 migration/3
23 ? 00:00:00 ksoftirqd/3
25 ? 00:00:00 kworker/3:0H
26 ? 00:00:00 khelper
27 ? 00:00:00 kdevtmpfs
28 ? 00:00:00 netns
29 ? 00:00:00 writeback
30 ? 00:00:00 kintegrityd
با استفاده از دستور pidof هم میتونید شمارهی Pid یه پروسه رو با استفاده از نامش پیدا کنید.
% pidof firefox
1558
حتی میتونید اینطوری لقمه رو دور سرتون بچرخونید :)
% ps aux | grep firefox
ali 1558 14.3 3.7 1169204 225732 ? Sl Nov20 4:50 /usr/lib64/firefox/firefox
ali 4194 0.0 0.0 112632 668 pts/0 D+ 00:19 0:00 grep --color=auto firefox
اینم یه راه دیگه :)
% pgrep firefox
1558
خب بریم سراغ کشتن پروسهها:
% kill [-SignalValue OR -SignalName] PID
برای مثال برای کشتن فایرفاکس باید از دستورات زیر استفاده کنم.
% kill -9 1558
% kill -SIGKILL 1558
البته امکان کشتن چند پروسهی همزمان هم هست.
% kill -9 1558 132 1105
در جدول زیر لیست سیگنالهای kill رو میبینید.
Number | Name | Description | Used for |
---|---|---|---|
0 | SIGNULL | Null | Check access to pid |
1 | SIGHUP | Hangup | Terminate; can be trapped |
2 | SIGINT | Interrupt | Terminate; can be trapped |
3 | SIGQUIT | Quit | Terminate with core dump; can be |
9 | SIGKILL | Kill | Forced termination; cannot be trapped |
15 | SIGTERM | Terminate | Terminate; can be trapped |
24 | SIGSTOP | Stop | Pause the process; cannot be trapped |
25 | SIGTSTP | Terminal | stop Pause the process; can be |
26 | SIGCONT | Continue | Run a stopped process |
لیست تمام سیگنالها رو با دستور زیر ببینید.
% kill -L
1 HUP 2 INT 3 QUIT 4 ILL 5 TRAP 6 ABRT 6 IOT 7 BUS 8 FPE 9 KILL 10 USR1 11 SEGV 12 USR2
13 PIPE 14 ALRM 15 TERM 16 STKFLT 17 CHLD 17 CLD 18 CONT 19 STOP 20 TSTP 21 TTIN 22 TTOU 23 URG 24 XCPU
25 XFSZ 26 VTALRM 27 PROF 28 WINCH 29 IO 29 POLL 30 PWR 31 UNUSED 31 SYS 34 RTMIN 64 RTMAX
2- Pkill
pkill کار شما رو راحت میکنه چون نیازی به Pid نداره و با نام پروسهها کار میکنه.
% pkill firefox
۳- Killall
در لینوکس هر فرایند میتونه تعدادی فرایند فرزند هم داشته باشه. برای کشتن یک فرایند به همراه تمامی فرزندهاش از دستور killall استفاده کنید.
% killall mysqld
پ.ن. توجه داشته باشید که هر کاربر فقط میتونه پروسههای مربوط به خودش رو kill کنه و امکان kill کردن پروسههای دیگر کاربران رو نداره اما کاربر root میتونه پروسههای سیستمی و پروسههای کاربران دیگه رو kill کنه.
پ.ن. اینهم لیست کامل توضیحات سیگنالها البته به انگلیسی
SIGHUP - The SIGHUP signal disconnects a process from the parent process. This an also be used to restart processes. For example, "killall -SIGUP compiz" will restart Compiz. This is useful for daemons with memory leaks.
SIGINT - This signal is the same as pressing ctrl-c. On some systems, "delete" + "break" sends the same signal to the process. The process is interrupted and stopped. However, the process can ignore this signal.
SIGQUIT - This is like SIGINT with the ability to make the process produce a core dump.
SIGILL - When a process performs a faulty, forbidden, or unknown function, the system sends the SIGILL signal to the process. This is the ILLegal SIGnal.
SIGTRAP - This signal is used for debugging purposes. When a process has performed an action or a condition is met that a debugger is waiting for, this signal will be sent to the process.
SIGABRT - This kill signal is the abort signal. Typically, a process will initiate this kill signal on itself.
SIGBUS - When a process is sent the SIGBUS signal, it is because the process caused a bus error. Commonly, these bus errors are due to a process trying to use fake physical addresses or the process has its memory alignment set incorrectly.
SIGFPE - Processes that divide by zero are killed using SIGFPE. Imagine if humans got the death penalty for such math. NOTE: The author of this article was recently drug out to the street and shot for dividing by zero.
SIGKILL - The SIGKILL signal forces the process to stop executing immediately. The program cannot ignore this signal. This process does not get to clean-up either.
SIGUSR1 - This indicates a user-defined condition. This signal can be set by the user by programming the commands in sigusr1.c. This requires the programmer to know C/C++.
SIGSEGV - When an application has a segmentation violation, this signal is sent to the process.
SIGUSR2 - This indicates a user-defined condition.
SIGPIPE - When a process tries to write to a pipe that lacks an end connected to a reader, this signal is sent to the process. A reader is a process that reads data at the end of a pipe.
SIGALRM - SIGALRM is sent when the real time or clock time timer expires.
SIGTERM - This signal requests a process to stop running. This signal can be ignored. The process is given time to gracefully shutdown. When a program gracefully shuts down, that means it is given time to save its progress and release resources. In other words, it is not forced to stop. SIGINT is very similar to SIGTERM.
SIGCHLD - When a parent process loses its child process, the parent process is sent the SIGCHLD signal. This cleans up resources used by the child process. In computers, a child process is a process started by another process know as a parent.
SIGCONT - To make processes continue executing after being paused by the SIGTSTP or SIGSTOP signal, send the SIGCONT signal to the paused process. This is the CONTinue SIGnal. This signal is beneficial to Unix job control (executing background tasks).
SIGSTOP - This signal makes the operating system pause a process's execution. The process cannot ignore the signal.
SIGTSTP - This signal is like pressing ctrl-z. This makes a request to the terminal containing the process to ask the process to stop temporarily. The process can ignore the request.
SIGTTIN - When a process attempts to read from a tty (computer terminal), the process receives this signal.
SIGTTOU - When a process attempts to write from a tty (computer terminal), the process receives this signal.
SIGURG - When a process has urgent data to be read or the data is very large, the SIGURG signal is sent to the process.
SIGXCPU - When a process uses the CPU past the allotted time, the system sends the process this signal. SIGXCPU acts like a warning; the process has time to save the progress (if possible) and close before the system kills the process with SIGKILL.
SIGXFSZ - Filesystems have a limit to how large a file can be made. When a program tries to violate this limit, the system will send that process the SIGXFSZ signal.
SIGVTALRM - SIGVTALRM is sent when CPU time used by the process elapses.
SIGPROF - SIGPROF is sent when CPU time used by the process and by the system on behalf of the process elapses.
SIGWINCH - When a process is in a terminal that changes its size, the process receives this signal.
SIGIO - Alias to SIGPOLL or at least behaves much like SIGPOLL.
SIGPWR - Power failures will cause the system to send this signal to processes (if the system is still on).
SIGSYS - Processes that give a system call an invalid parameter will receive this signal.
SIGRTMIN* - This is a set of signals that varies between systems. They are labeled SIGRTMIN+1, SIGRTMIN+2, SIGRTMIN+3, ......., and so on (usually up to 15). These are user-defined signals; they must be programmed in the Linux kernel's source code. That would require the user to know C/C++.
SIGRTMAX* - This is a set of signals that varies between systems. They are labeled SIGRTMAX-1, SIGRTMAX-2, SIGRTMAX-3, ......., and so on (usually up to 14). These are user-defined signals; they must be programmed in the Linux kernel's source code. That would require the user to know C/C++.
SIGEMT - Processes receive this signal when an emulator trap occurs.
SIGINFO - Terminals may sometimes send status requests to processes. When this happens, processes will also receive this signal.
SIGLOST - Processes trying to access locked files will get this signal.
SIGPOLL - When a process causes an asynchronous I/O event, that process is sent the SIGPOLL signal.