溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

[shell]>/dev/null 2>&1 的作用

發(fā)布時間:2020-08-12 08:41:49 來源:ITPUB博客 閱讀:192 作者:dbasdk 欄目:建站服務(wù)器

 [shell]>/dev/null 2>&1 的作用

放棄不會更舒服,只會萬劫不復(fù)

撐住,才有后來的一切 ---2017.8.26

我們在編寫shell的時候或者查看系統(tǒng)中的腳本程序時,經(jīng)常會碰到題目中的文字信息,很多人搞不清楚這是什么意思(What),如何使用(How),用在何處(Where),何時要用(When),為什么這么用(Why)。

下面小編帶你逐步剖析一下,相信讀過本文后,你會豁然開朗,媽媽再也不用擔(dān)心我的學(xué)習(xí)了[shell]>/dev/null 2>&1 的作用。

一、知識儲備

1、文件描述符

Linux shell執(zhí)行命令時,每個進程都和三個打開的文件相聯(lián)系,并使用文件描述符來引用這些文件。由于文件描述符不容易記憶,shell同時也給出了相應(yīng)的文件名


文件 文件描述符
輸入文件—標(biāo)準(zhǔn)輸入 0(缺省是鍵盤,為0時是文件或者其他命令的輸出)
輸出文件—標(biāo)準(zhǔn)輸出 1(缺省是屏幕,為1時是文件)
錯誤輸出文件—標(biāo)準(zhǔn)錯誤 2(缺省是屏幕,為2時是文件)

這里對文件描述符作一下引申:

我們在進行應(yīng)用的測試或者運維時經(jīng)常會碰到“Too many open files”的提示,這說明你操作的文件句柄已經(jīng)超過了系統(tǒng)的參數(shù)配置,需要進行相應(yīng)修改,下面簡單介紹一下linux與solaris系統(tǒng)下文件句柄的修改方式:

1.1、Linux平臺

A、查看配置

通過命令:ulimit -n 進行查看系統(tǒng)當(dāng)前值;

查看當(dāng)前系統(tǒng)使用的打開文件描述符數(shù),可以使用下面的命令:

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

1632 0 1513506

  • 第一個數(shù)表示當(dāng)前系統(tǒng)已分配使用的打開文件描述符數(shù)

  • 第二個數(shù)為分配后已釋放的(目前已不再使用)

  • 第三個數(shù)等于file-max。

B、修改配置

Linux內(nèi)核本身有文件描述符最大值的限定,你可以根據(jù)需要更改:

  • 系統(tǒng)最大打開文件描述符數(shù):/proc/sys/fs/file-max

  • 臨時性設(shè)置:echo 1000000 > /proc/sys/fs/file-max

    永久設(shè)置:修改/etc/sysctl.conf文件,增加fs.file-max = 1000000

  • 進程最大打開文件描述符數(shù)
    使用
    ulimit -n查看當(dāng)前設(shè)置。使用ulimit -n 1000000進行臨時性設(shè)置。
    要想永久生效,你可以修改
    /etc/security/limits.conf文件,增加下面的行:

                   * hard nofile 1000000

               * soft nofile 1000000

               root hard nofile 1000000

               root soft nofile 1000000

(對于安裝過oracle數(shù)據(jù)庫的各位,上面的信息想必不會陌生)

還有一點要注意的就是hard limit不能大于/proc/sys/fs/nr_open,因此有時你也需要修改nr_open的值。

執(zhí)行echo 2000000 > /proc/sys/fs/nr_open

C、總結(jié)

  • 所有進程打開的文件描述符數(shù)不能超過/proc/sys/fs/file-max

  • 單個進程打開的文件描述符數(shù)不能超過user limit中nofile的soft limit

  • nofile的soft limit不能超過其hard limit

  • nofile的hard limit不能超過/proc/sys/fs/nr_open

1.2、Solaris平臺

進程能夠打開的最大文件句柄數(shù)決定了每個進程能夠同時打開的文件數(shù)量。Solaris10上缺省值是256,對于某些應(yīng)用而言,缺省值太小,需要手工修改。

A、查看配置

有兩種方式,一是使用ulimit命令,二是使用prctl命令;

a)、ulimit命令

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表示進程能夠打開的最大文件句柄數(shù)量。

