Linux system Process Management command detailed

Source: Internet
Author: User
Tags month name set time terminates pkill

The best way to manage the Linux process is to use the command line system commands. The Linux process below involves commands such as at, BG, FG, Kill, crontab, Jobs, PS, pstree, top, nice, Renice, sleep, nohup.


1. At command: Timed Run command


Function: The AT command executes the specified sequence of commands at a specified time.


Format:

At [-v] [-Q x] [-F file] [-m] timeatq [-v] [-Q X]ATRM [-v] [-Q x] job...batch [-v] [-f file] [-m]


The main options are as follows.


-V: Displays the time at which the job will be executed.


-Q: Select the queue name with the Q parameter, and the queue name can be any letter between A~z and A~z. The higher the queue's alphabetic order, the lower the queue priority level.


-F: Reads commands or shell scripts from a file instead of specifying them after prompting.


-M: Sends an e-mail message to the user after the job has finished executing.


Time: Sets when the job executes. The time parameter can be any of the following formats.


HH:MM format--such as 04:00, represents 4:00am. If the time is past, it will be executed at the next day.


Midnight represents 12:00am. Noon represents 12:00pm. Teatime represents 4:00pm.


English month name Date year format--such as January 15 2005, on behalf of January 15, 2005. Year is not available.


MMDDYY, Mm/dd/yy, or MM.DD.YY format--such as 011505, represents January 15, 2005.


Now + Time Format--time in minutes, hours, days, or weeks units. If now + 5 days, the delegate command should be executed at this time after 5 day. The offset is formatted as a time + offset, in minutes, hours, and days.


Description: The AT command is actually a set of command sets. The AT command executes the specified sequence of commands at a specified time. ATQ queries the sequence of commands that have been set. ATRM Delete the command sequence that has been set. Batch executes the SET command sequence when the system load is below a set value (0.8).


Examples of applications are as follows.


(1) Allow the system to execute the job specified in file data 17:30 after two days, using the command:

#at-f Data 15:30 +2 days


(2) Configuring batch jobs


To perform a one-time task when the average system load drops below 0.8, use the batch command. After you type the batch command, the "at>" prompt appears. Type the command you want to execute, press the ENTER key, and then type ctrl-d. You can specify more than one command by typing each command and pressing the "Enter" key. After you type all the commands, press Enter to go to a blank line, and then type "ctrl-d". Alternatively, you can enter the shell script after the prompt, press ENTER after each line of the script, and then type "ctrl-d" at the blank line to exit. If the average system load drops below 0.8, this set of commands or scripts will be executed. If this set of commands or scripts attempt to display information in standard output, the output is mailed to the user in e-mail.


(3) Advanced application: Control the use of at and batch


The/etc/at.allow and/etc/at.deny files can be used to restrict the use of the AT and batch commands. The two formats used by the control file are one user per line. Two files are not allowed to use white space characters. If the control file is modified, the at daemon does not have to be restarted. Each time a user attempts to execute an at or batch command, the control file is read. The superuser can always execute the AT and batch commands, regardless of how the control file is specified. If the At.allow file exists, only the users listed in it can use the at or batch commands, and the At.deny file is ignored. If the At.allow file does not exist, all users listed in the At.deny file are prohibited from using the AT and batch commands.


2. BG Command Background Run command


function: The BG command enables a suspended process to execute in the background.


Format: BG


The command has no parameters.


Instructions for use: Linux as a multitasking environment, users perform multiple tasks at the same time, such as viewing system conditions, backing up data, editing files, and printing files. Long-time tasks should not be performed in the foreground task, but should be handed to the background task to execute. This way the foreground task can continue to function normally other operations without waiting.


Examples of applications are as follows.


(1) If you want to sort the files of a large directory and enter the results into a file, you can use the command:

#du-a/| Sort-rn >/tmp/du.sorted


(2) Since this command executes for a long time, it can be run in the background. Use the combo key "Ctrl + Z". Then use the BG command. If you want to put this command directly in the background, you can use the "&" symbol after the command:

#du-a/| Sort-rn >/tmp/du.sorted &

[1] 237m32


3. FG Suspend Program


Function: The FG command enables a suspended process to be executed in the foreground.


Format: FG-[job-spec]


[Job-spec]: Background task number.


Description: The FG command and the BG command are relative. If you want to see the background program running, you can use the FG command to turn it back to the foreground. The BG command enables multiple processes to be executed in the background.


Examples of applications are as follows.


When using the FG command, the background task number is added, and if no number is added, the current task is changed.


#du-a/| Sort-rn >/tmp/du.sorted &

