在 CentOS 中,您可以使用 nohup
命令結合 ulimit
命令來限制資源使用
打開終端。
使用 ulimit
命令設置資源限制。例如,以下命令將限制進程的最大 CPU 時間為 300 秒(5 分鐘),最大文件大小為 100MB,以及最大進程數為 5:
ulimit -t 300 -f 100M -u 5
nohup
命令運行您的程序。例如,如果您要運行名為 my_script.sh
的腳本,可以使用以下命令:nohup ./my_script.sh &
這將在后臺運行腳本,并將其輸出重定向到名為 nohup.out
的文件。
請注意,ulimit
命令設置的資源限制僅對當前 shell 會話有效。如果您希望永久更改資源限制,請編輯 /etc/security/limits.conf
文件并添加相應的規則。
例如,要在 /etc/security/limits.conf
文件中為特定用戶設置資源限制,請添加以下行:
username soft core 0
username hard core 0
username soft nofile 100
username hard nofile 100
username soft nproc 5
username hard nproc 5
將 username
替換為您要限制資源的用戶名。這將限制該用戶的最大文件數為 100,最大進程數為 5。