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

How to use ulimit in linux

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

This article will explain in detail how to use ulimit in linux. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

1, description:

Ulimit is used for the resources used by shell to start the process.

2, category:

Shell built-in command

3. Syntax format:

Ulimit [- acdfHlmnpsStvw] [size]

4. Parameter description:

-H sets hardware resource limits.

-S sets software resource limits.

-a displays all current resource restrictions.

-c size: sets the maximum value of the core file. Unit: blocks

-d size: sets the maximum value of the data segment. Unit: kbytes

-f size: sets the maximum value for creating files. Unit: blocks

-l size: sets the maximum value for locking processes in memory. Unit: kbytes

-m size: sets the maximum amount of resident memory that can be used. Unit: kbytes

-n size: sets the maximum value of file descriptors that the kernel can open at the same time. Unit: n

-p size: sets the maximum value of the pipe buffer. Unit: kbytes

-s size: sets the maximum stack value. Unit: kbytes

-t size: sets the maximum limit of CPU usage time. Unit: seconds

-v size: sets the maximum value of virtual memory. Unit: kbytes

5. Give an example

When writing a program under Linux, if the program is relatively large, we will often encounter the problem of "segment error" (segmentation fault). This is mainly due to the fact that the initial stack size (stack size) of the Linux system is too small, usually 10m. I usually set the stack size to 256m so that there are no segment errors! The command is:

Ulimit-s 262140

If you want the system to remember this configuration automatically, edit the / etc/profile file. Under the line "ulimit-S-c 0 > / dev/null 2 > & 1", add "ulimit-s 262140" and save and restart the system.

Linux for each user, the system limits the maximum number of processes. To improve performance, depending on the equipment resources,

Set the maximum number of processes for each linux user. Below, I set the maximum number of processes for a linux user to 10000:

Ulimit-u 10000

For Java applications that need to make a lot of socket connections and keep them open

It is best to modify the number of files that can be opened per process by using ulimit-n xx, which defaults to 1024.

Ulimit-n 4096 increases the number of files that can be opened by each process to 4096, which defaults to 1024

Other important settings that are recommended to be set to unlimited (unlimited) are:

Segment length: ulimit-d unlimited

Maximum memory size: ulimit-m unlimited

Stack size: ulimit-s unlimited

CPU time: ulimit-t unlimited

Virtual memory: ulimit-v unlimited

Our company server needs to adjust the stack size parameter of ulimit to unlimited unlimited. When using ulimit-s unlimited, it can only be effective in the shell at that time. Reopening a shell will become invalid. So you have to add ulimit-s unlimited at the end of / etc/profile, and source / etc/profile makes the modified file effective.

PS: if you come across a similar error prompt:

Ulimit: max user processes: cannot modify limit: disallowed operation

Ulimit: open files: cannot modify limit: disallowed operation

Why are root users allowed? Will ordinary users encounter such a problem again?

Take a look at / etc/security/limits.conf and you'll probably understand.

Linux has default ulimit restrictions for users, and this file can configure the user's hard configuration and soft configuration, and the hard configuration is the upper limit.

Changes that exceed the upper limit will result in errors such as "disallowed actions".

Add in limits.conf

* soft noproc 10240

* hard noproc 10240

* soft nofile 10240

* hard nofile 10240

It limits the maximum number of threads and files for any user to 10240.

=

When deploying applications under Linux, you will sometimes encounter the problem of Socket/File: Can't open so many files. This value will also affect the maximum number of concurrency of the server. In fact, Linux is limited by file handle, and the Linux is not very high by default, which is usually 1024. It is easy to reach this number for production servers. The following is how to correct this system default value by correcting the configuration. Because I encountered this problem when configuring Nginx+php5, I summarized this article into the nginx+apache article.

View method

We can use ulimit-a to see all the limit values

[root@centos5] # ulimit-a

Core file size (blocks,-c) 0

Data seg size (kbytes,-d) unlimited

Max nice (- e) 0

File size (blocks,-f) unlimited

Pending signals (- I) 4096

Max locked memory (kbytes,-l) 32

Max memory size (kbytes,-m) unlimited

Open files (- n) 1024

Pipe size (512 bytes,-p) 8

POSIX message queues (bytes,-Q) 819200

Max rt priority (- r) 0

Stack size (kbytes,-s) 10240

Cpu time (seconds,-t) unlimited

Max user processes (- u) 4096

Virtual memory (kbytes,-v) unlimited

File locks (- x) unlimited | |

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