【问题标题】:executing python script using bash使用 bash 执行 python 脚本
【发布时间】:2023-04-03 03:18:02
【问题描述】:

我试图执行一个 bash 启动停止脚本,但是我得到了错误

nohup: 运行命令 `python2.7 失败
/home/shopStart.py': 没有这样的文件或目录

我正在尝试关注此帖子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 $1 && kill $1
    rm "$pid_file"
    echo "stopped"
}

read running pid < <(running)

case $1 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: $0 <start|stop|restart|status>"
        exit
        ;;
esac

【问题讨论】:

  • 这似乎是文件位置的问题..你能在问题中显示文件系统上的文件位置吗?
  • @MohamedGad-Elrab 我接受了命令并在 ssh 会话中运行它,它运行良好。我还在脚本所在的文件夹中做了一个 pwd

标签:
bash
python-2.7
shell
startup