Skip to content

Instantly share code, notes, and snippets.

@richhh7g
Last active February 14, 2024 21:46
Show Gist options
  • Save richhh7g/37ad9d1bb1e5dda668e3b06b97f3c4aa to your computer and use it in GitHub Desktop.
Save richhh7g/37ad9d1bb1e5dda668e3b06b97f3c4aa to your computer and use it in GitHub Desktop.
Shell - Kill process
#!/bin/bash
port=3000
if [ $# -gt 0 ]; then
port=$1
fi
process_pid=$(lsof -i :$port | grep "LISTEN" | awk '{print $2}')
if [ -n "$process_pid" ]; then
echo "Process PID found on port $port: $process_pid"
kill -9 "$process_pid"
echo "Process killed successfully."
else
echo "No process running on port $port."
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment