Summary of Common Linux Commands & Utility Commands
December 22, 2023
by Mr. Denis Woodward
No Comments
❝
Linux is the front end will be skills, the front is a lot of children’s shoes to walk again big front line, the back-end to mention the, small make up to you set the tens of thousands of words (due to space constraints in this paper, has cut) about the practical basis of Linux command, may say that these are must master, of course, these aren’t enough, the small make up will continue to update, You can pay attention to my dynamic, disk, this wave can be bookmarked.
❞
ls
One of the most frequently used commands.
Command format: ls [OPTION]… [FILE]… Pure input:
[root@iz2ze76ybn73dvwmdij06zz /]# lsbin dev home lib64 media opt proc run srv tmp var
boot etc lib lost+found mnt patch root sbin sys usr www
Copy the code
Add option-l which shows permission, owner, owner group, size, time, file name
Ls -a (all) Linux ‘.’ (dot) files are hidden by default. -A can query all files.
[root@iz2ze76ybn73dvwmdij06zz /]# ls -a. bin etc lib64 mnt .pearrc run sys usr
. boot home lost+found opt proc sbin .test var
.autorelabel dev lib media patch root srv tmp www
Copy the code
Display lS-R backwards, the default order is (a, B, C, D) in this order
[root@iz2ze76ybn73dvwmdij06zz /]# ls -rwww usr sys sbin root patch mnt lost+found lib etc boot
var tmp srv run proc opt media lib64 home dev bin
Copy the code
Ls -r () recursively displays subdirectories
[root@iz2ze76ybn73dvwmdij06zz test]# ls -R. :data
./data:
img ./data/img: car product user ./data/img/car: ./data/img/product: ./data/img/user: Copy the code
Ls-lt Creates files A and B in reverse order by modification time
[root@iz2ze76ybn73dvwmdij06zz test]# ls -ltThe total dosage of 0-rw-r--r-- 1 root root 0 9月 15 22:18b-rw-r--r-- 1 root root 0 9月 15 22:18aCopy the code
ls -version
[root@iz2ze76ybn73dvwmdij06zz test]# ls --versionLs (GNU coreutils) 8.22Copyright (C) 2013 Free Software Foundation, Inc.
License: GPL + : the GNU general public license version 3 or updated version < http://gnu.org/licenses/gpl.html >.This software is free software: you are free to modify and redistribute it.There are no other warranties within the law.Written by Richard M. Stallman and David MacKenzie.Copy the code
cat
Cat, again, a relatively high frequency command. More officially, commands are used to link files and print them to standard output devices. This text output command can be used to view file contents, create content, etc. Here are some useful uses. Syntax: cat [options]… [file]…
Cat -n views and displays the line number
[root@iz2ze76ybn73dvwmdij06zz linuxdemo]# cat -n a 1 this is a txt
2 hello every body
3 hello world
4 hahaha
Copy the code
Cat creates an empty file b
[root@iz2ze76ybn73dvwmdij06zz linuxdemo]# cat >b <<EOF> EOF
[root@iz2ze76ybn73dvwmdij06zz linuxdemo]# lsa b
Copy the code
Cat writes the content (if there was any content, it will be overwritten)
[root@iz2ze76ybn73dvwmdij06zz linuxdemo]# cat >a<<EOF> today is a good day
> because off work
> hahaha
> EOF
[root@iz2ze76ybn73dvwmdij06zz linuxdemo]# cat atoday is a good day because off work hahaha Copy the code
Cat displays multiple file contents at the same time
[root@iz2ze76ybn73dvwmdij06zz linuxdemo]# cat a > b[root@iz2ze76ybn73dvwmdij06zz linuxdemo]# cat btoday is a good day
because off work
hahaha
Copy the code
Append content can be seen with a > after the content is appended
[root@iz2ze76ybn73dvwmdij06zz linuxdemo]# cat a >> b[root@iz2ze76ybn73dvwmdij06zz linuxdemo]# cat btoday is a good day
because off work
hahaha
today is a good day because off work hahaha Copy the code
Cat –help (Try it yourself if you want to learn more)
[root@iz2ze76ybn73dvwmdij06zz linuxdemo]# cat --helpCat [option]... [file]...Output [file] or standard input combination to standard output.-A, --show-all = -vet-b, --number-nonblank Specifies the number of the non-blank output lineE equals - vE-e, --show-ends is displayed at the end of each line"$"-n, --number Specifies the number of all lines in the output-s, --squeeze-blank does not output multiple blank lines-t is equivalent to vT-t, --show-tabs displays the TAB character as ^I-u (ignored)-v, --show-nonprinting uses ^ and M- references, except for LFD and TAB --helpDisplay this help information and exit--version Displays the version information and exitsIf no file is specified, or the file is"-"Is read from standard input.Example:Cat f -g Outputs the contents of f, standard input, and g.Cat copies the contents of standard input to standard output.GNU coreutils online help: <http://www.gnu.org/software/coreutils/> Please report to < http://translationproject.org/team/zh_CN.html > cat translation errorsFor complete documentation, run: info coreutils'cat invocation'Copy the code
User permissions
Everyone knows that Linux is a multitasking, multi-user operating system. Multiple users can perform different tasks in the operating system at the same time, and one user can also log in multiple people. Generally speaking, root has the most permissions. The database we often use is usually a mysql user, and we may use a WWW account for web pages. We all know the story of deleting runaways, so some operation permissions should be managed by some special personnel. In order to study systematically, I think it is necessary to understand its basic knowledge.
Linux Role Classification
Super user :root (user ID –UID) 0 Has the highest management rights.
Common users:
System user UID:1-999(RHEL7 centos7) 1-499(RHEL6)Local user UID:1000+(rHEL7 centos7) 500+(rHEL6)Copy the code
UID: unique identifier of a user.
Linux group classification
Group:
Group classification:
According to the functions of accounts:Super user group: root GID:0Common User Group:System user group: GID 1-- 999Local user group: GID 1000+Copy the code
Relationships between Linux users and groups
A user can belong to one group or multiple groups. Multiple users can belong to one group or multiple groups.
Configuration files for Linux users and groups
1./etc/passwd: indicates the user and attribute information.
2./etc/group: indicates the group and attribute information.
/etc/shadow: indicates the user password and attribute information.
/etc/gshadow: indicates the group password and attribute information.
5./etc/default/useradd: add the user rule file.
6./etc/login.defs: Set the limit of user accounts.
Linux user information file
One of the most important user management files, this file is open to all users, each line holds a user’s information, each attribute is separated by a colon.
cat /etc/passwd
root:x:0:0:root:/root:/bin/zsh
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin Copy the code
Example: the root: x: 0-0: root: / root: / bin/ZSH
1. Login user name :root
2. The user password placeholder is X
3. User UID: 0
4. User group GID:0
5. User account description :root
6. User home directory :/root
7. User default shell:/bin/zsh (default: /bin/bash)
Linux password information file
Only user root has the read permission for this file. Each line stores a record of the user, and each attribute is separated by a colon.
cat /etc/shadow
root:$6$LxE7qiaZ$ZZXf/aCO1Zqs1oOS9WDutUk9rFOq4MOoSG9IeXq2f5sduGrgfYvzOWHZmJZFGC.0Of1DjCna0M.oQZTY8r00/.:17769:0:99999:7:::
bin:*:17110:0:99999:7:::
daemon:*:17110:0:99999:7:::
Copy the code
Example: bin:*:17110:0:99999:7:: 1. Login user name: bin
2. Encrypted password: * indicates that the password is locked
3. Date of last password change: 17110 indicates the number of days between 1970-1-1 when the password cannot be changed
4. Password change period: 99999 indicates that you never need to change the password. If it is any other number, it indicates the number of days from 1970-1-1 that the password must be changed.
5. Maximum password validity days: specifies the maximum password validity days.
6. Password expiration warning: the number of days before the password officially expires (-1, never prompted)
7. How many days after the password expires Disable user: can log in, single cannot operate.
8. User disabled date: how many days after the account expires, can not log in.
9. Reserve parameters
Linux group information file
The group information for the user group is stored here, with a group record in one row and properties separated by colons.
I believe that most people should be like me, the beginning of the use of Windows, it is familiar with the Windows platform installation package, such as.exe, is not too common, generally installed in Windows mysql, I believe that.msi is also seen. Most servers run Linux, so let’s take a look at the basics of the Linux installation package. Different distributions of systems use different tools. At present, most company servers are based on centos, so centos will be used as an example in the future.
Based on the redhat
yum
zypper
rpm
Based on centos
yum
rpm
rpm
Full name: Redhat Package Manager
Download it from http://rpmfind.net
The following uses RPM -5.4.10-10.i586. RPM as an example.
RPM: indicates the software package name
5: indicates the major version number
4: indicates the second version
10: Revision number
I586: CPU architecture platform
RPM: indicates the package suffix
yum
Solve the dependency problem of software package installation, automatically download software package, based on THE C/S architecture. The repo file is a configuration file for the YUM source that defines the repository information. Why is it a C/S architecture? The header of the original RPM software will record the dependency information of the modified software. Therefore, it is possible to analyze the content of the head to know its dependencies and record these information. Then the server is to provide these RPM packages, and then analyze the dependency of the package, the record will be stored in a directory on the server. The client downloads its record dependency files during software installation, analyzes them, and finally downloads them once.
Process Management Basics
Generally speaking, programs are divided into two categories, one is system program, one is application program. A running program is a process. A process occupies memory, and when you kill it, resources are released.
Type of process
User process: the user’s own program, which the user can control to turn on and off.
Interactive process: A process that interacts with users.
Batch process: a collection of processes that start other processes in sequence.
Daemon: a process that is always running. Crond.
Process properties
Process ID: (PID) Unique numeric identifier to distinguish different processes.
Processes have parents and children.
User ID(UID) and owner group of the user that started the process.
There are three states of a process:
Run – R
Dormancy, S
Zombies – Z
Priority of a process: The value ranges from -20 to 19. A smaller value indicates a higher priority. The default value is 0.
The terminal to which the process is linked.
Resources occupied by processes.
The relationship between parent and child processes
If the parent terminates first, the child is adopted by the init process. If the child terminates first and the parent does not call wait or waitPID, the process processes the dead state. If you want to avoid this situation, you need to call wait or waitPID.
Process management tool
Ps: View processes.Top: Displays dynamic process information.kill: Kill process.Pstree: View the process tree.Pgrep: indicates a search process.Lsof: displays the open files of the process.Copy the code
Process Management practices
ps
Statically displays information about the current process.
Command: ps
Syntax: ps parameter
Common parameters:
A Displays all user processes. R Displays running processes. L Long format output U Displays processes by user name and startup timeCopy the code
Action: BSD format to display the process
➜ ~ ps aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
Root 1 0.0 0.0 43364 3596? Ss November 03 0:01 /usr/lib/systRoot 2 0.0 0.0 0 0? S November 03 0:00 [kthreadd]Root 3 0.0 0.0 0 0? S November 03 0:00 [ksoftirqd/0]Root 5 0.0 0.0 0 0? S< November 03 0:00 [kworker/ 0:0hRoot 7 0.0 0.0 0 0? S November 03 0:00 [Migration /0]Root 8 0.0 0.0 0 0? S November 03 0:00 [rcu_bh]Root 9 0.0 0.0 0 0? S November 03 0:05 [rcu_sched]Root 10 0.0 0.0 0 0? S November 03 0:00 [watchdog/0]Root 11 0.0 0.0 0 0? S November 03 0:00 [watchdog/1]Root 12 0.0 0.0 0 0? S November 03 0:00 [Migration /1]Root 13 0.0 0.0 0 0? S November 03 0:00 [ksoftirqd/1]Root 15 0.0 0.0 0 0? S< November 03 0:00 [kworker/ 1:0hCopy the code
Standard format displays processes unix-style
~ ps -ef
UID PID PPID C STIME TTY TIME CMD
Root 1 0 0 November 03? 00:00:01 /usr/lib/systemd/systemd --systeRoot 2 0 0 November 03? 00:00:00 [kthreadd]Root 3 2 0 November 03? 00:00:00 [ksoftirqd/0]Root 5 2 0 November 03? 00:00:00 [kworker/0:0H]Root 7 2 0 11月03? 00:00:00 [migration/0]Root 8 2 0 November 03? 00:00:00 [rcu_bh]Root 9 2 0 November 03? 00:00:05 [rcu_sched]Root 10 2 0 11月03? 00:00:00 [watchdog/0]Root 11 2 0 November 03? 00:00:00 [watchdog/1]Root 12 2 0 November 03? 00:00:00 [migration/1]Root 13 2 0 November 03? 00:00:00 [ksoftirqd/1]Root 15 2 0 November 03? 00:00:00 [kworker/1:0H]Root 17 2 0 November 03? 00:00:00 [kdevtmpfs]Root 18 2 0 November 03? 00:00:00 [netns]Root 19 2 0 November 03? 00:00:00 [khungtaskd]Root 20 20 November 03? 00:00:00 [writeback]Root 21 2 0 November 03? 00:00:00 [kintegrityd]Copy the code
Attribute details:
USER: indicates the owner of a process
PID: indicates the ID of a process
%CPU: indicates the percentage of processes in the CPU
%MEM: percentage of memory occupied by processes
VSZ: indicates the size of virtual memory occupied by a process
RSS: fixed memory usage
STAT Process Status
R is running, can pass in queue;
S is in the dormant state; T stop or be tracked; Z Zombie process; N Lower priority process L Some pages are locked into memory; The leader of the S process (there are child processes below it)Copy the code
Copy the code
START Time when the process is started.
TIME Indicates the CPU consumption TIME.
COMMAND Indicates the name and parameters of a COMMAND.
Sort by specified attribute:
Sort by CPU from smallest to largest.
➜ ~ ps aux --sort % CPUUSER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
Root 1 0.0 0.0 43364 3596? Ss November 03 0:01 /usr/lib/systRoot 2 0.0 0.0 0 0? S November 03 0:00 [kthreadd]Root 3 0.0 0.0 0 0? S November 03 0:00 [ksoftirqd/0]Root 5 0.0 0.0 0 0? S< November 03 0:00 [kworker/ 0:0hRoot 7 0.0 0.0 0 0? S November 03 0:00 [Migration /0]Root 8 0.0 0.0 0 0? S November 03 0:00 [rcu_bh]Root 9 0.0 0.0 0 0? S November 03 0:05 [rcu_sched]Root 10 0.0 0.0 0 0? S November 03 0:00 [watchdog/0]Root 11 0.0 0.0 0 0? S November 03 0:00 [watchdog/1]Copy the code
Sort by CPU from largest to smallest.
➜ ~ ps aux --sort -% CPUUSER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
Root 21342 0.3 0.4 137184 17112? Ssl 03:24 1:16 /usr/local/aegi
Root 1 0.0 0.0 43364 3596? Ss November 03 0:01 /usr/lib/systRoot 2 0.0 0.0 0 0? S November 03 0:00 [kthreadd]Root 3 0.0 0.0 0 0? S November 03 0:00 [ksoftirqd/0]Root 5 0.0 0.0 0 0? S< November 03 0:00 [kworker/ 0:0hRoot 7 0.0 0.0 0 0? S November 03 0:00 [Migration /0]Root 8 0.0 0.0 0 0? S November 03 0:00 [rcu_bh]Root 9 0.0 0.0 0 0? S November 03 0:05 [rcu_sched]Root 10 0.0 0.0 0 0? S November 03 0:00 [watchdog/0]Root 11 0.0 0.0 0 0? S November 03 0:00 [watchdog/1]Root 12 0.0 0.0 0 0? S November 03 0:00 [Migration /1]Root 13 0.0 0.0 0 0? S November 03 0:00 [ksoftirqd/1]Copy the code
top
In contrast to ps, the top command displays dynamic information about a process.
Command: top
Syntax: the top parameter
Common parameters:
Operation:
➜ ~ top
Top-11:38:42 UP 13:15, 2 Users, Load Average: 0.01, 0.03, 0.05Tasks: 98 total, 1 running, 97 sleeping, 0 stopped, 0 zombie
%Cpu(s): 0.2us, 0.0sy, 0.0Ni, 99.8ID, 0.0wa, 0.0hi, 0.0Si, 0.0stKiB Mem : 3881688 total, 229264 free, 543932 used, 3108492 buff/cache
KiB Swap: 1049596 total, 1049596 free, 0 used. 3026944 avail Mem PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 1 root 20 0 43364 3596 2340 S 0.0 0.1 0:01.79 systemd 2 root 20 00 0 S 0.0 0.0 00.00 kthreadd3 root 20 00 0 S 0.0 0.0 0:00.14 ksoftirqd/05 root 0-20 00 0 S 0.0 0.0 00.00 kworker/0:+7 Root rt 00 0 S 0.0 0.0 0:00.33 Migration /08 root 20 00 0 S 0.0 0.0 00.00 rcu_bh9 root 20 0 0 0 S 0.0 0.0 0:05.73 rcu_schedCopy the code
Attribute Explanation:
The first five lines are collated statistics of the data.
The first line:
11:38:42 Current timeUp 13:15 System running time. The format is minute2 user Indicates the number of current login usersLoad Average: 0.01, 0.03, 0.05 Average value of system load and average task queue length 1 minute,5 minutes, and 15 minutes ago.Copy the code
Line 2 and 3:
98 Total Total number of processes1 Running Number of running processes97 Number of sleeping processes0 Stopped Number of processes that have been stopped0 Zombie Number of zombie processes %Cpu(s): 0.2 us CPU usage of system user processes0.0sy Percentage of CPU usage by kernel processes0.0 ni Percentage of CPU usage in user process space by processes whose priorities have changed99.8 ID Percentage of idle CPUS0.0 wa Percentage of CPU time spent waiting for input and output0.0 HI Hardware CPU interrupt usage percentage0.0 Si Percentage of soft interrupt usage0.0 ST Percentage of VM usageCopy the code
Line 4 and 5:
Mem: (unit K)
3881688 Total Total physical memory229264 Free Total free memory543932 Used Total used physical memory3108492 Buff /cache The amount of memory in the kernel cacheCopy the code
Swap: (unit K)
1049596 Total Total number of switch areas1049596 Free Total number of free switch areas0 Used Indicates the total number of used switch areas3026944 Avail Mem Amount of memory availableCopy the code
Title:
PID: process id
USER: indicates the USER name of the process owner
PR: priority
NI: Process priority, nice value, negative -> high priority, positive -> low priority
VIRT: total virtual memory VIRT =swap + res
RES: actual memory size
SHR: indicates the size of shared memory
S: indicates the process status
D: uninterruptible sleep state R: running S: sleeping t: tracking z: zombie processCopy the code
%CPU: indicates the percentage of CPU usage since the last update
%MEM: percentage of physical memory used by a process
TIME+: indicates the total CPU usage of a process, expressed in 1/100 of a second
Each label above is the directory name named after the PID of our current process.
free
View the used and free memory of the system.
Command: free
Syntax: Free parameter
Common parameters:
-b, --bytes displays -k, --kilo k displays -m, --mega m displays -g, --giga g displaysCopy the code
Operation:
➜ ~ free - m total used free shared buff/cache available
Mem: 3790 536 210 2 3043 2950
Swap: 1024 0 1024
Copy the code
The unit is G.
➜ ~ free - h total used free shared buff/cache available
Mem: 3.7g 538M 207M 2.4m 3.0G 2.9gSwap: 1.0 G 1.0 G 0 bCopy the code
File search
When doing operations related things, we need to look up files like xxx.conf, logs, and the location of an executable.
An overview of
which: View the location of the execution file.Whereis: Views executable file locations and related files.Locate: Works with the database cache to quickly view the location of a file.Grep: filter matches, it is a file search tool.Find: You can view files based on conditions.Copy the code
which
Command: which?
Syntax: the which parameter queries the target
Common commands: Generally, this command is used without parameters
--all, -a Displays all matched pathsCopy the code
Operation:
➜ ~ whichpwd/usr/bin/pwdCopy the code
whereis
Command: whereis
Syntax: the whereis parameter queries the target
Common commands:
Generally, without parameters, -b searches only binary files. -b < directory > Defines the binary file search path. -m Searches only man manualCopy the code
Operation:
➜ ~ whereis ls
ls: /usr/bin/ls /usr/share/man/man1/ls.1.gz
Copy the code
locate
Command: locate
Syntax: Locate file name
Common parameters: None
Actions: The locate and find commands do the same thing, but are more efficient because locate looks up a database and find looks up directory files.
Database:
➜ ~ the ls/var/lib/mlocate/mlocate db/var/lib/mlocate/mlocate.db
Copy the code
Related configuration files:
➜ ~ the ls/etc/updatedb. Conf/etc/updatedb.conf
Copy the code
Related scheduled tasks:
➜ ~ the ls/etc/cron. Daily/mlocate/etc/cron.daily/mlocate
Copy the code
For example:
➜ ~ touch shafa
➜ to locate shafa➜ ~ updatedb
➜ to locate shafa/root/shafa
Copy the code
Note: If we created the file that day to find, need to manually updatedB.
grep
Command: grep
Syntax: grep parameter target value file
Common parameters:
Take anti - v-i ignores case-n Prints the line number with the output* * begins with **$ends with *^ $a blank lineCopy the code
Operation:
Check the /etc/passwd line that contains root
➜ ~ grep root /etc/passwd
root:x:0:0:root:/root:/bin/zsh
operator:x:11:0:operator:/root:/sbin/nologin
Copy the code
Run the following command to query /etc/passwd without root:
Check /etc/passwd for the WWW line and display the line number
➜ ~ grep -n www /etc/passwd
23:www:x:1000:1000::/home/www:/sbin/nologin
Copy the code
find
Command: the find
Syntax: find path parameter output
Common parameters:
Path: For example,. Indicates the current directory, and/indicates the system root directory. -print: "\n" as delimiter, newline -print0: with xargs, "\0" as delimiterCopy the code
Find Common command options:
-name Searches for files by name. Name -perm finds files by file permissions. 666 777 Isodepth When searching for a file, first search the file in the current directory. -user Searches for files by file owner -atime-ctime (day) -mmin-cmin-amin (minute) -size n [c] Searches for files with n blocks in length. -follow If the find command encounters a symlink file, it follows it to the file the link points to.Copy the code
Operation:
View files starting with test. In the current directory
➜ ~ find.-name"test*"./test.txt
./autojump/tests
./test.1
Copy the code
View files in the current directory starting with test.
➜ ~ find.-name"test*" -print./test.txt
./autojump/tests
./test.1
Copy the code
View files starting with test. In the current directory. The output result is not displayed on a new line
➜ ~ find.-name"test*" -print0
./test.txt./autojump/tests./test.1
Copy the code
Execute the corresponding command on what you are looking for
The default action is -print
Common actions:
-exec can be followed by a custom shell commandCopy the code
Operation:
Query. TXT files with ls for more properties
➜ test find . -name "*.txt" -exec ls -l {} \;
-rw-r--r-- 1 root root 0 11月 4 14:56./xq1.txt-rw-r--r-- 1 root root 0 November 4 14:56./xq.txt-rw-r--r-- 1 root root 0 11月 4 14:56./xq2.txtCopy the code
Query.txt files and change them to.bak files
➜ test find . -name "*.txt" -exec mv {} {}.bak \;
➜ test ls
xq1.txt.bak xq2.txt.bak xq.txt.bak
Copy the code
Logical query:
- a and-o or+ is higher than- belowCopy the code
Action: View files ending in. Sh or. Q.
➜ test find . -name "*.sh" -o -name "*.q"./book.q
./stop.sh
./start.sh
Copy the code
View files that end in. Sh and begin with s.
➜ test find . -name "*.sh" -a -name "s*"./stop.sh
./start.sh
Copy the code
View after inserting content into the file.
➜ test ll
Total amount of 12 k-rw-r--r-- 1 root root 9 11月 4 15:10 book.q
-rw-r--r-- 1 root root 13 November 4 15:10 start.sh-rw-r--r-- 1 root root 4 November 4 15:10 stop.sh-rw-r--r-- 1 root root 0 11月 4 14:56xq1.txt.bak-rw-r--r-- 1 root root 0 November 4 14:56xq2.txt.bak-rw-r--r-- 1 root root 0 November 4 14:56xq.txt.bakCopy the code
View files whose value is greater than 40K and less than 50K in /etc
➜ test find /etc/ -size +40k -a -size -50k
/etc/selinux/targeted/active/modules/100/sysadm/hll
/etc/selinux/targeted/contexts/files/file_contexts.homedirs.bin
Copy the code
Schedule a task
When it comes to planning, it’s time. When it comes to tasks, it’s what to do. What do you do at any time? What time do you go to work every day? Every year’s National Day and New Year’s Day occur at fixed time nodes. Like these, we can call them timed tasks.
For example, sometimes. Quick off work, the leadership to an urgent task, Xiao Wang ah, add a shift. Tomorrow in a good mood, ask for a leave to interview. This is a temporary assignment.
So how do we do both of these things in Linux?
Task type
Timing: To do something repeatedly and continuously at regular intervals.
Temporary: The task ends after one execution, only once.
Command corresponding to the task type
Crontab: You will feel asphyxiated on a regular basis.
At (temporary mission): Perform a mission that ends once.
The use of the at
Command: the at
Grammar: at time
Common parameters:
-l Displays the task list. -c Displays the task detailsCopy the code
Operation: Check whether the service is enabled
[root@iz2ze0ajic0vbv28hcdctpz ~]# systemctl status atd● ATD. Service-job spooling tools Loaded: loaded (/usr/lib/systemd/system/atd.service; enabled; vendor preset: enabled)
Active: active (running) since 二 2018-11-13 11:50:47 CST; 1 day 9h ago
Main PID: 491 (atd)
CGroup: /system.slice/atd.service └ ─ 491 / usr/sbin/atd - fNovember 13 11:50:47 IZ2Ze0AJic0VBv28hCDCTpz SystemD [1]: Started Job Spooling Tools.11月 13 11:50:47 IZ2Ze0AJIC0VBV28HCDCTPZ SystemD [1]: Starting Job Spooling Tools...Copy the code
Several ways to set tasks: Create a task (default: today, next day if the time is longer than the current time)
[root@iz2ze0ajic0vbv28hcdctpz ~]# lssoft
[root@iz2ze0ajic0vbv28hcdctpz ~]# at 21:17at> mkdir sk
at> <EOT>
job 6 at Wed Nov 14 21:17:00 2018 Copy the code
Create at specified time:
[root@iz2ze0ajic0vbv28hcdctpz ~]# at 20:00 2018-11-19at> mkdir happy
at> <EOT>
job 7 at Mon Nov 19 20:00:00 2018
Copy the code
Add time from now on:
[root@iz2ze0ajic0vbv28hcdctpz ~]# at now + 3minat> mkdir hello
at> <EOT>
job 8 at Wed Nov 14 21:41:00 2018
Copy the code
Tomorrow at 11:11
[root@iz2ze0ajic0vbv28hcdctpz ~]# at 11:11 tomorrowat> mkdir world
at> <EOT>
job 9 at Thu Nov 15 11:11:00 2018
Copy the code
It will be executed at 3.24 PM two days later
[root@iz2ze0ajic0vbv28hcdctpz ~]# at 3:24 pm +2 daysat> mkdir new
at> <EOT>
job 11 at Fri Nov 16 15:24:00 2018
Copy the code
View the task list (shows tasks that have not yet been executed)
[root@iz2ze0ajic0vbv28hcdctpz ~]# atq5 Thu Nov 15 21:11:00 2018 a root
Copy the code
View the information of a single task :(remove some information due to too much content)
[root@iz2ze0ajic0vbv28hcdctpz /]# atq7 Mon Nov 19 20:00:00 2018 a root
5 Thu Nov 15 21:11:00 2018 a root
11 Fri Nov 16 15:24:00 2018 a root
9 Thu Nov 15 11:11:00 2018 a root
12 Wed Nov 14 22:11:00 2018 a root Copy the code
Example Delete task 12
[root@iz2ze0ajic0vbv28hcdctpz /]# atrm 12[root@iz2ze0ajic0vbv28hcdctpz /]# atq7 Mon Nov 19 20:00:00 2018 a root
5 Thu Nov 15 21:11:00 2018 a root
11 Fri Nov 16 15:24:00 2018 a root
9 Thu Nov 15 11:11:00 2018 a root Copy the code
Scheduled Task Processing
Cron is a scheduled task execution tool for Linux, crondtab is a periodic command execution, and Crond is a scheduled task daemon.
The service start
Check whether the service is startedCopy the code
[root@iz2ze0ajic0vbv28hcdctpz ~]# systemctl status crond● Crond. Service-command Scheduler Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor preset: enabled)
Active: active (running) since 二 2018-11-13 11:50:47 CST; 1 day 10h ago
Main PID: 489 (crond)
CGroup: /system.slice/crond.service └ ─ 489 / usr/sbin/crond - n11月 13 11:50:47 IZ2ZE0AJIC0VBv28hCDCTpz SystemD [1]: Started Command Scheduler11月 13 11:50:47 IZ2ZE0AJIC0VBV28HCDCTPZ SystemD [1]: Starting Command Scheduler...11月 13 11:50:47 IZ2ze0AJIC0VBv28hCDCTpz crond[489]: (CRON) INFO (RANDOM_DELAY will be scaled with factor 7... ed.)11月 13 11:50:47 iz2ze0ajic0vbv28hcdctpz crond[489]: (CRON) INFO (running with inotify support) Hint: Some lines were ellipsized, use -l to show in full. Copy the code
If no, run the commandsystemctl start crond
systemctl enable crond
Copy the code
Crontab command
Syntax: crontab parameter
Common parameters:
Crontab -u # Specify the cron information of the user crontab -l # List the cron information of the current user crontab -u user -l # List the cron information of the specified user crontab -r # Delete the cron service crontab -e # Edit the cron service crontab -r -u user # Delete the scheduled task for the specified userCopy the code
Operation:
Edit the Cron service
[root@iz2ze0ajic0vbv28hcdctpz ~]# crontab -ecrontab: installing new crontab
Copy the code
Cron. d/ is a task that the system automatically needs to do periodically (not in hours, minutes, seconds, etc.).Cron. deny Users in the file cannot perform scheduled tasksOther files are executed scripts of different cycles.Copy the code
Scheduled Task Rule
First, let’s look at the format of the task (see figure):
Some special symbols of time:
Here are some examples:
# Nginx reloads at 20:30 per night30 20 * * * /usr/local/nginx/sbin/nginx -s reload
# Nginx reloads at 5:30 on the 1st, 10th, 15th of each month1,10,15 * * /usr/local/nginx/sbin/nginx -s reload
# Nginx is reloaded every 30 minutes between 20:00 and 22:00 every day0,30 20-22 * * * /usr/local/nginx/sbin/nginx -s reload Nginx is reloaded every Saturday at 10:00pm0 22* * 6 /usr/local/nginx/sbin/nginx -s reload Nginx is reloaded every hour0 */1 * * * /usr/local/nginx/sbin/nginx -s reload Copy the code
expect
Sometimes we may need a tool that can interact with Linux without human intervention. Here we introduce a tool Expect. This summary is mainly about how to use Expect.
Expect to introduce
TCL(Tool Command Language) is a shell-like language that you can use to do a lot of things. Expect builds on it, and Expect offers commands that TCL doesn’t. If you want to write a script that handles input and output automatically, I recommend Expect.
Expect to install
yum install -y expect
Expect to use
1. Define the shell used to execute the script. #! /usr/bin/expect 2. Set the timeout period, in seconds. Timeout -1 indicates that it never times out. 3. The shell spawn needs to run in an Expect environment to pass interactive instructions. 4. The expect command checks whether the output contains a certain string and does not return immediately. Otherwise, it returns after a period of time and uses timeout to set the waiting time. 5. Send Performs the interactive action and input the action to be performed to the interactive command. At the end of the command string, "\r" should be added to check if there is an abnormal waiting state. 6. Exp_continue Continue with the subsequent interaction operations. 7. After interact, interact keeps the interaction state and transfers control to the console. If no interact is added, the interface exits automatically.Copy the code
➜ batch. / 35spawn ssh root@ip
root@ip's password:
Last login: Sat Jan 5 10:15:56 2019 from 113.46.187.213
Welcome to Alibaba Cloud Elastic Compute Service ! Copy the code
❝
This is just the tip of the Linux ice, and it’s still on the way. ️