代码编织梦想

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

常用命令:

  • 后台运行不输出任何信息: nohup ./test > /dev/null 2>&1 &
  • 后台运行输出日志: nohup ./test > /tmp/1.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.

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/wan212000/article/details/128006307

nohup 不输出日志-爱代码爱编程

关键在于最后的 >/dev/null 2>&1 部分,/dev/null是一个虚拟的空设备(类似物理中的黑洞),任何输出信息被重定向到该设备后,将会石沉大海 >/dev/null 表示将标准输出信息重定向到"黑洞" 2>&1 表示将标准错误重定向到标准输出(由于标准输出已经定向到“黑洞”了,即:标准输出此时也是"

nohup不输出nohup.out日志信息,已解决。_柒的意志的博客-爱代码爱编程

最近项目中使用的springboot打的jar包,放到服务器上跑,为了防止关闭终端窗口导致程序挂掉,采用nohup和&组合命令来操作 命令如下 : nohup java -jar test.jar & 但是这种方式启动项目会默认生成一个nohup.out的文件来记录日志,而且这个文件太占磁盘内存了,几天下来发现这个文件大小有好几个G,

java nohup不输出日志的方法-爱代码爱编程

nohup java -jar springboot.jar --server.port=80 >/dev/null 2>&1 & 拓展: 0、1和2分别表示标准输入、标准输出和标准错误信息输出,可以用来指定需要重定向的标准输入或输出。 如果想要正常输出和错误信息都不显示,则要把标准输出和标准错误都重定向到/dev/nul

linux nohup不生成日志,linux重定向及nohup不输出的方法-爱代码爱编程

FreeBSD可以同时运行多个进程,在shell下直接输入命令后,shell将进程放到前台执行。如果要将进程放到后台执行,需要在命令行的结尾加上一个 “&” 符号。下面的命令从后台执行,从ftp.isc.org下载文件。 $ fetch ftp://ftp.isc.org/pub/inn/inn-1.7.2.tar.gz & 当程序

linux nohup不生成日志,nohup不输出日志信息的方法,及linux重定向学习-爱代码爱编程

起因 最近使用nohup创建了一个后台进程,默认日志输出到了nohup.out文件中,程序跑起来也就没再管,过了大约一周,发现硬盘空间不够了,于是查找原因,发现这个nohup.out文件已经到了70G了,导致硬盘空间不足了。 解决方案 只输出错误信息到日志文件 nohup ./program >/dev/null 2>log &am

Linux nohup命令不再默认输出日志文件-爱代码爱编程

Linux使用 nohup命令启动程序后,会打印大量的日志,很快运维同事收到磁盘已满的报警短信通知。 1、解决方案 1、只记录异常日志 # nohup python -u Job.py >/dev/null 2>error.log  2>&1 & 2、不记录任何日志 # nohup python -u Job.p

nohup输出日志文件-爱代码爱编程

1 前言 1 代表stdout 标准输出 2 代表stderr 标准错误 1.1 标准输出 1.1.1 标准输出,日志文件覆盖 command > output.txt command 1> output.txt 以上两句等价。将命令的stdout重定向至文件,stdout将不会出现在终端。 如果文件已经存在,将会覆盖。 1.1

nohup命令不产生nohup.log日志文件-爱代码爱编程

背景 用jar包方式启动springboot项目一开始使用nohup java -jar xxx.jar &方式后台启动,但是会产生nohup.log日志。时间长了nohup.log日志越来越大,而且本身配置了logback滚动日志,不需要nohup.log日志。最终使用如下命令解决。 解决方法 nohup java -jar -Xms102

还在付费使用 xshell?我选择这款超牛逼的 ssh 客户端,完全免费_写代码的珏秒秒的博客-爱代码爱编程

分享过FinallShell这款SSH客户端,也是xiaoz目前常用的SSH客户端工具,FinalShell使用起来方便顺手,但令我不爽的是tab数量变多的时候FinalShell越来越卡,而且内存占用也比较高。 最近发现一款使用使用C语言开发的跨平台SSH客户端WindTerm,完全免费用于商业和非商业用途,没有限制。 所有发布的源代码(第三方目录除外

ubuntu 20.04.5安装无线网卡rtl8821ce驱动_晨之清风的博客-爱代码爱编程

陈拓 2022/11/26-2022/11/26 准备工作 查看网络设备   WiFi设备未驱动。 查看WiFi设备型号 lspci | grep -i wireless 或 lspci -nn | grep -i net 设备代号10ec:c821可以用来查询验证WiFi设备型号。 在网站PCI devices查询无线设备型号:

ceph对象存储_桂安俊@kylinos的博客-爱代码爱编程

目录 一、环境准备 二、什么是对象存储 三、部署对象存储服务 1、启动RGW服务 2、更改RGW服务端口 3、客户端测试 一、环境准备 Ceph集群搭建参照:Ceph集群部署_桂安俊@kylinOS的博客-CSDN博客 以下Ceph存储实验环境均基于上述Ceph集群环境搭建。 二、什么是对象存储 1、对象存储 也就是键值存