Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Here is my script, please run it, and ask ask it to kill a harmless process.
Script:
Quote:
#!/bin/bash
# The Following line kills the process given by the user
WHO="$(whoami)"
PROCESS_NAME="$1"
PROCESS_NUMBER="$(ps aux | grep "$PROCESS_NAME" | grep -v grep | awk '{print $2}')"
echo "Trying to kill Process $PROCESS_NAME"
echo -e "Trying to kill Process# $PROCESS_NUMBER\c\c\c"
#Check if the user is Root:
if test $WHO = root; then
#kill "$PROCESS_NUMBER"
echo "Sent the KILL command to Process# $PROCESS_NUMBER"
else
echo "Only root can run this script"
#echo $WHO
exit 0
fi
# End of Script
When I run it:
Code:
root@edslaptop:/home/ed# killname kuickshow
Trying to kill Process kuickshow
Trying to kill Process# 10960
11765
11767
11770Sent the KILL command to Process# 10960
11765
11767
11770
root@edslaptop:/home/ed#
Process # 10960 is correct, but what are the other two that have found
their way into $PROCESS_NUMBER?
They are always separated by 2 (i.e. 11765 = 11767 + 2). I don't know where these
numbers are from - I don't think they are process numbers!
They very well could be process numbers. Try doing a "ps -e | grep processname" and see if multiple results show up. While the names may not be exact matches, the string "processname" may show up in a few processes. You can use sed to keep only the first entry if that's what you want, or something more appropriate to search through the results for exactly what you want.
ed@edslaptop:~$ killname kuickshow
Trying to kill Process kuickshow
Trying to kill Process# 10960
12358
12360
12363Only root can run this script
ed@edslaptop:~$ ps -e | grep processname
ed@edslaptop:~$
How would I just cut $PROCESS_NAME down to its first line?
#!/bin/bash
# The following line kills the process given by the user
WHO="$(whoami)"
PROCESS_NAME="$1"
PROCESS_NUMBER="$(ps -e | grep $PROCESS_NAME | sed -e '2,$d' | sed -e's/ //g' | sed -e 's/[^0-9].*//')"
echo "Trying to kill Process $PROCESS_NAME"
echo -e "Trying to kill Process# $PROCESS_NUMBER\n"
#Check if the user is Root:
if test $WHO = root; then
#kill "$PROCESS_NUMBER"
echo "Sent the KILL command to Process# $PROCESS_NUMBER"
else
echo "Only root can run this script"
#echo $WHO
fi
exit 0
# End of Script
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.