b)、prctl命令

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表示進程能夠打開的最大文件句柄數(shù),其中basic表示軟限制,privileged表示硬限制。非root用戶可以在硬限制的范圍內(nèi)自行調(diào)整軟硬限制值。

B、修改配置

修改此參數(shù)通常有以下幾種方法:

1)、使用ulimit命令或plimit命令修改

ulimit命令只能修改當(dāng)前SHELL及其子進程的設(shè)置,設(shè)置后立即生效,一旦當(dāng)前SHELL退出設(shè)置即失效。-S參數(shù)用于設(shè)置軟限制,-H參數(shù)用于設(shè)置硬限制。

  • 設(shè)置軟限制:

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

軟限制只能在privileged的值以下調(diào)整,此例中所能調(diào)整的最大值是65536。

  • 設(shè)置硬限制

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

ulimit命令中open files顯示的是軟限制,可以用prctl命令顯示硬限制,即privileged值。

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                                 -

非root用戶調(diào)整硬限制時只能往小調(diào),不能往大調(diào)。

2)、修改/etc/system參數(shù)

在Solaris10上,這種方法已經(jīng)不建議使用,但這種方式仍然有效。/etc/system中設(shè)置參數(shù)是全局有效的,即所有用戶均會受影響。并且設(shè)置后,需要重啟系統(tǒng)才能生效。

設(shè)置方法是在/etc/system文件中增加以下兩個參數(shù),然后重啟系統(tǒng)。

set rlim_fd_cur=1024

set rlim_fd_max=65535


  • 以下是兩個參數(shù)的說明:

  • rlim_fd_max (硬限制) 

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 : 65,536 

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 65,536). 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 (軟限制) 

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命令

project是Solaris10新增加的特性,可以通過設(shè)置project參數(shù)為一個用戶或一組用戶設(shè)置參數(shù)值。設(shè)置后可立即生效。

以下是設(shè)置示例:

root@hisdb:/ #>projadd user.test  (創(chuàng)建project user.test) 

root@hisdb:/ #>id -p test

uid=100(test) gid=1(other) projid=100(user.test)   (test用戶屬于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)

設(shè)置basic和privilege值分別為65537和65538,越過/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

root用戶的結(jié)果只受/etc/system里參數(shù)的影響,而不受project user.test影響,root用戶不屬于此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)

用戶test當(dāng)前值和最大值都是65535,而project user.test里設(shè)置的值分別是65537和65538,用戶結(jié)果與project設(shè)置值不一致,這是因為project中設(shè)置的值超過了/etc/system里設(shè)置的最大硬限制數(shù)是65535,此時系統(tǒng)自動將用戶結(jié)果調(diào)整為/etc/system中設(shè)置的最大硬限制數(shù)。

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用戶未受project user.test調(diào)整影響。 

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

用戶test當(dāng)前值和最大值均與project user.test中的設(shè)置一致。

注:

如果在系統(tǒng)里同時設(shè)置/etc/system和project里的參數(shù)時要注意以下幾點:

1. /etc/system的設(shè)置是全局設(shè)置,會影響所有用戶,而project的設(shè)置僅影響屬于此project的用戶。

2. /etc/system的設(shè)置需要重啟系統(tǒng)才能生效,而project的設(shè)置立即生效(新進程)。

3. /etc/system的硬限制值是所有用戶的最大限制值。如果project中的設(shè)置值超過了/etc/system的硬限制值,則project設(shè)置無效,相應(yīng)用戶值會被設(shè)置為/etc/system的硬限制值。

1.3、HPUX平臺

A、查看配置

通過命令:ulimit -a

B、修改配置

1)、通過sam(smh)命令

2)、設(shè)定HP-UX的核心環(huán)境,對核心環(huán)境進行管理。但修改后不能立即對核心參數(shù)進行管理。因為系統(tǒng)會向boot.config讀出參數(shù),所以只有移走boot.config,然后再用getkinfo重建boot.config文件。在SAM--》Kernel configuration--> Parameter會自動運行g(shù)etkinfo 命令。

  • 先修改/usr/conf/master.d/core-hpux: 

*range maxfiles<=60000 

*range maxfiles_lim<=60000 

  • 把/var/sam/boot.config文件mv成boot.config.bak 

    mv /var/sam/boot.config /var/sam/boot.config.bak 

  • 然后運行:/usr/sam/lbin/getkinfo -b 

