Shell |
您所在的位置:网站首页 › shell黑洞文件 › Shell |
前言
Linux中常用到,nohup command>文件路径 2>&1 &,来后台执行命令。 /dev/null 表示空设备文件,/dev/null 就像是一个回收站,任何写入该文件的内容都会被清除,用来清除打印。0 表示stdin标准输入1 表示stdout标准输出2 表示stderr标准错误 nohup始终不停的运行命令,即使在你的用户账户已经退出的情况下。如果需要在后台永久的运行命令采用:nohup + ... (Commnd) + & 值得注意的是&:在后台运行命令,账户退出后,命令自动跟着退出。 编写测试用例写入结果到指定文件 第一条语句a_error_command_test是一条错误的指令,date是可执行指令,返回当前时间。 vim nohup.sh内容: a_error_command_test date >现在我们输出结果到nohup_sh.log文件中(如果输出文件不存在将会自己创建): 上面执行了两遍命令可知:>是覆盖写入。 >>由形象便可知:>>相对于>多了个>,即为追加的意思 sh nohup.sh >> nohup_sh.log cat nohup_sh.lognohup command>/dev/null 2>&1 & /dev/null 表示空设备文件0 表示stdin标准输入1 表示stdout标准输出2 表示stderr标准错误上面我们的>写入其实相对于是 sh nohup.sh > nohup_sh.log 1>&1好理解的:2比1多一个错误重定向输出 sh nohup.sh > nohup_sh.log 2>&1在后台运行命令,账户退出后,命令自动跟着退出。 重新编写nohup.sh脚本: #! /bin/sh while(true) do echo "hello world!" date sleep 6 done如果不加&后台运行,将在前台一直输出,查看输出文件: sh nohup.sh >> nohup_sh.log & tail -f nohup_sh.log但是,断开shell连接会话会停止该进程,重连后查看日志: 即使用户退出也永久的执行命令。 nohup + command & nohup sh nohup.sh >> nohup_sh.log 2>&1 & tail -f nohup_sh.log 2021-1-6java -cp test.jar com.xxx.xxx 2>> test.txt & 将xxx中的System.err.out的输出内容重定向到test.txt文件,这样做的目的:只打印出system输出内容,筛选掉kafka或springboot在控制台打印的信息。 2022-11-16nohup + &打印日志自动清理: while :; do echo "" > provider.log echo "" > consumer.log echo "==========================================================================================" echo $(date +%F%n%T) 'log clear end' sleep 1d done #nohup sh clear.sh >> clear.log 2>&1 &😘如果对你有所帮助点个赞,给点小动力,谢谢啦~ |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |