温馨提示×

CentOS ulimit CPU时间限制方法

小樊
39
2025-08-28 21:07:53
栏目: 智能运维

在 CentOS 系统中,你可以使用 ulimit 命令来设置或查看用户进程的资源限制,包括 CPU 时间

要设置 CPU 时间限制,请按照以下步骤操作:

  1. 打开终端。

  2. 使用 ulimit 命令设置 CPU 时间限制。例如,如果你想将用户进程的 CPU 时间限制设置为 300 秒,可以使用以下命令:

ulimit -t 300 

这将仅影响当前 shell 会话中的新进程。如果你想永久更改 CPU 时间限制,请执行以下步骤:

  1. 打开 /etc/security/limits.conf 文件:
sudo vi /etc/security/limits.conf 
  1. 在文件末尾添加以下行(用实际的用户名替换 <username>):
<username> soft core 0 <username> hard core 0 <username> soft nproc <max_processes> <username> hard nproc <max_processes> <username> soft nofile <max_files> <username> hard nofile <max_files> <username> soft memlock unlimited <username> hard memlock unlimited <username> soft as unlimited <username> hard as unlimited <username> soft cpu 300 <username> hard cpu 300 

<username> 替换为实际的用户名,将 <max_processes><max_files> 替换为你希望设置的相应限制。

  1. 保存并关闭文件。

  2. 重新登录以使更改生效。

现在,你已经成功设置了 CentOS 系统中用户的 CPU 时间限制。请注意,这些设置仅适用于普通用户。如果你需要为 root 用户设置限制,请使用 root 用户登录并重复上述步骤。

0