[1] 237

#fg 1


4. Jobs Show Background Program


Role: The jobs command displays the execution of background tasks.


Format: Jobs [options] [Jobspec ...]


The main options are as follows.


-L: Long output format, showing all content.


-N: does not output information.


-P: Outputs the process number only.


-R: Outputs only the running process.


[Jobspec]: Background task number.


Examples of applications are as follows.


Put two processes in the background of the system and then use the Jobs command to view the execution of the background tasks:

#du-a/etc > user.data &[1] 233# Find/-name core-type f-ls > Core.data &[2] 234#jobs-l[1] + 237 Runnin G Du-a/etc > user.data[2]-238 Running Find/-name core-type f-ls > Core.data


Description: The current task above is "du-a/etc > user.data" because the background task number is "[1]". When the first background task executes successfully and the second background task is in progress, the current task automatically becomes the background task of the background task number "[2]". That is, the current task is dynamic.


Linux Process Management Command detailed (2)

5. Kill command kills the process


Function: Kill command terminates a process.


Format: Kill [-S signal |-p] [-a]pid ...


Kill-l [Signal]


The main options are as follows.


-S: Specifies the signal to send.


-P: Analog send signal.


-L: Specifies a list of names for the signal.


PID: ID number of the process to be terminated.


Signal: Indicates a signal.


Description: Kill can send the specified information to the program. The preset information is sigterm (15), which terminates the specified program. If you still cannot terminate the program, you can use the Sigkill (9) information to try to force the removal of the program. The KILL command works by sending a system operation signal to the kernel of the Linux system and the process flag number of a program, and then the system kernel can manipulate the process specified by the process flag number. When a foreground process needs to be interrupted, the CTRL + C key combination is usually used, but for a background process, it is not a key combination that can be solved, you must use the KILL command.


Examples of applications are as follows.


If an error occurs during the execution of the command, the user can "kill" to end the task. For processes running in the background, you can use the KILL command to terminate:

#du-a/| Sort-rn >/tmp/du.sorted &[1] 237#kill 237 or use command: #du-a/| Sort-rn >/tmp/du.sorted &[1] 237#kill%1


For zombie processes, you can use kill-9 to force termination of an exit.


For example, a program has been completely dead, if kill does not add signal strength can not exit, the best way is to add the signal strength-9, the next to kill the parent process;


Like what:

[[Email protected] ~]# PS aux |grep gaimbeinan 5031 9.0 2.3 104996 17484 S 13:23 0:01 gaimroot 5036 0.0 0 .0 5160 724 pts/3 s+ 13:24 0:00 grep gaim


The KILL command family members are described below.


Terminates a process or terminates a running program, typically through kill, Killall, Pkill, Xkill, and so on. For example, a program has died, but can not quit, then you should consider the application of these tools. Killall through the name of the program, directly kill all processes, Pkill and killall the application of similar methods, but also directly kill the running program. If you want to kill a single process, kill it with kill. Xkill is a program that kills graphical interfaces on the desktop. For example, when Firefox crashes and cannot exit, Firefox can be killed by clicking the mouse button. When the Xkill runs out and the individual brain bone icon, which graphics program crashes, a little is OK. If you want to terminate Xkill, right-click Cancel.


6. crontab command


Role: The crontab command is used to install, delete, or list the task table used to drive a cron background process. The configuration is then performed by the cron daemon at a set time.


Format: crontab [u user] File


crontab [-u user] {-e |-l |-r}


The main options are as follows.


-E: Executes the text editor to set the time table, and the default text editor is VI.


-R: Deletes the current schedule table.


-L: Lists the current schedule table.


[-u user]: Specifies the user name to set.


The format of the crontab file is:


Minute hour day Month DayOfWeek command

minute--minutes, any integer from the 0~59. hour--hours, any integer from the 0~23. day--the date, any integer from the 1~31 (if the month is specified, it must be a valid date for that month). month--month, any integer from the 1~12 (or use the English abbreviations of the month such as Jan, Feb, etc.). dayofweek--week, any integer from 0~7, here 0 or 7 for Sunday (or use the English abbreviations of the week such as Sun, Mon, etc.). command--the command to execute (which can be a command such as LS, or a command that executes the script you write yourself). )


Examples of applications are as follows.


Set up a timed, periodic system prompt:


[Cao @www Cao] #crontab-E


At this point the system will open a VI editor, if I enter "* * * 5 Wall" Tomorrow rest, pay attention to backup database ", and then save the exit. At this point in the/var/spool/cron/directory will produce a Cao file, the contents are as follows:

