使用 bash 执行 python 脚本

executing python script using bash

我曾尝试执行 bash 启动停止脚本,但出现错误

nohup: failed to run command `python2.7 /home/shopStart.py': No such file or directory

我正在尝试遵循此 post shell start / stop for python script 但已将启动命令更改为执行 python2.7

/home/shopStart.py

这是我的代码:

#!/bin/bash

script_home="/home"
script_name="$script_home/shopStart.py"
pid_file="$script_home/shoppid.pid"

# returns a boolean and optionally the pid
running() {
    local status=false
    if [[ -f $pid_file ]]; then
        # check to see it corresponds to the running script
        local pid=$(< "$pid_file")
        local cmdline=/proc/$pid/cmdline
        # you may need to adjust the regexp in the grep command
        if [[ -f $cmdline ]] && grep -q "$script_name" $cmdline; then
            status="true $pid"
        fi
    fi
    echo $status
}

start() {
    echo "starting $script_name"
    nohup "python $script_name" &
    echo $! > "$pid_file"
}

stop() {
    # `kill -0 pid` returns successfully if the pid is running, but does not
    # actually kill it.
    kill -0  && kill 
    rm "$pid_file"
    echo "stopped"
}

read running pid < <(running)

case  in 
    start)
        if $running; then
            echo "$script_name is already running with PID $pid"
        else
            start
        fi
        ;;
    stop)
        stop $pid
        ;;
    restart)
        stop $pid
        start
        ;;
    status)
        if $running; then
            echo "$script_name is running with PID $pid"
        else
            echo "$script_name is not running"
        fi
        ;;
    *)  echo "usage: [=13=] <start|stop|restart|status>"
        exit
        ;;
esac

python 命令放在引号外。

nohup "python $script_name" &设为:

nohup python "$script_name" &

否则"python $script_name"的扩展将被视为nohup的参数文件路径。