Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

[shell] & the role of gt;/dev/null 2 GTTTITING AMP 1

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

Giving up will not be more comfortable, it will only be doomed forever.

Hold on, there will be everything that follows-August 26th, 2017.8

When we write shell or look at scripts in the system, we often encounter text messages in the title, and many people don't know what it means (What), how to use it (How), where to use it (Where), when to use it (When), and why it is used so (Why).

The following editor will take you to analyze it step by step. I believe that after reading this article, you will suddenly become enlightened, and my mother will no longer have to worry about my study.

.

I. knowledge reserve

1. File descriptor

When Linux shell executes the command, each process is associated with three open files and references them using file descriptors. Because the file descriptor is not easy to remember, shell also gives the corresponding file name:

File descriptor input file-standard input 0 (default is keyboard, 0 is output of file or other command) output file-standard output 1 (default is screen, 1 is file) error output file-standard error 2 (default is screen, 2 is file)

Here is an extension of the file descriptor:

We often encounter the prompt of "Too many open files" when testing or operation and maintenance of an application, which means that the file handle you are operating has exceeded the parameter configuration of the system and needs to be modified accordingly. Here is a brief description of how to modify the file handle in linux and solaris systems:

1.1.The Linux platform

A. View the configuration

View the current value of the system through the command: ulimit-n

To view the number of open file descriptors currently used by the system, you can use the following command:

[root@localhost ~] # cat / proc/sys/fs/file-nr

1632 0 1513506

The first number represents the number of open file descriptors assigned to the current system

The second number is those that have been released after allocation (currently no longer in use)

The third number equals file-max.

B. Modify the configuration

The Linux kernel itself has a limit on the maximum value of the file descriptor, which you can change as needed:

Maximum number of open file descriptors for the system: / proc/sys/fs/file-max

Temporary setting: echo 1000000 > / proc/sys/fs/file-max

Permanent settings: modify / etc/sysctl.conf file, add fs.file-max = 1000000

Maximum number of open file descriptors for a process

Use ulimit-n to view the current settings. Use ulimit-n 1000000 for temporary settings.

To take effect permanently, you can modify the / etc/security/limits.conf file by adding the following line:

* hard nofile 1000000

* soft nofile 1000000

Root hard nofile 1000000

Root soft nofile 1000000

(for those of you who have installed oracle database, the above information will be familiar.)

It is also important to note that hard limit cannot be greater than / proc/sys/fs/nr_open, so sometimes you need to change the value of nr_open.

Execute echo 2000000 > / proc/sys/fs/nr_open

C, summary

All processes cannot open more than / proc/sys/fs/file-max file descriptors

The number of file descriptors opened by a single process cannot exceed the soft limit of nofile in user limit

The soft limit of nofile cannot exceed its hard limit

The hard limit of nofile cannot exceed / proc/sys/fs/nr_open

1.2.The Solaris platform

The maximum number of file handles a process can open determines the number of files that each process can open at the same time. The default value on Solaris10 is 256. for some applications, the default value is too small and needs to be modified manually.

A. View the configuration

There are two ways, one is to use the ulimit command, and the other is to use the prctl command

A), ulimit command

Oracle@hisdb:~ $> ulimit-a

Core file size (blocks,-c) unlimited

Data seg size (kbytes,-d) unlimited

File size (blocks,-f) unlimited

Open files (- n) 256

Pipe size (512 bytes,-p) 10

Stack size (kbytes,-s) 10240

Cpu time (seconds,-t) unlimited

Max user processes (- u) 11445

Virtual memory (kbytes,-v) unlimited

Open files represents the maximum number of file handles that a process can open.

B), prctl command

Oracle@hisdb:~ $> prctl-I process $$

Process: 1079:-bash

NAME PRIVILEGE VALUE FLAG ACTION RECIPIENT

……

Process.max-file-descriptor

Basic 256-deny 1079

Privileged 65.5K-deny-

System 2.15G max deny-

……

Process.max-file-descriptor represents the maximum number of file handles that a process can open, where basic represents a soft limit and privileged represents a hard limit. Non-root users can adjust the soft and hard limit values within the hard limit.

B. Modify the configuration

There are usually several ways to modify this parameter:

1), use the ulimit command or plimit command to modify

