shell start / stop for python script

后端 未结 5 1071
青春惊慌失措
青春惊慌失措 2020-12-20 23:42

I have a simple python script i need to start and stop and i need to use a start.sh and stop.sh script to do it.

I have start.sh:

#!/bin/sh

scri         


        
相关标签:
5条回答
  • 2020-12-21 00:08

    I don't have a unix box on at the moment, so i can't test this, but it should be fairly simple to get the idea.

    start.sh:

    if [ -e ./temp ]
    then
      pid=`cat temp`
      echo "Process already exists; $pid"
    else
      script='/path/to/my/script.py'
      echo 'starting $script with nohup'
      nohup /usr/bin/python $script &
      echo $! > temp
    fi
    

    stop.sh:

    if [ -e ./temp ]
    then
      pid=`cat temp`
      echo "killing $pid"
      kill -15 $PID
      rm temp
    else
      echo "Process not started"
    fi
    

    Try this out.

    0 讨论(0)
  • 2020-12-21 00:12

    The "correct" approach would probably be to have your script write its pid to a file in /var/run, and clear it out when you kill the script. If changing the script is not an option, have a look at start-stop-daemon.

    If you want to continue with the grep-like approach, have a look at proctools. They're built in on most GNU/Linux machines and readily available on BSD including OS X:

    pkill -f /path/to/my/script.py
    
    0 讨论(0)
  • 2020-12-21 00:13

    ps aux | grep "/path/to/my/script.py"

    will return both the pid for the instance of script.py and also for this instance of grep. That'll probably be why you're getting a no such process: by the time you get around to killing the grep, it's already dead.

    0 讨论(0)
  • 2020-12-21 00:18

    init-type scripts are useful for this. This is very similar to one I use. You store the pid in a file, and when you want to check if it's running, look into the /proc filesystem.

    #!/bin/bash
    
    script_home=/path/to/my
    script_name="$script_home/script.py"
    pid_file="$script_home/script.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 "$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
    
    0 讨论(0)
  • 2020-12-21 00:30

    It is because ps aux |grep SOMETHING also finds the grep SOMETHING process, because SOMETHING matches. After the execution the grep is finished, so it cannot find it.

    Add a line: ps aux | grep -v grep | grep YOURSCRIPT

    Where -v means exclude. More in man grep.

    0 讨论(0)
提交回复
热议问题