在 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。