Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

2.3 Common system work commands


May 23, 2021 That's what Linux should learn



The book you are reading now is summed up by Mr. Liu after more than a decade of operations study and dozens of training sessions, and you may not be able to find some commands you've seen before in this section. But don't worry, the reason for this arrangement is that Mr. Liu Wei worked hard to combine Linux commands with actual combat, really let the reader understand the technology in practice, rather than simply stacking the commands into the book to let the reader go back hard.

After spending nearly a year summarizing, summarizing, sorting and classifying the most commonly used Linux commands, Mr. Liu arranged these commonly used commands into subsequent chapters, and then adopted a method of practicing ad lithology to deepen the reader's understanding and mastery. This approach is quite effective based on feedback from years of training results, so I believe it is certainly suitable for your learning.

1. Echo command

The echo command is used to output a string or variable extracted at the terminal in the format "echo (string| $variable)."

For example, the command that outputs the specified Linuxprobe.com "10"" to the terminal screen is:

[root@linuxprobe ~]# echo Linuxprobe.Com

The command displays the following information on the terminal screen:

Linuxprobe.Com

Below, we use the $variable to extract the value of the variable SHELL and output it to the screen:

[root@linuxprobe ~]# echo $SHELL /bin/bash

2. Date command

The date command is used to display and set the time or date of the system in the format of "date ."

Simply enter parameters that begin with the "plus" sign in the powerful date command to output the system's time or date in the specified format, so that the commands that back up the data can be combined with the time information output in the specified format in your daily work. F or example, packaged files are automatically packaged as "backup-2017-9-1.tar.gz" in the year-month-day format, and users only need to look at the file name to get an idea of when each file will be backed up. The format and function of the parameters commonly found in the date command are shown in Table 2-4.

The parameters and effects in the Table 2-4 ate command

Parameter Act %t Jumper (Tab Key) %H hours (00-23) %I Hours (00 to 12) %M minutes (00 to 59) %S seconds (00 to 59) %j for the first few days of the year

The date command for viewing the current system time in the default format looks like this:

[root@linuxprobe ~]# date Mon Aug 24 16:11:23 CST 2017

The date command for viewing the current system time in the format "Year-month-day hours:minutes:seconds" looks like this:

[root@linuxprobe ~]# date "+%Y-%m-%d %H:%M:%S" 2017-08-24 16:29:12

The date command to set the system's current time to 8:30 on September 1, 2017 looks like this:

[root@linuxprobe ~]# date -s "20170901 8:30:00" Fri Sep 1 08:30:00 CST 2017

Use the date command again and see the current system time in the default format, as follows:

[root@linuxprobe ~]# date Fri Sep 1 08:30:01 CST 2017

The parameter %j in the date command can be used to see that today is the first day of the year. T his parameter is a good distinction between old and new backup times, i.e. the larger the number, the closer it is to the current time. The use of this parameter and the results are shown below.

[root@linuxprobe ~]# date "+%j" 244

3. Reboot command

The reboot command is used to restart the system in the form of reboot.

Because restarting a computer involves administrative permissions for hardware resources, you can only restart with root administrators by default, with the following commands:

[root@linuxprobe ~]# reboot

4. Poweroff command

The poweroff command is used to shut down the system in the form of poweroff.

This command, like the reboot command, involves administrative rights for hardware resources, so by default only the root administrator can shut down the computer, as follows:

[root@linuxprobe ~]# poweroff

5. wget command

The wget command is used to download network files in the terminal in the format "wget (parameters) download address".

If you don't have management experience with Linux systems, just look at the parameters of the wget command and what it does, and then take a look at the demo experiment below, remember not to rush to success. T he following sections will gradually explain the linux system configuration management methods, you can wait for you to master the network card configuration methods before you carry out this experimental operation. Table 2-5 shows the parameters of the wget command and the role of the parameters.

Table 2-5 the parameters of the wget command and its effect

Parameter Action -b Background Download Mode -P Download to specified directory -t Maximum number of attempts -c intermittently -p Download all resources within the page, including pictures, videos, etc. -r Recursive Downloads

Try using the wget command to download the book's latest pdf-format electronic document from the book's companion site, with a full path of https://www.linuxprobe.com/docs/LinuxProbe.pdf, and the download after executing the command works as follows:

[root@linuxprobe ~]# wget https://www.linuxprobe.com/docs/LinuxProbe.pdf --2017-08-24 19:30:12 -- https://www.linuxprobe.com/docs/LinuxProbe.pdf Resolving www.linuxprobe.com (www.linuxprobe.com)... 2 20.181.105.185 Connecting to www.linuxprobe.com (www.linuxprobe.com)|220.181.105.185|:80...connected. H TTP request sent, awaiting response... 200 OK Length: 45948568 (44M) [application/pdf] Saving to: ‘LinuxProbe.pdf’ 100%[===========================================>] 45,948,568 32.9MB/s in 1.3s 2017-08-24 19:30:14 (32.9 MB/s) - ‘LinuxProbe.pdf’ saved [45948568/45948568]

Next, we use the wget command to recursively download all page data and files within the www.linuxprobe.com site, which will automatically be saved to the next directory named www.linuxprobe.com the current path. The command to do this is wget -r -p https://www.linuxprobe.com, and the result of the execution is as follows.

[root@linuxprobe ~]# wget -r -p https://www.linuxprobe.com --2017-08-24 19:31:41-- https://www.linuxprobe.com/ Resolving www.linuxprobe.com... 1 06.185.25.197 Connecting to www.linuxprobe.com|106.185.25.197|:80... c onnected. H TTP request sent, awaiting response... 2 00 OK Length: unspecified [text/html] Saving to: 'www.linuxprobe.com/index.html' .................. Omit the download process...

6. ps command