1.4、AIX平臺

A、查看配置

通過命令:ulimit -a

B、修改配置

1)、通過工具smit進行修改

2)、修改文件:/etc/security/limits

2、文件重定向

2.1、輸出重定向

Command > filename 把標(biāo)準(zhǔn)輸出重定向到一個新文件中
Command >> filename 把標(biāo)準(zhǔn)輸出重定向到一個文件中(追加)
Command > filename 把標(biāo)準(zhǔn)輸出重定向到一個文件中
Command > filename 2>&1 把標(biāo)準(zhǔn)輸出和錯誤一起重定向到一個文件中
Command 2 > filename 把標(biāo)準(zhǔn)錯誤重定向到一個文件中
Command 2 >> filename 把標(biāo)準(zhǔn)輸出重定向到一個文件中(追加)
Command >> filename2>&1 把標(biāo)準(zhǔn)輸出和錯誤一起重定向到一個文件(追加)

2.2、輸入重定向

Command < filename > filename2 Command命令以filename文件作為標(biāo)準(zhǔn)輸入,以filename2文件作為標(biāo)準(zhǔn)輸出
Command < filename Command命令以filename文件作為標(biāo)準(zhǔn)輸入
Command << delimiter  從標(biāo)準(zhǔn)輸入中讀入,知道遇到delimiter分界符

2.3、綁定重定向

Command >&m 把標(biāo)準(zhǔn)輸出重定向到文件描述符m中
Command < &- 關(guān)閉標(biāo)準(zhǔn)輸入
Command 0>&- 同上

3、/dev/null介紹

是個黑洞設(shè)備,它丟棄一切寫入其中數(shù)據(jù),空設(shè)備通常被用于丟棄不需要的輸出流。記得當(dāng)年用windows時候,有個類似的設(shè)備:NUL ,跟這個功能一樣。任何寫入該設(shè)備數(shù)據(jù)都會被丟棄掉。從這個里面讀取數(shù)據(jù)返回是空。將一些不用內(nèi)容經(jīng)常發(fā)送給這個設(shè)備,丟棄不需要的數(shù)據(jù)。

二、>/dev/null 2>&1剖析

前面啰嗦了太多,各位是不是有點不耐煩了,不要著急,如果你仔細(xì)閱讀了前面的基礎(chǔ)知識,相信這個問題就迎刃而解了:

我們把題目分解開來如下:

  • /dev/null 代表空設(shè)備文件 

  • > 代表重定向到哪里,例如:echo "123" > /home/123.txt 

  • 1 表示stdout標(biāo)準(zhǔn)輸出,系統(tǒng)默認(rèn)值是1,所以">/dev/null"等同于"1>/dev/null" 

  • 2 表示stderr標(biāo)準(zhǔn)錯誤 

  • & 表示等同于的意思,2>&1,表示2的輸出重定向等同于1

那么本文標(biāo)題的正確理解為: 

  • 1>/dev/null 首先表示標(biāo)準(zhǔn)輸出重定向到空設(shè)備文件,也就是不輸出任何信息到終端,說白了就是不顯示任何信息。 

  • 2>&1 接著,標(biāo)準(zhǔn)錯誤輸出重定向等同于 標(biāo)準(zhǔn)輸出,因為之前標(biāo)準(zhǔn)輸出已經(jīng)重定向到了空設(shè)備文件,所以標(biāo)準(zhǔn)錯誤輸出也重定向到空設(shè)備文件。 

各位看官,不知你理解了沒有,你應(yīng)該明白What/How/Where/When/Why,如果沒有理解的話,請再回頭多看幾遍,相信你會有收獲的。


如果你覺得有所收獲并愿意繼續(xù)學(xué)習(xí)的話,請掃碼關(guān)注:

[shell]>/dev/null 2>&1 的作用

[shell]>/dev/null 2>&1 的作用[shell]>/dev/null 2>&1 的作用[shell]>/dev/null 2>&1 的作用你也可以轉(zhuǎn)賬贊賞喲[shell]>/dev/null 2>&1 的作用[shell]>/dev/null 2>&1 的作用[shell]>/dev/null 2>&1 的作用

          [shell]>/dev/null 2>&1 的作用

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI