Linux Swiss Army knife: lsoffuserpkillpgreplsof command details: the original function of the lsof command is to list the processes of opened files. However, in LINUX, all devices exist as files, lsof is very powerful. generally, root users can execute the lsof command. for linux, the Swiss army knife: lsof/fuser/pkill/pgrep lsof command is described as follows: the original function of the lsof command is to list the processes of opened files. However, in LINUX, all devices exist in the line of files. Therefore, lsof is very powerful. generally, the root user can execute the lsof command, and the common user can see the/usr/sbin/lsof command. However, the common user's execution will display the "permission denied" sof Command usage as follows: lsof abc.txt shows the process of opening the file abc.txt lsof-I: 22 know what program is running on port 22 lsof-c abc show The file opened by the abc process lsof-g Gid: displays the processes belonging to gid. lsof + d/usr/local/displays the files opened by the process in the directory. the preceding figure shows the files lsof + D/usr/local, however, the directory will be searched. for a long time, lsof-d 4 displays the process lsof-I with fd 4 to display the syntax for conforming processes: lsof-I [46] [protocol] [@ hostname | hostaddr] [: service | port] 46-> IPv4 or IPv6protocol-> TCP or UDPhostname-> Internet host namehostaddr-> IPv4 location service-> service name in/etc/service (either one or more) port-> port number (can be more than one) example: TCP: 25-TCP and port 25@1.2.3.4-Internet IPv4 Host address 1.2.3.4tcp@www.linuxidc.com: ftp-TCP protocol hosthaha.ks.edu.tw service name: ftplsof-n do not convert IP to hostname, default is without the-n parameter example: lsof-I tcp@www.linuxidc.com: ftp-nlsof-p 12: check which files are opened by the process whose process number is 12. lsof + |-r [t] controls the repeated execution of lsof. the default value is 15 s refresh-r, lsof will always execute until it receives the interrupt signal + r, lsof will always execute until there is no file shown example: keep checking the current ftp connection status: lsof-I tcp@www.linuxidc.com: ftp-rlsof-s lists the size of the opened file. if there is no size, leave a blank lsof-u username as UID to list the usage of the opened file fuser command. Identify processes using files or file structures. Syntax: fuser [-c |-d |-f] [-k] [-u] [-x] [-V] file... The fuser command lists the process numbers of local processes. these local processes use the local or remote files specified by the File parameter. For blocking special devices, this command lists processes that use any files on the device. Each process number is followed by a letter indicating how the process uses files. C. use this file as the current directory. E. use this file as the executable object of the program. R uses this file as the root directory. S. use this file as a shared library (or another object that can be loaded. The process number is written to the standard output (there is a space between the process numbers in the row ). A linefeed is written with a standard error (after the last output of each file operand ). All other outputs are written with standard errors. This fuser command does not detect processes in the mmap region, and the related file descriptor is disabled. Flag-c contains a File system report about any opened files. -D implies that the-c and-x flags are used. Any report on opening a file that is not linked to the file system (deleted from the parent directory. When used together with the-V Mark, it also reports the node number and size of the deleted file. -F only reports on open instances of files. -K sends the SIGKILL signal to each local process. Only the root user can terminate the process of another user. -U provides the login name for the local process in parentheses after the process number. -V provides detailed output. -X is used with-c or-f to report executable and loaded objects other than standard fuser output. For example, to list the process numbers of local processes using the/etc/passwd file, enter: to list the process numbers and user logon names of processes using the/etc/filesystems file, enter: fuser-u/etc/filesystems to terminate all processes that use the given file system, enter: the fuser-k-x-u-c/dev/hd1 or fuser-kxuc/home Command lists the process numbers and user names, terminate each process that is using the/dev/hd1 (/home) file system. Only the root user can terminate the process of another user. If you are trying to unload the/dev/hd1 file system and a process accessing the/dev/hd1 file system is not allowed, you may want to use this command. To list all processes that are using files deleted from a given file system, enter the fuser-d/usr file/dev/kmem for system image. /Dev/mem is also used for system image name pgrep, pkill-lookup, or sending a signal to the process by name and other attributes. Syntax pgrep [-flvx] [-n |-o] [-d delim] [-P ppidlist] [-g pgrplist] [-s sidlist] [-u euidlist] [- U uidlist] [-G gidlist] [-J projidlist] [-t termlist] [-T taskidlist] [-c ctidlist] [-z zoneidlist] [pattern] pkill [-signal] [-fvx] [-n |-o] [-P ppidlist] [-g pgrplist] [-s sidlist] [-u euidlist] [-U uidlist] [-G gidlist] [-J projidlist] [-t termlist] [-T taskidlist] [-c ctidlist] [-z zoneidlist] [pattern] Note: The pgrep program checks the active processes in the system and reports the ID of the process whose properties match the specified conditions on the command line. Each process ID is represented by a single decimal number, separated by a split string and the next ID. the default split string is a new line. For each attribute option, you can specify a set of possible values separated by commas on the command line. For example, pgrep-G other and daemon match the process where the real group ID is other or daemon. If multiple conditions are assigned, these matching conditions are calculated according to the logical AND "AND" rules. For example, the attributes of pgrep-G other, daemon-U root, and daemon matching processes are: (the real group ID is other or daemon) and (the real user ID is root or daemon) the pkill function is the same as the pgrep function, except that the matched process is notified by a signal, it is like replacing the output of the pgrep process ID with kill (1. A signal name or number can be used as the first command line option for pkill. The following options are supported:-c ctidlist only matches The process of IDs listed in The list. -D delim specifies the string to be split between each matched process ID. If the-d option is not specified, the new line character is used by default. The-d option is only valid in the pgrep command. -F regular expression mode matches the execution with the complete process parameter string (obtained from the pr_psargs field of the/proc/nnnnn/psinfo file. If the-f option is not available, the expression only matches the execution file name (obtained from the pr_fname field of the/proc/nnnnn/psinfo file. -G pgrplist only matches the process of the process Group ID in the given list. If group 0 is included in the list, it is interpreted as the group ID of the pgrep or pkill process. -G gidlist only matches the process of the real group ID in the given list. Each group ID can be specified by the group name or number Group ID. -J projidlist matches the process of the Project ID in the given list. Each project ID can be specified by the project name or digital project ID. -L long format output. Output the name of each matching process along with the process ID. The process name is obtained from the pr_psargs or pr_fname field, depending on whether the-f option is specified. The-l option is only valid in the pgrep command. -N matches the latest (recently generated) processes that meet all other matching conditions. It cannot be used with the-o option. -O matches the oldest (earliest generated) processes that meet all other matching conditions. It cannot be used with the-n option. -P ppidlist only matches the process with the parent process ID in the given list. -S sidlist only matches the process in the specified list of process session IDs. If ID 0 is in the list, this is interpreted as the session ID of the pgrep or pikill process. -T termlist only matches the process associated with the terminal in the given list. Each terminal is specified as the suffix of the terminal device path name in/dev. For example, term/a or pts/0. -T taskidlist only matches the process of the task ID in the given list. If ID 0 is included in the list, this is interpreted as the session ID of the pgrep or pikill process. -U euidlist only matches the processes in the specified list of valid user IDs. Each user ID can be specified by a login name or a digital user ID. -U uidlist only matches the process of the real user ID in the given list. Each user ID can be specified by a login name or a digital user ID. -V reverse matching. All matching processes except matching conditions. -X only considers that the parameter string or execution file name of a process matches the specified pattern correctly. Mode is considered accurate when all characters in the process parameter string or executable file name match mode. -Z zoneidlist only matches the process of the region ID in the given list. Each region ID can be specified by a region name or a region ID of a number. This option is only valid in the global region. If the pkill program sends signals to processes in other regions, the {PRIV_PROC_ZONE} privilege must be declared in the city. -Signal indicates the signal sent to each matching process. If not specified, SIGTERM is the default signal. -Signal is only valid as the first option in the pkill command. A signal can be a symbolic name defined in signal. h without a SIG prefix, but also a corresponding signal value. The following operations are supported: pattern specifies an Extended Regular Expression (ERE) mode to match the executable file name or complete process parameter string. For a complete description of the ERE syntax, see regex (5 ). Example 1 obtain a process ID and obtain the sendmail process ID: example % pgrep-x-u root sendmail283 example 2 terminate a process to terminate the recently generated xterm: example % pkill-n xtermExit Status. the following exit value is returned: 0. one or more processes are matched. 1. no process is matched. 2. Invalid command line parameters are specified. 3. a fatal error occurs.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service