Linuxnohup 命令

it2026-01-23  2

nohup ( no hang up(不挂起))

用于在系统后台不挂断地运行命令,退出终端不会影响程序的运行

nohup Command [ Arg … ] [ & ] C o m m a n d Command Command:要执行的命令。 A r g Arg Arg:一些参数,可以指定输出文件。 & \& &:让命令在后台执行,终端退出后命令仍旧执行。

无论是否将 nohup 命令的输出重定向到终端,输出都将附加到当前目录的 nohup.out 文件中。

示例

1. 编写一段 p y t h o n python python 代码:

# 向文件中写入0-9 File = "/home/xx/Dataset/New/test" with open(File, 'a') as FileWrite: for i in range(0,10): FileWrite.write(i)

2. 使用 n o h u p nohup nohup 命令

nohup python /home/xx/Projects/PycharmProjects/Test/test1.py >(日志路径)

输出:

>> nohup: ignoring input and appending output to 'nohup.out'

在默认情况下(非重定向时),会输出一个名叫 n o h u p . o u t nohup.out nohup.out 的文件到当前目录下,如果当前目录的 n o h u p . o u t nohup.out nohup.out 文件不可写,输出重定向到 $ H O M E / n o h u p . o u t HOME/nohup.out HOME/nohup.out 文件中。

打开 n o h u p . o u t nohup.out nohup.out 文件,可以看到日志中记录了程序运行时出现的错误信息。

Traceback (most recent call last): File "/home/xx/Projects/PycharmProjects/Test/test1.py", line 5, in <module> InsWrite.write(i) TypeError: write() argument must be str, not int

打开 t e s t test test 文件, 文件为空

修改代码 File = "/home/xx/Dataset/New/test" with open(File, 'a') as FileWrite: for i in range(0,10): FileWrite.write(str(i))

打开 n o h u p . o u t nohup.out nohup.out 文件, 文件为空 打开 t e s t test test 文件,成功写入

最新回复(0)