# do not EDIT this file-edit the master and reinstall.

# (/tmp/crontab.2707 installed on Thu Jan 1 22:01:51 2004)

# (Cron version--$Id: crontab.

C,v 2.13 1994/01/17 03:20:37 Vixie Exp $)


* * 5 Wall "Tomorrow rest, pay attention to backup database"


In this way, every Friday off-hours (17:35), the system will pop up a terminal to remind us to take care of the backup database. The result is shown in 4-7.


(Click to view larger image) Figure 4-7 A timed, periodic system prompt

7. ps Command View permissions


function: The PS command primarily looks at the status of processes in the system.


Format: PS [options]


The main options are as follows.


-A: Displays information about all processes in the system.


-E: Displays information for all processes.


-F: Displays all information about the process.


-L: Displays process information in long format.


-R: Displays only the processes that are running.


-U: Displays user-facing formats (including information such as user name, CPU, and memory usage).


-X: Displays process information on all non-control terminals.


-P: Displays information for the process specified by the process ID.


-T: Displays information about processes on the specified terminal.


Note: To monitor and control a process, first understand the current process, that is, the current process needs to be viewed. The PS command is the most basic and very powerful process view command. Depending on the information displayed, you can determine which process is running, which process is suspended, how long the process has been running, what resources the process is using, the relative priority of the process, and the process's flag number (PID). All this information is useful to the user and is more important to the system administrator. Use the "ps-aux" command to get all the information about the process for all users on the terminal, as shown in Figure 4-8, which explains the basic information of the process.


Linux Process Management Command detailed (3)


Figure 4-8 in the second line of code, user represents the START process users. The PID represents the process flag number. %CPU represents the percentage of time that the process is running to occupy the CPU and the total elapsed time of the process. %mem represents the proportion of memory and total memory that the process consumes. The vsz represents the amount of virtual memory consumed, in kilobytes. RSS is the physical memory value that the process occupies, in kilobytes. TTY indicates the terminal at which the process was established, "?" Indicates that the process does not occupy the terminal. Stat represents the running state of the process, including the following code: D, non-disruptive sleep, R, ready (in a running queue), S, Sleep, T, tracked or stopped, Z, terminating (zombie) process, z not present, but temporarily unable to eliminate; W, not enough memory paging to allocate;< High-priority processes; N, low-priority processes; L, with memory paging allocated and locked in memory (real-time system or I/O). Start is the process start time. Time for the execution. command is the name of the corresponding order.


  


(Click to view larger image) Figure 4-8 Ps-aux command detailed

Examples of applications are as follows.


In the case of system maintenance, if the CPU load suddenly increases without knowing which process is causing the situation. Use the command:


  


#ps aux | Sort +3n


Description because the%CPU option is in the third column, the sort selection is 3 as a parameter, and the + is the output result in ascending order.


8. Pstree command


The pstree command lists the current processes, along with their tree structure.


Format: pstree [options] [Pid|user]


The main options are as follows.


-A: Displays the command and full parameters of the executing program.


-C: Cancel the program with the same name and merge display.


-H: Processes the output and highlights the program being executed.


-L: Long format display.


-N: Sort by pid size.


-P: Displays PID.


-U: Displays UID information.


-G: Displayed using VT100 terminal encoding.


-u: Displayed using UTF-8 (Unicode) encoding.


Note: The use of the PS command to obtain accurate data, but the data is large, which is not easy to master the overall system overview. Pstree just can make up for this shortcoming. It can display the current execution program in a tree-like structure. Pstree supports specifying a specific program (PID) or user (user) as the starting point for the display.


Examples of applications are as follows.


When the process starts, it may produce one of its own child processes. You can easily see this information by running the Pstree command. Run Pstree with Superuser privileges:


  


#init-+-apmd

|-atd

|-bdflush

|-gconfd-2

|-gdm-binary---gdm-binary-+-x

| '-startkde-+-kwrapper

| '-ssh-agent

|-gpm

|-HTTPD---8*[httpd]

...... Slightly lower


The command will automatically merge with the same name of the program, all "|-HTTPD---8*[httpd]" means the child processes produced by 8 httpd processes in the system.


9. The top command displays the process


The top command is used to display the current process status of the system.


Format: top [options]


The main options are as follows.


D: Specifies the interval of the update, in seconds.


Q: There are no delayed updates. If the user has a superuser, the top command will be executed with the highest precedence.


C: Displays the full path and name of the process.


S: Cumulative mode, which accumulates the CPU time of a child process that has completed or disappeared.


S: Safe mode.


I: Do not display any idle (idle) or useless (Zombie) processes.