The ulimit command can only modify the settings of the current SHELL and its child processes, which takes effect immediately after setting, and becomes invalid as soon as the current SHELL exits the setting. The-S parameter is used to set the soft limit, and the-H parameter is used to set the hard limit.

Set soft limits:

Oracle@hisdb:~ $> ulimit-S-n 1024

Oracle@hisdb:~ $> ulimit-a

Core file size (blocks,-c) unlimited

Data seg size (kbytes,-d) unlimited

File size (blocks,-f) unlimited

Open files (- n) 1024

Pipe size (512 bytes,-p) 10

Stack size (kbytes,-s) 10240

Cpu time (seconds,-t) unlimited

Max user processes (- u) 11445

Virtual memory (kbytes,-v) unlimited

Oracle@hisdb:~ $> prctl-I process $$

Process: 1079:-bash

NAME PRIVILEGE VALUE FLAG ACTION RECIPIENT

……

Process.max-file-descriptor

Basic 1.02K-deny 1079

Privileged 65.5K-deny-

System 2.15G max deny-

……

Oracle@hisdb:~ $> ulimit-S-n 65536

Oracle@hisdb:~ $> ulimit-a

Core file size (blocks,-c) unlimited

Data seg size (kbytes,-d) unlimited

File size (blocks,-f) unlimited

Open files (- n) 65536

Pipe size (512 bytes,-p) 10

Stack size (kbytes,-s) 10240

Cpu time (seconds,-t) unlimited

Max user processes (- u) 11445

Virtual memory (kbytes,-v) unlimited

Oracle@hisdb:~ $> ulimit-S-n 65537

-bash: ulimit: open files: cannot modify limit: Invalid argument

The soft limit can only be adjusted below the value of privileged, and in this case the maximum value that can be adjusted is 65536.

Set hard limit

Oracle@hisdb:~ $> ulimit-a

Core file size (blocks,-c) unlimited

Data seg size (kbytes,-d) unlimited

File size (blocks,-f) unlimited

Open files (- n) 256

Pipe size (512 bytes,-p) 10

Stack size (kbytes,-s) 10240

Cpu time (seconds,-t) unlimited

Max user processes (- u) 11445

Virtual memory (kbytes,-v) unlimited

Open files shows soft limits in the ulimit command, and you can use the prctl command to display hard limits, that is, privileged values.

Oracle@hisdb:~ $> prctl-I process $$

Process: 1139:-bash

NAME PRIVILEGE VALUE FLAG ACTION RECIPIENT

……

Process.max-file-descriptor

Basic 256-deny 1139

Privileged 65.5K-deny-

System 2.15G max deny-

……

Oracle@hisdb:~ $> ulimit-H-n 65537

-bash: ulimit: open files: cannot modify limit: Not owner

Oracle@hisdb:~ $> ulimit-H-n 32768

Oracle@hisdb:~ $> ulimit-a

Core file size (blocks,-c) unlimited

Data seg size (kbytes,-d) unlimited

File size (blocks,-f) unlimited

Open files (- n) 256

Pipe size (512 bytes,-p) 10

Stack size (kbytes,-s) 10240

Cpu time (seconds,-t) unlimited

Max user processes (- u) 11445

Virtual memory (kbytes,-v) unlimited

Oracle@hisdb:~ $> prctl-I process $$

Process: 1139:-bash

NAME PRIVILEGE VALUE FLAG ACTION RECIPIENT

Process.max-file-descriptor

Basic 256-deny 1139

Privileged 32.8K-deny-

System 2.15G max deny-

Non-root users can only adjust the hard limit to minor, not major.

2), modify / etc/system parameter

On Solaris10, this method is no longer recommended, but it still works. The setting parameters in / etc/system are globally valid, that is, all users are affected. And after it is set up, the system needs to be rebooted to take effect.

The setting method is to add the following two parameters to the / etc/system file, and then restart the system.

Set rlim_fd_cur=1024

Set rlim_fd_max=65535

The following are descriptions of the two parameters:

Rlim_fd_max (hard limit)

Description: Specifies the "hard" limit on file descriptors that a single process might have open.Overriding this limit requires superuser privilege.

Data Type: Signed integer

Default: 65536

Range: 1 to MAXINT

Units: File descriptors

