运行python脚本的Bash脚本

2024-04-27 02:24:33 发布

您现在位置:Python中文网/ 问答频道 /正文

我刚接触bash脚本,从stackoverflow中找到了一个代码。我把它和我的脚本合并,它不运行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

这是我的代码:-在

^{pr2}$

Tags: 代码py脚本bashauto情况rootstackoverflow
3条回答

@NickyMan,问题与shell脚本中的逻辑有关。你的程序找不到runserver,总是说“Good”。 在这段代码中,如果找不到服务器,那么execrunserver。在

#!/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

如果您更熟悉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 &")

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

#!/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

相关问题 更多 >