Python—后台运行(nohup 、&、 2>&1详解)

一、脚本文件(test.py)

1
2
3
4
5
6
7
8
9
10
# -*- coding: UTF-8 -*-
import time
 
print("hello"," python")
os.getcwd()      # 获得当前工作目录
os.chdir(path)   # 改变当前工作路径
 
while True:
    print("start to print %s" % "python")  
    time.sleep(2)

二、运行脚本

1
2
3
4
5
6
7
8
9
10
11
12
[root@localhost ~]# python test.py >my.log
[root@localhost ~]# python test.py >my.log 2>&1
[root@localhost ~]# python test.py 1>my.log                          # 等同于第一条,只是1可以省略不写
[root@localhost ~]# python test.py 1>my.log 2>&1                     # 等同于第二条,只是1可以省略不写
[root@localhost ~]# python test.py 1>my.log 2>error.log              # 等同于第二条,只是把错误输出到error.log文件了
 
 
[root@localhost ~]# python test.py >>my.log                           
[root@localhost ~]# python test.py >>my.log 2>&1
[root@localhost ~]# python test.py 1>>my.log                         # 等同于第一条,只是1可以省略不写
[root@localhost ~]# python test.py 1>>my.log 2>&1                    # 等同于第二条,只是1可以省略不写
[root@localhost ~]# python test.py 1>>my.log 2>>error.log            # 等同于第二条,只是把错误输出到error.log文件了

三、2>&1

  • 0 表示stdin标准输入,用户键盘输入的内容
  • 1 表示stdout标准输出,输出到显示屏的内容
  • 2 表示stderr标准错误,报错内容

2>&1是一个整体,>左右不能有空格,即将错误内容重定向输入到标准输出中去。

2>&1中的&是为了区别文件1和1(标准输出),假如为2>1,就成了将错误内容输出到文件1中,而不是标准输出中。

四、nohup命令

https://blog.csdn.net/suyues/article/details/100046978

https://blog.csdn.net/weixin_42840933/article/details/85780125

posted @   刘_love_田  阅读(7187)  评论(0编辑  收藏  举报
编辑推荐:
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
阅读排行:
· DeepSeek “源神”启动!「GitHub 热点速览」
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
· DeepSeek R1 简明指南:架构、训练、本地部署及硬件要求
· NetPad:一个.NET开源、跨平台的C#编辑器
点击右上角即可分享
微信分享提示