【问题标题】:Bash script running python scripts运行 python 脚本的 Bash 脚本
【发布时间】:2023-04-14 15:05:01
【问题描述】:

我是 bash 脚本的新手,从 * 中找到了其中一个代码。我将它与我的脚本合并,它不运行 python。当我尝试回应时,它总是会“好”。当我尝试运行ps -ef | grep runserver* 时,它总是出现并导致python 脚本无法运行。

root      1133  0.0  0.4  11988  2112 pts/0    S+   02:58   0:00 grep --color=auto runserver.py

这是我的代码:-

#!/bin/sh
SERVICE='runserver*'

if ps ax | grep -v grep | grep $SERVICE > /dev/null
then
    python /var/www/html/rest/runserver.py
    python /var/www/html/rest2/runserver.py
else
    echo "Good"
fi

【问题讨论】:

  • 这有帮助吗? *.com/questions/4377109/…
  • @HaifengZhang 我用了那个例子,但它不起作用......这就是为什么我要求另一个解决方案
  • @Aison 如果条件为真,我可以运行我的 python 脚本,但现在它总是转到 else 条件
  • 能否提供ps ax | grep -v grep | grep runserver的输出?

标签: python linux bash shell


【解决方案1】:

如果你更熟悉 python,试试这个吧:

#!/usr/bin/python

import os
import sys

process = os.popen("ps aux | grep -v grep | grep WHATEVER").read().splitlines()

if len(process) == 2:
  print "WHATEVER is running - nothing to do"
else:
  os.system("WHATEVER &")

【讨论】:

    【解决方案2】:

    下面的代码是你想要的吗?

    #!/bin/sh
    SERVER='runserver*'
    CC=`ps ax|grep -v grep|grep "$SERVER"`
    if [ "$CC" = "" ]; then
        python /var/www/html/rest/runserver.py
        python /var/www/html/rest2/runserver.py
    else
        echo "good"
    fi
    

    【讨论】:

      【解决方案3】:

      @NickyMan,问题与 shell 脚本中的逻辑有关。您的程序找不到 runserver 并且总是说“好”。 在这段代码中,如果没有找到服务器,则执行 runserver.

      #!/bin/sh
      SERVICE='runserver*'
      
      if ps ax | grep -v grep | grep $SERVICE > /dev/null
      then
          echo "Good"
      else
          python /var/www/html/rest/runserver.py
          python /var/www/html/rest2/runserver.py
      fi
      

      【讨论】:

        最近更新 更多