Question :- How I can kill the running process ID, when ever I need to kill a background process I do ps -e | grep “process_name” and this command prints something like this “4605 ? 11:18:29 mysqld” and then I kill the process id with “kill -9 4605” So is there are any simple way to simplify this and make it quicker reducing the amount of typing?
Answer :- Yes, you can do this in simple way. Many Linux users and admin uses pgrep to kill the process id, if you run the pgrep command such as “pgrep mysqld” it will print the all process id associated with MySQL and if you want to kill all MySQL process id you can simply use the “pkill mysql” command. There are lots of way to do this.
Find The Process ID And Kill It
You can also use below command to kill the process id using grep command:
ps -eaf | grep "process_name | grep -v grep | awk '{ print $2 }' | xargs kill -9
You can use one line command and reuse it form the history of your bash, or better create an alias for it .
You can also create a bash script and set it to execute on specific time.
# mkdir /root/script # cd /root/script/ # vim kill_process.sh #!/bin/bash PID=`ps -eaf | grep | grep -v grep | awk '{print $2}'` if [[ "" != "$PID" ]]; then echo "killing $PID" kill -9 $PID fi
Save and close the file and make it executable.
# chmod +x /root/script/kill_process.sh
Now if you want to run this script at specific time make a cron entry like below:
# crontab -e ## Kill Process ID */5 * * * * /root/script/kill_process.sh > /dev/null/
Save cron file using :wq! . In above cron I scheduled “kill_process.sh” to execute at every 5 minutes.
Suggested Read:
- What is Process ID or PID in Linux?
- List Of All Running Process In Linux/UNIX
- How To Find Top 10 CPU Consuming Process In Linux/Ubuntu
- How To Run Process Or Program On Specific CPU Cores In Linux
Thanks:)
If you find this tutorial helpful please share with your friends to keep it alive. For more helpful topic browse my website www.looklinux.com. To become an author at LookLinux Submit Article. Stay connected to Facebook.
Leave a Comment