N: Displays the number of updates and exits top when finished.


Note: The basic function of the top command and the PS command is the same, showing the current process status of the system. But top is a dynamic display process, that is, you can constantly refresh the current state by pressing the user key. Here is a combination of figure 4-9 to illustrate the information it gives.

(Click to view larger image) Figure 4-9 Top command detailed

In Figure 4-9, the first line represents the current time, the system startup time, the current number of system logon users, and the average load. The second line shows all the started, currently running, suspended (sleeping) and useless (Zombie) processes. The third line shows the current usage of the CPU, including the proportion of the system occupied, the user usage ratio, idle (idle) ratio. Row four shows the use of physical memory, including total available memory, used memory, free memory, and memory consumed by the buffer. The five lines show the usage of the swap partition, including the total swap partition, used, idle, and the swap partition used for caching. Line six shows the most items, and the detailed explanations are listed below.


PID (Process ID): Progress flag number, non-0 positive integer. User: The username of the process owner. PR: The priority level of the process. NI: The value of the priority level of the process. VIRT: The virtual memory value that the process occupies. RES: The physical memory value that the process occupies. SHR: Shared memory value used by the process. STAT: The state of the process, where s indicates hibernation, R is running, Z represents a zombie state, and N indicates that the process precedence value is negative. %CPU: The CPU usage that the process consumes. %MEM: The percentage of physical memory and total memory that the process occupies. Duration: The total CPU time that is consumed after the process is started. Command: The start command name of the process startup, and if this line does not appear, the process will have a full command line. While the top command is in use, you can also use some interactive commands to complete the functionality of other parameters. These commands are started by using shortcut keys.


< space;: Refresh now.


P: Sort According to CPU usage size.


T: Sort by time, cumulative time.


Q: Exit the top command.


M: Toggles display memory information.


T: Toggles display of process and CPU status information.


C: Toggle display command name and full command line.


M: Sorts according to the size of the memory used.


W: Writes the current settings to the ~/.TOPRC file. This is the recommended way to write top configuration files.


As you can see, the top command is a very powerful tool for monitoring systems and is especially important for system administrators. However, its disadvantage is that it consumes a lot of system resources.


Linux Process Management Command detailed (4)


10. The nice command changes the privilege priority


function: The nice command can change the priority level of the program execution.


Format: Nice [-n < priority >][--help][--version][Command]


The main options are as follows.


-n< priority level > or-< priority level > or--adjustment=< priority;: Sets the priority level of the command to be executed. Levels range from -20~19, where 20 is the highest and 19 is the lowest.


--help: Online Help.


Note: Application priority values range from -20~19, and the smaller the number, the higher the priority. In general, the priority value of a normal application (the CPU Usage value) is 0, and if you have a high priority level for a common program, it will start and run faster. It is important to note that the normal user can only adjust the application's priority value between 0~19, only the superuser has the right to adjust the higher priority value (from -20~19).


Examples of applications are as follows.


Add 1 to the precedence of the LS command and execute it, and input the result into the file 1. txt:


  


# nice-n 1 ls >1.txt


11. Renice command to modify the priority level


Role: The Renice command allows the user to modify the priority of a running process.


Format: Renice priority [[-P] PIDs] [[-G] pgrps] [[-u] users]


The main options are as follows.


Priority: Precedence level


-P PIDs: Change the priority level of the program, this parameter is a preset value.


-G Pgrps: Use the program group name to modify the priority of all programs that belong to the program group.


-u User: Specify the user name and modify the priority of all programs that belong to the user.


Description: The Renice command can re-adjust the priority level of the program being executed. By default, the program identification code is used to specify the program, adjust its priority, or you can specify the program group or user name to adjust the priority level, and modify all the programs that belong to the program group or user priority. Level range from -20~19, only super users can change the priority of other user programs and set negative levels. Ordinary users can only use the Renice command for all their own processes.


Examples of applications are as follows.


Process and process owners with process PID 987 and 32 have a priority number of daemon and root plus 1:


  


#renice +1 987-u Daemon root-p 32


12. Sleep command pauses the process


Function: The function of the Sleep command is to suspend the process for a period of time.


Format: Sleep Number [options]


The main options are as follows.


Number: Length of time, followed by S, M, H, or D.


S: in seconds.


M: in minutes.


H: in hours.


D: The unit is in days.


Description: If no time is specified, in seconds. This command is mostly used in shell programming to allow two commands to pause between executions for a specified time.


Examples of applications are as follows.


The following command causes the process to pause for 60 seconds before viewing which user is logged on to the system:


  


