python - 将 nohup 重定向到脚本上的标准输出
问题描述
我有一个.sh
通过nohup
运行大量 Python 脚本的脚本。
我习惯nohup
在后台运行它们,并避免在出现“ nohup:忽略输入并将输出附加到'nohup.out' ”消息时进行必要的输入。
nohup python script2.py 2> /dev/null &
nohup python script2.py 2> /dev/null &
[...]
当我运行这个.sh
时,我得到两个 Python 脚本在后台运行,好的。
这个 Python 脚本生成一个日志文件,我将其派生到std.stdout
:
stream_config = logging.StreamHandler(sys.stdout) <-- here
stream_config.setFormatter(formatter)
importer_logger.addHandler(stream_config)
由于我的实施,我想启动那些nohup
但将标准输出发送到 PID 1
我可以很容易地做到这一点,而无需使用nohup
如下:
python tester.py > /proc/1/fd/1
但是我怎样才能将“不要按回车继续”和“将文件导出到标准输出”结合起来呢?
我没有运气尝试了这些:
nohup python tester.py > /proc/1/fd/1 2> /dev/null &
nohup python tester.py 2> /proc/1/fd/1 &
提前致谢。
解决方案
您可以通过使用 shell 间接启动您的程序来修复您的命令,以便您可以重定向其输出而不是 nohup 的输出:
nohup bash -c "exec python tester.py > /proc/1/fd/1" 2> /dev/null &
这不是最佳选择,但它至少纠正了导致您的尝试失败的问题。
推荐阅读
- c# - 如何使用 .NET Core 3.0 在 App-Service 中启用证书身份验证?
- python-3.x - 来自具有不同列名和行大小但几乎没有重合值的两个数据帧的结果数据帧,并行呈现(Python)
- java - isRequestedSessionIdValid 登录成功后返回 false
- angular - Angular 在插值 {{function()}} 中使用函数是什么意思,这是一个好习惯吗
- swift - Swift - 预计解码数组
但找到了一本字典 - ubuntu - 恶意软件一次又一次地自主安装
- java - SortedList 像 SortedSet 一样保持顺序,但也允许重复元素
- c# - Unity:无法引用命名空间(甚至无法引用不同脚本中的方法)
- javascript - 如何从 subscribe() 函数分配全局变量并更新视图
- python - 从列表中读取文件名和函数名的不同文件调用函数