python - Shebang 使用 nohup 启动 Python 脚本

标签 python bash shebang

我觉得写起来很长:

nohup python -u myscript.py > log.txt 2>&1 &

每次我想启动一个 Python 脚本作为后台进程。

是否有可能像 shebang 这样的第一行脚本:

#!nohup python -u myscript.py > log.txt 2>&1 &
import time
while True:
    print 'hello'
    time.sleep(1)

像这样的命令

run myscript.py

是否会使用 myscript.py 第一行中的命令自动启动脚本

注意:我正在寻找一个单文件解决方案,我不想在 myscript.py 旁边有第二个文件 myscript.sh bash 脚本 来完成这项工作。

最佳答案

恐怕这不是直接可能的,但你可以像这样克服它:

在某处(可能在/home/basj/bin/中)创建文件 run_py.sh 并使其可运行:

#!/bin/bash
nohup python -u "$@" > log.txt 2>&1 &

然后在你的 python 文件中包含这个头文件(并使它们可运行)

#!/home/basj/bin/run_py.sh
print "I am in python on background with nohup now :)"

关于python - Shebang 使用 nohup 启动 Python 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34696662/

相关文章:

python - 无论设置为 True,settings.DEBUG 仍然为 false

python - 让 Pandas NaT 像 NaN 一样传播

python - 集合真的比列表快吗?

bash - 如何将文件内容用作变量但保留 `\0`

未知路径的shell脚本shebang

r - 如何使我的 R 脚本可执行?

python - 使用 tweepy 导入错误

bash - sed - 打印字符串第 n 次出现之前的所有行

控制台输出未对齐

shell - -f 在解释器定义中是什么意思 : ! #/bin/awk -f