#sleep 60; W.H.O.


13. Nohup commands the user to continue working after exiting the system


Role: The Nohup command ensures that the execution program can continue to work after the user exits the system.


Format: Nohup command


Description: When exiting the Linux system, all programs will be closed, including those in the background program. But sometimes, for example, a user is downloading a large file, but you need to quit the system because you have to work or have something to do, and you want the program to continue when you exit the system. At this point, we can use the Nohup command to enable the process to continue execution after the user exits. At the same time these processes are executed in the background (commands are run in the background, nohup must be used in conjunction with the & operation), and the results will be written to the Nohup.out file in the user's own directory.


Examples of applications are as follows.


The program executes automatically in the background:


  


Nohup wget-c-t0 &


14. Pgrep command


Role: The Pgrep command finds the currently running process and lists the PID of the process that matches the given condition. All conditions must match before they are listed.


Usage rights: All users.


Format: pgrep [option] [program name]


The options are as follows.


-L: Lists the program name and process ID.


-O: The ID of the process initiation.


-N: ID of the process termination.


Examples of applications are as follows.


User Cao View the list of processes for sshd:


  


[[email protected] @cao] $pgrep-L sshd

829 sshd


Linux Process Management Command detailed (5)

15. Chkconfig command to set various services of the system


Function: Chkconfig command Check, set the system of various services.


Format: chkconfig [--add][--del][--list][system service]


or Chkconfig [--level < class Code >][system service][on/off/reset]


The main options are as follows.


--add: Adds the specified system service, allows the CHKCONFIG directive to manage it, and simultaneously adds the relevant data to the system-initiated narrative file.


--del: Deletes the specified system service, is no longer managed by the Chkconfig directive, and deletes the relevant data in the system-initiated narrative file.


--level< Class Code: Specifies which execution level the read system service is to open or close.


Description: Chkconfig provides a simple command-line tool to maintain the path hierarchy of the/ETC/RC[0-6].D, which allows the system administrator to manipulate the symbol lines directly in these paths, chkconfig execution is triggered by chkconfig commands, This command currently exists in the IRIX operating system. It even includes setting information that is maintained outside the/ETC/RC[0-6].D hierarchy. Chkconfig has 5 different functions: Add a new service to the manager, remove the service from the manager, list the currently started service information, change the service startup information, and check the startup status of the special service. This is a program developed by Red Hat in accordance with the GPL rules, which can query which system services the operating system performs at each level of execution, including a variety of resident services.


Examples of applications are as follows.


Chkconfig Management Service Skills


If you use the Chkconfig--list command, you will see a list of system services and they have been started (on) or stopped (off) in the run level 0~6. At the end of the list, you will see the Services Section managed by XINETD. If you use Chkconfig--list to query services managed by XINETD, you will see whether the XINETD service is enabled (on) or turned off (off). For example, the command chkconfig--list finger returns the following output:


Finger on


As shown above, finger is enabled as the XINETD service. If xinetd is running, finger will be enabled.


If you use Chkconfig--list to query the services in/ETC/RC.D, you will see the settings for the service at each runlevel. For example, the command chkconfig--list Anacron returns the following output:


Anacron 0:off 1:off 2:on 3:on 4:on 5:on 6:off


Chkconfig can also be used to set whether a service is started or deactivated within a specified runlevel. For example, to deactivate the NSCD service in RunLevel 3, 4, 5, use the following command:

Chkconfig--level 345 NSCD off


Additional note: Services managed by XINETD are immediately affected by chkconfig. For example, if xinetd is running and finger is disabled, finger can automatically be automatically enabled with xinetd changes after the chkconfig finger on command is executed. Changes to other services will not take effect immediately after the use of chkconfig. You must use the Service Daemon Stop command to stop individual services. In the previous example, replace the daemon with the name of the service you want to stop, such as httpd. Switch the stop to start or restart to start or restart the service.

4.4 Summary


Process management is still needed, although in desktop applications, we can do most of the work, but in server management, process management is very important. The above-described Linux process management commands are more important, and these commands allow you to observe the process. Process runs require system resources, and a process that consumes a large amount of resources can cause system performance bottlenecks. In this case, process scheduling is required to achieve real-time monitoring system and optimize performance.


The more you know about each process, the easier it is to pinpoint the problem of your process. Systems often encounter problems for some reason, such as slow or erratic speed, and using these tools can help you improve your ability to find the problem.


This article is from the "Give Me Three moles" blog, please be sure to keep this source http://wushuangjay.blog.51cto.com/3125450/1584030

Linux system Process Management command detailed

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.