The ps command is used to view the process status in the system in the format "ps (parameters).

It is estimated that the reader will be stunned the first time this command is executed - how can there be so many output values? I n fact, Mr. Liu usually uses the ps command with chapter 3's pipe character technology to grab the PID number that corresponds to a specified service process. Common parameters of the ps command and its effects are shown in Table 2-6.

The parameters and effects of the Table 2-6 ps command

The role of the argument

-a shows all processes (including those of other users) -u users and other details -x shows processes that do not control the terminal

There are many processes running through the Linux system at all times, and if you can manage them properly, you can optimize the performance of your system. In Linux systems, there are five common process states, namely, run, interrupt, uninterrupted, deadlock, and stop, each with the following meanings.

R (running): The process is running or waiting in the running queue.

S (interruption): The process is dormant and leaves the state when a condition is formed or when a signal is received.

D (non-disruptive): The process does not respond to system asynchronous signals and cannot be interrupted even with the kill command.

Z (dead): The process has been terminated, but the process descriptor remains until the parent process calls the wait4() system function and releases the process.

T (stop): The process stops running when it receives a stop signal.

When the ps aux command is executed, you typically see the process state shown in Table 2-7, which lists only some of the output values, and the normal output values do not include Chinese comments.

Table 2-7 process status

USER PID %CPU %MEM VSZ RSS TTYAT START TIME COMMAND Process Owner Process ID Number Operator Occupancy Memory Occupancy Virtual Memory Usage (in KB) The amount of fixed memory (in KB) at the end of the process state where the CPU's time command name and parameter root 1 0.0 0.4 53684 7628? S s 07:22 0:02 /usr/lib/systemd/systemd root 2 0.0 0.0 0 0 ? S 07:22 0:00 [kthreadd] root 3 0.0 0.0 0 0 ? S 07:22 0:00 [ksoftirqd/0] root 5 0.0 0.0 0 0 ? S < 07:22 0:00 [kworker/0:0H] root 7 0.0 0.0 0 0 ? S 07:22 0:00 [migration/0] .................. Omitting part of the output information...

As mentioned earlier, command parameters in Linux systems are divided into long and short formats, which cannot be merged between long and short formats, but can be merged between short and short formats, and only one - (minus sign) can be retained after the merge. In addition, the ps command allows parameters to be written directly as ps aux without subtracting the sign (-).

7. Top command

The top command is used to dynamically monitor information such as process activity and system load in the format top.

The top command is powerful enough to dynamically view system operations and operations, treating it entirely as an "enhanced version of Windows Task Manager" in Linux. The top command runs as shown in Figure 2-6.

2.3 Common system work commands

The running interface of the Figure 2-6 top command

In Figure 2-6, the top command executes statistics for the first 5 behavior systems as a whole, which represents the following meaning.

Line 1: System time, running time, number of login terminals, system load (three values are 1 minute, 5 minutes, 15 minutes average, the smaller the value means the lower the load).

Line 2: Total number of processes, number of processes running, number of processes in sleep, number of processes stopped, number of deadlocked processes.

Line 3: Percentage of user-occupied resources, percentage of resources occupied by the system kernel, percentage of process resources that changed priority, percentage of idle resources, and so on. Where the data is CPU data and displayed as a percentage, for example , "97.1 id" means that 97.1% of CPU processor resources are idle.

Line 4: Total physical memory, memory usage, free memory, amount of memory cached as the kernel.

Line 5: Total virtual memory, virtual memory usage, virtual memory idle, amount of memory that has been loaded ahead of time.

8. Pidof command

The pidof command is used to query the PID value of a specified service process in the format "pidof ( parameters ) .

The process number value (PID) for each process is unique, so piDs can be used to distinguish between different processes. For example, you can use the following commands to query the PID of the sshd service program on this machine:

[root@linuxprobe ~]# pidof sshd 2156

9. Kill command

The kill command is used to terminate a service process that specifies a PID in the format of "kill (parameters) and "process PID".

Next, we use the kill command to terminate the process represented by the PID that was queried with the pidof command above, as shown below. The effect of this operation is equivalent to forcing the sshd service to stop.

[root@linuxprobe ~]# kill 2156

10. Killall command

The killall command is used to terminate all processes corresponding to a service with a specified name in the form of: "killall (parameters) (service name).

In general, service programs for complex software have multiple processes working together to serve users, and if it can be cumbersome to end these processes one by one, you can use the Killall command to bulk end all the processes that a service program has. L et's take the httpd service program as an example to end all of its processes. Since the RHEL7 system does not have httpd service program installed by default, we only need to look at the operation process and output results at this time, and so on after learning the relevant content to practice.

[root@linuxprobe ~]# pidof httpd 13581 13580 13579 13578 13577 13576 [root@linuxprobe ~]# killall httpd [root@linuxprobe ~]# pidof httpd [root@linuxprobe ~]#

If we want to stop a command immediately after executing it in the system terminal, we can press the Ctrl-C key combination (a shortcut that is more commonly used in production) at the same time, which will immediately terminate the process of the command. Alternatively, if some commands are executed by constantly outputing information on the screen, affecting the input of subsequent commands, you can add a symbol at the end when the command is executed, so that the command goes into the system background to execute.

Many students in the registration of Mr. Liu Wei training courses will mention that their English foundation is not good, in fact, you need not worry, because our books, training courses and even Red Hat questions are Chinese. A nd then you'll find that linux commands have always been used, not pure English words, and even if they're spelled 100% the same, they'll certainly end up being of different use. So in the study of Linux system technology this matter, you and English people are absolutely standing on the same running line, not to mention the right choice of a suitable linux teaching material for you, rest assured to start learning it!

Mr. Liu Wei's introduction to the Linux technical training course: Https://www.linuxprobe.com/training