OS

pid로 실행 및 종료하는 스크립트 만들기

바리새인 2025. 3. 27. 19:33

실행

#!/bin/bash

# Run your application in the background and save the PID
your_app_command &

# Save the PID to a file
echo $! > app.pid

echo "Application started with PID $(cat app.pid)"

종료

#!/bin/bash

# Check if the pid file exists
if [ -f app.pid ]; then
    PID=$(cat app.pid)

    # Kill the process
    kill $PID

    # Optionally remove the pid file
    rm app.pid

    echo "Application with PID $PID stopped."
else
    echo "PID file not found. Application may not be running."
fi