Dynamic?: No

Validation: None

When to Change: When the maximum number of open files for a process is not enough. Other limitations in system facilities can mean that a larger number of file descriptors is not as useful as it might be. For example:

■ A 32-bit program using standard I/O is limited to 256 file descriptors. A 64-bit program using standard I/O can use up to 2 billion descriptors. Specifically, standard I/O refers to the

Stdio (3C) functions in libc (3LIB).

■ select is by default limited to 1024 descriptors per fd_set. For more information, see select (3C). Starting with the Solaris 7 release, 32-bit application code can be recompiled with a larger fd_set size (less than or equal to 65536). A 64-bit application uses an fd_set size of 65,536, which cannot be changed.

An alternative to changing this on a system wide basis is to use the plimit (1) command. If a parent process has its limits changed by plimit, all children inherit the increased limit. This alternative is useful for daemons such as inetd.

Commitment Level: Unstable

ChangeHistory: For information, see "rlim_fd_max (Solaris 8 Release)" on page 184.

Rlim_fd_cur (soft limit)

Description: Defines the "soft" limit on file descriptors that a single process can have open. A process might adjust its file descriptor limit to any value up to the "hard" limit defined by rlim_fd_max by using the setrlimit () call or by issuing the limit command in whatever shell it is running. You do not require superuser privilege to adjust the limit to any value less than or equal to the hard limit.

Data Type: Signed integer

Default: 256

Range: 1 to MAXINT

Units: File descriptors

Dynamic?: No

Validation: Compared to rlim_fd_max. If rlim_fd_cur is greater than rlim_fd_max, rlim_fd_cur is reset to rlim_fd_max.

When to Change: When the default number of open files for a process is not enough. Increasing this value means only that it might not be necessary for a program to use setrlimit to increase the maximum number of file descriptors available to it.

Commitment Level: Unstable

3), project command

Project is a new addition to Solaris10, and you can set parameter values for a user or group of users by setting the project parameter. It can take effect immediately after setting.

The following is an example of the settings:

Root@hisdb:/ # > projadd user.test (create project user.test)

Root@hisdb:/ # > id-p test

Uid=100 (test) gid=1 (other) projid=100 (user.test) (test user belongs to project user.test)

Root@hisdb:/ # > projmod-a-K "process.max-file-descriptor= (basic,65537,deny)" user.test

Root@hisdb:/ # > projmod-a-K "process.max-file-descriptor= (priv,65538,deny)" user.test

Root@hisdb:/ # > grep 'user.test' / etc/project

User.test:100::::process.max-file-descriptor= (basic,65537,deny), (priv,65538,deny)

Set the basic and privilege values to 65537 and 65538, respectively, exceeding the maximum hard limit in / etc/system

Root@hisdb:/ # > tail-2 / etc/system

Set rlim_fd_cur=1024

Set rlim_fd_max=65535

Root@hisdb:/ # > plimit $$

1041:-bash

Resource current maximum

Time (seconds) unlimited unlimited

File (blocks) unlimited unlimited

Data (kbytes) unlimited unlimited

Stack (kbytes) 10240 unlimited

Coredump (blocks) unlimited unlimited

Nofiles (descriptors) 1024 65535

Vmemory (kbytes) unlimited unlimited

The results of root users are only affected by the parameters in / etc/system, not by project user.test. Root users do not belong to this project.

Root@hisdb:/ # > su-test

Oracle Corporation SunOS 5.10 Generic Patch January 2005

Test@hisdb:~ $> plimit $$

1091:-bash

Resource current maximum

Time (seconds) unlimited unlimited

File (blocks) unlimited unlimited

Data (kbytes) unlimited unlimited

Stack (kbytes) 10240 unlimited

Coredump (blocks) unlimited unlimited

Nofiles (descriptors) 65535 65535

Vmemory (kbytes) unlimited unlimited

Test@hisdb:~ $> id-p

Uid=100 (test) gid=1 (other) projid=100 (user.test)

The current and maximum values of user test are 65535, while the values set in project user.test are 65537 and 65538, respectively. The user result is inconsistent with the project setting value. This is because the value set in project exceeds the maximum hard limit set in / etc/system is 65535. At this time, the system automatically adjusts the user result to the maximum hard limit set in / etc/system.

