1

I have tried to execute a bash start stop script however I am getting and error

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

I am trying to follow this post shell start / stop for python script but have changed the start command to execute python2.7

/home/shopStart.py

Here is my code:

#!/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
2
  • It seems to be a problem of file locations .. can you show the files locations on your file system in the question? Commented Apr 19, 2016 at 14:23
  • 1
    @MohamedGad-Elrab I took the command and ran it in the ssh session, and it was running fine. I have also done a pwd within the folder where the script resides Commented Apr 19, 2016 at 14:24

1 Answer 1

1

Put python command outside the quotes.

Make nohup "python $script_name" & as:

nohup python "$script_name" &

Otherwise the expansion of "python $script_name" will be treated as the argument file path of nohup.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.