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

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/fly910905/article/details/90739559

nohup grammar

Do not hang up the run command. Abbreviations no hangup, which means "do not hang up."

nohup Command [ Arg ... ] [ & ]

  • nohup runs a command specified by the Command parameter and any related Arg parameters of command, ignore all hang (SIGHUP) signals .
  • nohup command in the beginning, said they did not hang (no hang up), that is, off use with closed end or exit an account, the process also continues to run, usually with an ampersand. As nohup command &.

nohup output

If you do not redirect the output, the output will be appended to the command nohup nohup.out current directory files.

If nohup.out not write files in the current directory, output is redirected to $ HOME / nohup.out file.

Output basic grammar

  • / Dev / null device represents an empty file
  • 0 indicates the standard input stdin
  • 1 represents a standard output stdout
  • 2 represents standard error stderr
  • > File represents the standard output to file, it is equivalent to. 1> file
  • 2> error output indicates an error to the error file
  • 2> & 1 also shows a standard output is redirected to an error
  • 2> & 1> file: Error output to the terminal, the standard output is redirected to a file File, equal to > file 2> & 1 (standard output to a file, output redirected to the standard error) .
  • & In the command to the end, represent the background , to prevent the terminal has been occupied by a process , so that the terminal can not perform the task, with the> file 2> & 1 can save the log to a file, but if the terminal is closed, the process also stops running. The command> file.log 2> & 1 & .

nohup practical application

Console log output to the current directory

#!/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  &

No output console log

#!/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 &

If you want to output to the specified directory,

Just to / dev / null, replacing the specified directory path ~

Empty nohup

  • By non-stop service to clear the log nohup

cp /dev/null nohup.out

cat /dev/null > nohup.out

echo "" > nohup.out

Guess you like

Origin blog.csdn.net/fly910905/article/details/90739559