在 CentOS 系統中,你可以使用 ulimit 命令來設置或查看用戶進程的資源限制,包括 CPU 時間
要設置 CPU 時間限制,請按照以下步驟操作:
打開終端。
使用 ulimit 命令設置 CPU 時間限制。例如,如果你想將用戶進程的 CPU 時間限制設置為 300 秒,可以使用以下命令:
ulimit -t 300
這將僅影響當前 shell 會話中的新進程。如果你想永久更改 CPU 時間限制,請執行以下步驟:
/etc/security/limits.conf 文件:sudo vi /etc/security/limits.conf
<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> 替換為你希望設置的相應限制。
保存并關閉文件。
重新登錄以使更改生效。
現在,你已經成功設置了 CentOS 系統中用戶的 CPU 時間限制。請注意,這些設置僅適用于普通用戶。如果你需要為 root 用戶設置限制,請使用 root 用戶登錄并重復上述步驟。