Root@hisdb:/ # > projmod-s-K "process.max-file-descriptor= (basic,32767,deny), (priv,32768,deny)" user.test

Root@hisdb:/ # > plimit $$

1041:-bash

Resource current maximum

Time (seconds) unlimited unlimited

File (blocks) unlimited unlimited

Data (kbytes) unlimited unlimited

Stack (kbytes) 10240 unlimited

Coredump (blocks) unlimited unlimited

Nofiles (descriptors) 1024 65535

Vmemory (kbytes) unlimited unlimited

Root users are not affected by project user.test adjustments.

Root@hisdb:/ # > su-test

Oracle Corporation SunOS 5.10 Generic Patch January 2005

Test@hisdb:~ $> plimit $$

1099:-bash

Resource current maximum

Time (seconds) unlimited unlimited

File (blocks) unlimited unlimited

Data (kbytes) unlimited unlimited

Stack (kbytes) 10240 unlimited

Coredump (blocks) unlimited unlimited

Nofiles (descriptors) 32767 32768

Vmemory (kbytes) unlimited unlimited

The current and maximum values of the user test are consistent with the settings in project user.test.

Note:

Note the following when setting parameters in both / etc/system and project in the system:

1. The setting of / etc/system is a global setting and affects all users, while the setting of project only affects users who belong to this project.

2. The setting of / etc/system requires a restart of the system to take effect, while the setting of project takes effect immediately (new process).

3. The hard limit of / etc/system is the maximum limit for all users. If the setting value in project exceeds the hard limit value of / etc/system, the project setting is invalid and the corresponding user value is set to the hard limit value of / etc/system.

1.3.The HPUX platform

A. View the configuration

Through the command: ulimit-a

B. Modify the configuration

1), through the sam (smh) command

2) set the core environment of HP-UX and manage the core environment. However, the core parameters can not be managed immediately after modification. Because the system reads the parameters to boot.config, you have to remove the boot.config and then rebuild the boot.config file with getkinfo. The getkinfo command is automatically run in SAM-- "Kernel configuration-- > Parameter."

Modify / usr/conf/master.d/core-hpux first:

* range maxfiles > filename redirect standard output to a file (append) Command > filename redirect standard output to a file Command > filename2 > & 1 redirect standard output and errors to a file Command 2 > filename redirect standard errors to a file Command 2 > > filename redirect standard output to a file (append) Command > > filename2 > & 1 redirect standard output and errors to a file (append)

2.2, input redirection

Command

< filename >

The filename2Command command takes filename files as standard input and filename2 files as standard output Command

< filenameCommand命令以filename文件作为标准输入Command &m把标准输出重定向到文件描述符m中Command < &-关闭标准输入Command 0>

& ditto

3. / dev/null introduction

It is a black hole device that discards everything written into it, and empty devices are often used to discard unwanted output streams. I remember when I used windows, there was a similar device: NUL, which has the same function as this one. Any data written to the device will be discarded. The return of reading data from this is empty. Send some unwanted content to this device frequently, discarding unwanted data.

II. > / dev/null 2 > & 1 Analysis

I've talked too much in front of you. Are you a little impatient? don't worry. If you read the basic knowledge carefully, I believe this problem will be easily solved:

Let's break down the topic as follows:

/ dev/null stands for empty device file

> indicates where to redirect, for example: echo "123" > / home/123.txt

1 represents stdout standard output, and the system default value is 1, so "> / dev/null" equals "1 > / dev/null"

2 indicates stderr standard error

& indicates the equivalent meaning, 2 > & 1, indicating that the output redirection of 2 is equivalent to 1

Then the correct understanding of the title of this article is:

1 > / dev/null first means that standard output is redirected to an empty device file, that is, no information is output to the terminal, to put it bluntly, no information is displayed.

2 > & 1 then, the standard error output redirection is equivalent to the standard output, because the standard output has been redirected to the empty device file, so the standard error output is also redirected to the empty device file.

Ladies and gentlemen, I don't know if you understand it or not, you should understand What/How/Where/When/Why. If you don't understand, please look back and watch it a few more times. I believe you will get something.

If you feel that you have gained something and are willing to continue to learn, please scan the code and follow:

You can also transfer money to appreciate it.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report