这里写目录标题

  • 1. nohup 不输出日志文件方法
  • 1.1. 场景描述
  • 1.2. 解决思路
  • 1.3. 操作示例
  • 1.4. 关于 Linux 的重定向
  • 2. linux 后台执行命令: & 与 nohup 的用法
  • 2.1. &
  • 2.2. nohup 命令
  • 3. How to Run Linux Commands in the Background
  • 3.1. Add an Ampersand After Your Command
  • 3.2. Use bg to Send Running Commands to the Background
  • 3.3. Send Commands to the Background With nohup
  • 3.4. Run Background Commands Using System Redirects
  • 3.5. Set Linux Commands to the Background Using disown
  • 3.6. Run Linux Commands in the Background Using Tmux
  • 3.7. Leave Your Linux Commands in the Background


1. nohup 不输出日志文件方法

常用命令:

  • 后台运行不输出任何信息: nohup ./test > /dev/null 2>&1 &
  • 后台运行输出日志: nohup ./test > /tmp/1.log 2>&1 &

eg: nohup ./down3.sh > ./3.log 2>&1 &

1.1. 场景描述

nohup 启动程序时, 会在当前的目录下生成 nohup.log 文件。这样就会在很短的时间内将磁盘全部写满(当时的磁盘是 100G, 全部写满), 导致别的程序无法正常运行。(如启动: Jenkins 等)
那么如何让 nohup 命令不产生大量的日志文件呢

1.2. 解决思路

既然 nohup 会输出大量的日志。那如何让 nohup 不输出日志呢?
查了很多的资料发现没办法让 nohup 不输出日志。但是可以 利用 liunx 的黑洞 /dev/null, 它就像一个无底洞, 所有重定向到它的信息都会消失得无影 无踪。这一点非常有用, 当我们不需要回显程序的所有信息时, 就可以将输出重定向到 /dev/null

/dev/null
  • /dev/null: 在类 Unix 系统中, /dev/null, 或称空设备, 是一个特殊的设备文件, 它丢弃一切写入其中的数据(但报告写入操作成功), 读取它则会立即得到一个 EOF。
    在程序员行话, 尤其是 Unix 系统中, /dev/null 被称为位桶 (bit bucket) 或者黑洞 (black hole)。空设备通常被用于丢弃不需要的输出流, 或作为用于输入流的空文件。当你读它的时候, 它会提供无限的空字符 (NULL, ASCII NUL, 0x00)。

1.3. 操作示例

