LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This 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


Reply
  Search this Thread
Old 09-16-2005, 06:03 PM   #1
Riddick
Member
 
Registered: May 2004
Location: Rome, Italy
Distribution: slackware-current
Posts: 454

Rep: Reputation: 30
Script checking - what is my mistake?


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!

Help me!

Riddick
 
Old 09-16-2005, 06:21 PM   #2
Mad Scientist
Member
 
Registered: May 2003
Posts: 167

Rep: Reputation: 30
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.
 
Old 09-16-2005, 06:26 PM   #3
Riddick
Member
 
Registered: May 2004
Location: Rome, Italy
Distribution: slackware-current
Posts: 454

Original Poster
Rep: Reputation: 30
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?


Thanks,
Riddick
 
Old 09-16-2005, 06:34 PM   #4
Mad Scientist
Member
 
Registered: May 2003
Posts: 167

Rep: Reputation: 30
Add an

Code:
sed -e '2,$d'
to the end of your piping structure. For example, if you do

Code:
[user@domain ~]$ ps -e | grep spamd
 6889 ?        00:00:01 spamd
 6892 ?        00:00:00 spamd
 6893 ?        00:00:00 spamd
 6894 ?        00:00:00 spamd
 6895 ?        00:00:00 spamd
 6896 ?        00:00:00 spamd
Then you can do

Code:
[user@domain ~]$ ps -e | grep spamd | sed -e '2,$d'
 6889 ?        00:00:01 spamd
 
Old 09-16-2005, 06:41 PM   #5
Riddick
Member
 
Registered: May 2004
Location: Rome, Italy
Distribution: slackware-current
Posts: 454

Original Poster
Rep: Reputation: 30
Code:
PROCESS_NUMBER="$(ps aux | grep "$PROCESS_NAME" | grep -v grep | awk '{print $2}')|sed -e '2,$d'"
Gives:
Code:
root@edslaptop:/home/ed# killname kuickshow
Trying to kill Process kuickshow
Trying to kill Process# 10960
12678
12680|sed -e '2,'Sent the KILL command to Process# 10960
12678
12680|sed -e '2,'
root@edslaptop:/home/ed#
Perhaps we should try to find out where those numbers come
from!
Have you tried the script on your machine?


Completely unrelated question, but how do I overwrite a variable?

Thanks,
Riddick

Last edited by Riddick; 09-16-2005 at 06:42 PM.
 
Old 09-16-2005, 06:56 PM   #6
Mad Scientist
Member
 
Registered: May 2003
Posts: 167

Rep: Reputation: 30
Try this.

Code:
#!/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
 
Old 09-16-2005, 07:10 PM   #7
Riddick
Member
 
Registered: May 2004
Location: Rome, Italy
Distribution: slackware-current
Posts: 454

Original Poster
Rep: Reputation: 30
excellent, thanks very much!
I hope this could be useful to you too!

Thanks,
Riddick
 
Old 09-18-2005, 07:11 AM   #8
eddiebaby1023
Member
 
Registered: May 2005
Posts: 378

Rep: Reputation: 33
Quote:
Code:
sed -e '2,$d'
Why not use
Code:
head -1
Saves wasting time processing all those other lines. More obvious what you're doing, too.
 
Old 09-18-2005, 12:11 PM   #9
Mad Scientist
Member
 
Registered: May 2003
Posts: 167

Rep: Reputation: 30
You're right, that would be simpler.
 
Old 09-18-2005, 03:44 PM   #10
eddiebaby1023
Member
 
Registered: May 2005
Posts: 378

Rep: Reputation: 33
There's always more than one way to do it.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
arg. checking help in a script kirmet Programming 12 09-12-2005 01:02 AM
Disk space checking script? jimieee Linux - General 2 04-01-2004 02:57 AM
Checking if a device is mounted using a script thar Linux - Software 2 02-22-2004 03:17 PM
script for checking disk space pralaydesai Linux - Networking 3 03-25-2003 03:34 AM
Proccess checking within shell script khutze Programming 2 08-12-2002 01:07 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 09:21 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration