1. ulimit
  2. stdin,stderr,stdout

1. ulimit

ulimit 用于shell启动进程所占用的资源。

可以使用该命令查看进程占用资源的情况。

使用方法:ulimit [-acdfHlmnpsStvw] [size]

core file size          (blocks, -c) 0
data seg size           (kbytes, -d) unlimited
scheduling priority             (-e) 0
file size               (blocks, -f) unlimited
pending signals                 (-i) 15463
max locked memory       (kbytes, -l) 64
max memory size         (kbytes, -m) unlimited
open files                      (-n) 32768
pipe size            (512 bytes, -p) 8
POSIX message queues     (bytes, -q) 819200
real-time priority              (-r) 0
stack size              (kbytes, -s) 8192
cpu time               (seconds, -t) unlimited
max user processes              (-u) 15463
virtual memory          (kbytes, -v) unlimited
file locks                      (-x) unlimited
# /etc/security/limits.conf

* soft nofile 32768
* hard nofile 65536

2. stdin,stderr,stdout

In computing, the term stream refers to something that can transfer data. Here, all three streams carry text as the data.

  • stdin: Stands for standard input. It takes text as input.
  • stdout: Stands for standard output. The text output of a command is stored in the stdout stream.
  • stderr: Stands for standard error. Whenever a command faces an error, the error message is stored in this stream.

The command will require input from the keyboard. Here, the read tool is getting the input from stdin.

read	

The list is sent to stdout and the terminal prints it out. Let’s check stderr now.

ls -l

There are different ways an error may occur. For this example, sending ls an invalid argument will result in an error.

ls -l anything

1. Piping

This is a common technique that takes full advantage of the stdin and stdout streams.

Here, the | sign is responsible for piping. The output echo generates is written in the stdout stream. Then, the piping redirects the content of stdout to stdin for the grep command. That’s how grep knows what content to perform the operation on.

echo "hello world" | grep hello

# If you want to pipe both the stderr and stdout to the next command, then use the “|&” instead.
echo "hello world" |& cat	

2. Redirecting streams

To redirect the stdout content to a file, add the “>” angle followed by the target file name.

echo “hello world” > hello.txt

If the file did already exists, then the command above will overwrite it. To avoid it, make sure that the file name is unique. If you don’t want to overwrite, you may want to use “»” instead. It appends the output at the end of the target file.

echo "hello world" >> hello.txt

The goal of stdin is to work with input. This can also be redirected. For example, instead of typing the input from the keyboard, it can be loaded from a file.

In this command, cat will take its input directly from the hello.txt file.

cat < hello.txt

Interestingly, you can redirect both stdin and stdout in the same command line. Here, the following command will use hello.txt as stdin and send the stdout of the command to a file.

python3 pyin.py < hello.txt > output.txt

Redirecting stderr is similar to stdout. However, you need to mention the description ID 2 for indicating stderr. Otherwise, it’ll just use stdout.

Here, I’ll be redirecting the content of the stderr to a text file.

anything 2> error.txt

3. Redirecting Stdout and Stderr

Yes, it’s possible to redirect both of them simultaneously. All you need is to mention the description ID 1 and 2 before the redirection.

echo “hello world” 1>output.log 2>debug.log

4. Redirect all output to /dev/null

First, we’re dumping all the stdout to /dev/null. Then, in the second part, we’re telling bash to send stderr to stdout. In this example, there’s nothing to output. However, if you’re confused, you can always check if the command ran successfully.

grep -r hello /sys/ > /dev/null 2>$1

Reference

bash_stdin_stdout_stderr