nohup java -cp WEB-INF/lib/*:WEB-INF/classes org.b3log.solo.Starter >/dev/null 2>&1 &
  • >/dev/null 将信息输出到 /dev/null
  • 2>&1 将错误信息重定向到标准输出
  • 最后一个 & 符号, 表示程序在后台运行

1.4. 关于 Linux 的重定向

  • 0: 表示标准输入
  • 1: 标准输出, 在一般使用时, 默认的是标准输出
  • 2: 标准错误信息输出

2. linux 后台执行命令: & 与 nohup 的用法

大家可能有这样的体验: 某个程序运行的时候, 会产生大量的 log, 但实际上我们只想让它跑一下而已, log 暂时不需要或者后面才有需要。所以在这样的情况下, 我们希望程序能够在后台进行, 也就是说, 在终端上我们看不到它所打出的 log。为了实现这个需求, 我们介绍以下几种方法。

我们以下面一个 test 程序来模拟产生大量 log 的程序, 这个程序每隔 1 秒就会打印一句 “Hello world!”:

#include 

int main()
{
    fflush(stdout);
    setvbuf(stdout, NULL, _IONBF, 0);

    while (1) {
        printf("Hello world!\n");
        sleep(1);
    }
}

现在, 我们想要一个清静的世界, 终端上不要有大量的 log 出现, 我们要求 test 程序在后台运行。

2.1. &

这种方法很简单, 就是在命令之后加个 “&” 符号就可以了, 如下:

./test &

这样一来, test 程序就在后台运行了。但是, 这样处理还不够, 因为这样做虽然程序是在后台运行了, 但 log 依然不停的输出到当前终端。因此, 要让终端彻底的清静, 还应将 log 重定向到指定的文件:

./test >> out.txt 2>&1 &

2>&1 是指将标准错误重定向到标准输出, 于是标准错误和标准输出都重定向到指定的 out.txt 文件中, 从此终端彻底清静了。

但是这样做要注意, 如果 Test 程序需要从标准输入接收数据, 它就会在那死等, 不会再往下运行。所以需要从标准输入接收数据, 那这种方法最好不要使用。

那现在程序在后台运行了, 我们怎么找到它呢? 很简单, 有两种方法:

  1. jobs 命令

jobs 命令可以查看当前有多少在后台运行。

jobs -l

此命令可显示所有任务的 PID, jobs 的状态可以是 running, stopped, Terminated。但是如果任务被终止了(kill), shell 从当前的 shell 环境已知的列表中删除任务的进程标识。

  1. ps 命令
ps aux | grep test

2.2. nohup 命令

在命令的末尾加个 & 符号后, 程序可以在后台运行, 但是一旦当前终端关闭(即退出当前帐户), 该程序就会停止运行。那假如说我们想要退出当前终端, 但又想让程序在后台运行, 该如何处理呢?

实际上, 这种需求在现实中很常见, 比如想远程到服务器编译程序, 但网络不稳定, 一旦掉线就编译就中止, 就需要重新开始编译, 很浪费时间。

在这种情况下, 我们就可以使用 nohup 命令。nohup 就是不挂起的意思 ( no hang up)。该命令的一般形式为:

nohup ./test &

如果仅仅如此使用 nohup 命令的话, 程序的输出会默认重定向到一个 nohup.out 文件下。如果我们想要输出到指定文件, 可另外指定输出文件:

nohup ./test > myout.txt 2>&1 &

这样一来, 多管齐下, 既使用了 nohup 命令, 也使用了&符号, 同时把标准输出/错误重定向到指定目录下。

使用了 nohup 之后, 很多人就这样不管了, 其实这样有可能在当前账户非正常退出或者结束的时候, 命令还是自己结束了。所以在使用 nohup 命令后台运行命令之后, 需要使用 exit 正常退出当前账户, 这样才能保证命令一直在后台运行。

3. How to Run Linux Commands in the Background

Linux commands are a great way of interacting with the system using the terminal. However, sometimes it can take a while to finish the task at hand. This forces users to wait for a considerable time or spawn a new shell altogether.

Luckily, you can run Linux commands in the background by following some simple methods. The rest of this article illustrates some of these methods.

3.1. Add an Ampersand After Your Command

The easiest way to run a Linux background command is to add an Ampersand (&) symbol after the command. For example, if you start the gedit text editor from your terminal, you can not use the shell until you close the editor. However, when you add an extra & to your command, you’ll be able to use the shell immediately.

gedit &

3.2. Use bg to Send Running Commands to the Background

Sometimes you run a command only to find out it takes much longer to finish. You can easily send these commands to the background by hitting the Ctrl + Z keys and then using the bg command. Ctrl + Z stops the running process, and bg takes it to the background.

You can view a list of all background tasks by typing jobs in the terminal. Use the fg command to get back to the running task.

3.3. Send Commands to the Background With nohup

The nohup command in Linux allows admins to run terminal commands that are immune to HUP or Hang Up signals. You can run Linux commands in the background using nohup.

The below example runs an Nmap port scan in the background.

nohup sudo nmap -sS --top-ports=15 192.168.1.1/24

One key benefit of nohup is that your commands will run even if you exit the shell. Moreover, it generates log files of the execution. Look for nohup.out in the current directory or inside $HOME.

3.4. Run Background Commands Using System Redirects

You can also run background commands in Linux using system redirects. For example, if you run the below ping command, your shell will run it in the background and immediately give the terminal prompt back.

ping -c5 8.8.8.8 >output.log 2>&1 &

Here the output of the ping command is redirected to the output.log file. You can replace it with /dev/null if you want to discard the result. The 2>&1 tells bash to redirect any errors to the same file. The final & signals bash to run this command in the background.

3.5. Set Linux Commands to the Background Using disown

The disown command in Linux makes it easy to run commands in the background. First, you need to send the task in the background using the & operator. Then, type disown to detach it from your shell.

gedit &

One major advantage of disown is that, like nohup, the system won’t kill your task when you close your shell or log out.

3.6. Run Linux Commands in the Background Using Tmux

Tmux is a powerful multiplexer that allows us to run multiple terminal sessions within a single window. Learning tmux is an excellent choice for people who are unfamiliar with it. Tmux makes running background commands in Linux effortless.

tmux new -d 'ping -c 10 8.8.8.8 > output.log'

When you run the above tmux command, it will execute the ping command in a separate shell and keep it in the background. You can execute any Linux command in the background using this method.

3.7. Leave Your Linux Commands in the Background

Having the ability to run commands in the background makes system management more productive for admins. You can background your tasks in several ways. Bash features like the & and Ctrl + Z are convenient, but the system will kill the background job when the shell closes. On the other hand, tools like nohup and disown keep your command running even when you log out or terminate the shell.

If you leave your programs in the background for a long time, they may become zombie processes if they’re not coded properly. These processes can slow down the system significantly. So, make sure to identify and kill zombie processes every once in a while.