nohup java -jar app.jar > /dev/null 2>&1 & 解释一下这个命令
时间: 2023-10-26 10:49:28 浏览: 177
&1 &
This command runs a Java application in the background using the nohup command. The output of the application is redirected to /dev/null, which discards it. The 2> redirects any error messages to the same location as the output. The &1 & at the end of the command puts the process in the background and detaches it from the terminal, allowing it to continue running even if the terminal is closed.
相关问题
nohup java -jar app.jar > /dev/null 2>&1 &
&1 &
This command starts a Java application in the background, redirects standard output to /dev/null (which discards it), redirects standard error to standard output, and detaches the process from the terminal by using nohup and &. This allows the application to continue running even if the terminal is closed, and prevents it from being terminated by the HANGUP (SIGHUP) signal.
nohup java -jar /optapp.jar>/optlatest.log 2>&1 &
### 启动 Java JAR 文件并重定向输出到日志文件
在 Linux 系统中,可以使用 `nohup` 命令以后台进程的方式运行 Java 应用程序,并通过标准输出和错误输出的重定向功能将日志写入指定的日志文件。以下是具体的命令格式:
```bash
nohup java -jar your_application.jar > /path/to/output.log 2>&1 &
```
#### 命令解析:
- `nohup`: 表示 "no hang up",用于在终端关闭时保持进程继续运行。
- `java -jar your_application.jar`: 运行 Java JAR 文件的标准命令。
- `> /path/to/output.log`: 将标准输出重定向到 `/path/to/output.log` 文件中。如果文件不存在,则会自动创建;如果文件已存在,则内容会被覆盖。
- `2>&1`: 将标准错误(文件描述符为 2)重定向到与标准输出(文件描述符为 1)相同的位置,在此情况下是 `output.log` 文件。
- `&`: 在后台启动该进程。
如果需要将日志追加到现有的日志文件而不是覆盖它,可以使用 `>>` 替代 `>`:
```bash
nohup java -jar your_application.jar >> /path/to/output.log 2>&1 &
```
#### 示例:
假设当前目录下有一个名为 `app.jar` 的 Java 程序,并希望将其日志输出到 `/var/log/app.log` 文件中,可以执行以下命令:
```bash
nohup java -jar app.jar > /var/log/app.log 2>&1 &
```
#### 注意事项:
1. **默认日志输出**:如果不进行重定向,默认情况下,`nohup` 会将所有输出写入当前目录下的 `nohup.out` 文件中 [^3]。
2. **丢弃日志输出**:如果不需要任何日志输出,可以将标准输出和错误输出重定向到 `/dev/null`,这样所有的输出都会被丢弃:
```bash
nohup java -jar your_application.jar > /dev/null 2>&1 &
```
此方法适用于不需要调试或查看日志的情况 [^5]。
3. **查看运行中的进程**:可以通过 `ps -ef | grep java` 命令查看当前运行的 Java 进程。
4. **停止后台进程**:找到进程 ID (PID) 后,使用 `kill -9 PID` 命令终止进程 [^3]。
###
阅读全文
相关推荐
















