nohup(no hang-up)命令在Linux中用于在用户退出登录后继续运行指定的程序或脚本。为了优化nohup命令的资源占用,可以采取以下几种技巧:
nice和renice调整优先级nice -n 10 nohup your_command &
renice 10 -p <PID>
cpulimit -l 50 -p <PID>
sudo cgcreate -g cpu,memory:/mygroup
echo <PID> | sudo tee /sys/fs/cgroup/cpu/mygroup/tasks
echo "50000" | sudo tee /sys/fs/cgroup/cpu/mygroup/cpu.cfs_quota_us
timeout命令timeout 3600 nohup your_command &
nohup.outnohup your_command > output.log 2>&1 &
&后台运行nohup your_command &
top -p <PID>
htop -p <PID>
vmstat 1
systemd服务[Unit]
Description=My Service
[Service]
ExecStart=/path/to/your_command
Restart=always
User=your_user
[Install]
WantedBy=multi-user.target
然后启用并启动服务:sudo systemctl enable my_service
sudo systemctl start my_service
crontab -e
# 添加以下行,每小时重启一次服务
0 * * * * /bin/systemctl restart my_service
通过以上技巧,可以有效地优化nohup命令的资源占用,确保程序在后台稳定运行。