nohup是Linux系统中一个非常有用的命令,它可以让进程在用户退出登录后继续运行,非常适合长时间运行的大规模数据处理任务。
nohup command [arguments] > output.log 2>&1 &
nohup
- 使进程忽略挂起信号(SIGHUP)command
- 要执行的命令或脚本> output.log
- 将标准输出重定向到文件2>&1
- 将标准错误重定向到标准输出&
- 在后台运行nohup nice -n 19 your_processing_script.sh > output.log 2>&1 &
nice
调整进程优先级,避免影响系统其他关键服务ulimit
设置适当的资源限制nohup your_script.sh > /path/to/large/disk/output_$(date +%Y%m%d).log 2> error.log &
screen -S data_processing
nohup your_script.sh > output.log 2>&1 &
# 按Ctrl+A然后D退出screen会话
nohup your_script.sh && \
curl -X POST -H 'Content-type: application/json' \
--data '{"text":"数据处理完成"}' \
https://hooks.slack.com/services/XXX > output.log 2>&1 &
#!/bin/bash
# 设置资源限制
ulimit -n 65536
# 记录开始时间
echo "处理开始于: $(date)" >> progress.log
# 主处理逻辑
for file in /data/input/*.csv; do
echo "处理文件: $file - $(date)" >> progress.log
process_file "$file" >> results_$(date +%Y%m%d).csv 2>> errors.log
done
# 记录结束时间
echo "处理完成于: $(date)" >> progress.log
然后使用nohup运行:
nohup ./data_processor.sh > main.log 2>&1 &
ps aux | grep your_script
tail -f output.log
kill -9 <PID>
通过合理使用nohup命令,可以有效地在Linux系统中执行长时间运行的大规模数据处理任务,而无需保持终端连接。