How to send control+c from a bash script?

前端 未结 5 1673
孤街浪徒
孤街浪徒 2020-12-02 11:05

I\'m starting a number of screens in a bash script, then running django\'s runserver command in each of them. I\'d like to be able to programmatically stop them

5条回答
  •  温柔的废话
    2020-12-02 11:40

    Ctrl+C sends a SIGINT signal.

    kill -INT sends a SIGINT signal too:

    # Terminates the program (like Ctrl+C)
    kill -INT 888
    # Force kill
    kill -9 888
    

    Assuming 888 is your process ID.


    Note that kill 888 sends a SIGTERM signal, which is slightly different, but will also ask for the program to stop. So if you know what you are doing (no handler bound to SIGINT in the program), a simple kill is enough.

    To get the PID of the last command launched in your script, use $! :

    # Launch script in background
    ./my_script.sh &
    # Get its PID
    PID=$!
    # Wait for 2 seconds
    sleep 2
    # Kill it
    kill $PID
    

提交回复
热议问题