Linux:nohup、&、 2>&1、/dev/null

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/fly910905/article/details/90739559

nohup语法

不挂断地运行命令。no hangup的缩写,意即“不挂断”。

nohup Command [ Arg ... ] [ & ]

  • nohup 命令运行由 Command参数和任何相关的 Arg参数指定的命令,忽略所有挂断(SIGHUP)信号
  • nohup放在命令的开头,表示不挂起(no hang up),也即,关闭终端或者退出某个账号,进程也继续保持运行状态,一般配合&符号一起使用。如nohup command &。

nohup 输出

如果不将 nohup 命令的输出重定向,输出将附加到当前目录的 nohup.out 文件中。

如果当前目录的 nohup.out 文件不可写,输出重定向到 $HOME/nohup.out 文件中。

输出基本语法

  • /dev/null 表示空设备文件
  • 0 表示stdin标准输入
  • 1 表示stdout标准输出
  • 2 表示stderr标准错误
  • > file 表示将标准输出输出到file中,也就相当于 1>file
  • 2> error 表示将错误输出到error文件中
  • 2>&1 也就表示将错误重定向到标准输出上
  • 2>&1 >file :错误输出到终端,标准输出重定向到文件file,等于 > file 2>&1(标准输出重定向到文件,错误重定向到标准输出)
  • & 放在命令到结尾,表示后台运行防止终端一直被某个进程占用,这样终端可以执行别到任务,配合 >file 2>&1可以将log保存到某个文件中,但如果终端关闭,则进程也停止运行。如 command > file.log 2>&1 & 。

nohup 实际应用

控制台日志,输出到当前目录下

#!/bin/sh



APP1_NAME=cms-1.0.1-beta

tpid1=`ps -ef|grep $APP1_NAME|grep -v grep|grep -v kill|awk '{print $2}'`




if [ $tpid1 ]; then
kill -9 $tpid1
fi
# 控制台日志,输出到当前目录下
nohup java -jar $APP1_NAME.jar --spring.profiles.active=pro  &

不输出控制台日志

#!/bin/sh


APP1_NAME=cms-1.0.1-beta

tpid1=`ps -ef|grep $APP1_NAME|grep -v grep|grep -v kill|awk '{print $2}'`



if [ $tpid1 ]; then
kill -9 $tpid1
fi
#不输出控制台日志
nohup java -jar $APP1_NAME.jar --spring.profiles.active=pro  >/dev/null 2>&1 &

如果想要输出到指定目录下,

只需要将/dev/null,替换为指定目录的路径~

nohup 清空

  • 通过不停服务清空nohup日志

cp /dev/null nohup.out

cat /dev/null > nohup.out

echo "" > nohup.out

猜你喜欢

转载自blog.csdn.net/fly910905/article/details/90739559