Linux nohup 命令

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 文件, 文件为空

  1. 修改代码
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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值