【问题标题】:Run python script on boot-up CentOS在启动 CentOS 时运行 python 脚本
【发布时间】:2018-08-29 15:40:28
【问题描述】:

尝试在 CentOS 启动时运行 python 脚本。我的 /etc/init.d/example-script 看起来像这样:

case $1 in 
    start)
        $(python /etc/bin/example-script.py &)
    ;;
    stop)
        echo "filler text"
    ;;

当我尝试service example-script start 时,它会运行脚本,但不会在后台执行。 example-script.py 有一个 while(True) 循环,所以也许这就是为什么?

但是当我在终端中输入python /etc/bin/example-script.py & 时,它确实在后台执行。仅当此命令在 bash 脚本中时才会失败。

【问题讨论】:

  • 这可能更像是一个 CentOS 管理员问题,而不是 Python 编程问题,因此您可能应该在适合该问题的 SE 站点上询问。 (另外,我怀疑您需要指定 CentOS 的哪个版本。)

标签: python bash centos


【解决方案1】:

那是因为您通过将命令放入命令替换语法 ($()) 中生成了一个子shell,即命令替换在子shell 中运行命令;并且您将命令置于该子 shell 中,而不是原始脚本的 shell。

此外,您没有对 STDOUT 做任何事情,因此邀请另一个子 shell 似乎毫无意义。删除命令替换:

python /etc/bin/example-script.py &

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-02-16
    • 1970-01-01
    • 2014-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-24
    • 2018-11-07
    相关资源
    最近更新 更多