【神奇的 Linux】启动后台保持运行的进程

开始

文件描述符

  • 0 stdin 标准输入
  • 1 stdout 标准输出
  • 2 stderr 标准错误

重定向

echo "test" > temp.txt  # > 会覆盖已存在的文件
echo "test" >> temp.txt  # >> 会以累加的方式输出到文件
python hello.py < input.in  # 重定向到输入
python hello.py > output.out # 重定向到输出
python hello.py 2> error.err  # 重定向到错误输出
python hello.py 2>stderr.txt  1>stdout.txt # 输出分离 
python hello.py > output.txt 2>&1  # 输出合并 
python hello.py 2 > /dev/null  # 任何数据都会被丢弃到垃圾桶(位桶/黑洞)
python hello.py >/dev/null 2>&1  # 标准输出和标准错误 

进入正题

单一个& 是不够的,因为是终端启动的进程,那么该进程的父进程就是终端,所以终端关闭,子进程随着关闭。所以要用到 nohup 将进程的父进程设置为1的 init 进程 。

nohup python hello.py > out.log 2>&1 &

雾霾天 等风来

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容