LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 12-21-2010, 05:34 AM   #1
josecolella
Member
 
Registered: Nov 2010
Location: Milan, Italy
Distribution: Linux Mint 13, Debian 6.0
Posts: 62

Rep: Reputation: 3
Question Having trouble with a script about detection of processes


I am trying to build a script that detects when you start the implementation of a process whose name is passed as an argument. For this I am using the command ps-ef to list all system processes.

I have this so far:
#! /bin/bash
# Title: Detection de un proceso
# Author: Jose Miguel Colella
# Description: Inicion de un proceso

while true; do
Numero=`ps -ea |grep ${1} | cut -d " " -f 2 | egrep -grep`
if test $Numero ; then
kill $Numero
fi
done

but it wont process
 
Old 12-21-2010, 05:56 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
On my system the Process ID in the output of ps -ea is the first field, not the second. Moreover it is not clear what egrep -grep means: it throws an error on my system. Anyway, take in mind that you can customize the output of the ps command, forcing it to give only the information you need. For example, if I want to retrieve the PID of a sleep process, I can do:
Code:
ps -C sleep -o pid=
Furthermore, what if two processes with the same command name are detected simultaneously? You will get a "unary operator expected" error from the test statement, since it accepts only one argument. Maybe you need to review your script a bit more.
 
1 members found this post helpful.
Old 12-21-2010, 06:00 AM   #3
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
Hi,

This part looks wrong: ps -ea |grep ${1} | cut -d " " -f 2 | egrep -grep

The egrep command is in the wrong place and isn't syntactically correct. The cut command is probably wrong as well. (field 2 vs field 1)

The egrep command should be: egrep -v grep. That is assuming you want to get rid of the grep command in the ps -ea output.

It also is in the wrong place. After this part ps -ea |grep ${1} | cut -d " " -f 2 you are left with (PID) numbers only, so the egrep will never get rid of the grep command. And egrep isn;t needed here, grep will do.

This would be better: ps -ea | grep -v "grep" | grep "${1}" | cut -d " " -f 1

You do realize that this script will keep running until you manually kill it?

Hope this helps.

Last edited by druuna; 12-21-2010 at 06:02 AM. Reason: Spelling.
 
1 members found this post helpful.
Old 12-21-2010, 06:01 AM   #4
AwesomeMachine
Senior Member
 
Registered: Jan 2005
Location: USA and Italy
Distribution: Debian testing/sid; OpenSuSE; Fedora
Posts: 1,829

Rep: Reputation: 257Reputation: 257Reputation: 257
To get a list of PROCIDs you can do this:

ps -ea | awk '{print $1}'

I can't figure out what you are actually trying to do. I'm sorry I couldn't be of more help.
 
Old 12-21-2010, 09:14 AM   #5
josecolella
Member
 
Registered: Nov 2010
Location: Milan, Italy
Distribution: Linux Mint 13, Debian 6.0
Posts: 62

Original Poster
Rep: Reputation: 3
For Druuna when I try the commands which you suggested, when I execute the script it says line 3: test: too many arguments.

Why is it doing that? and how do I fix it?

Sorry for so many questions
 
Old 12-21-2010, 09:26 AM   #6
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
Hi,

If the ps -ea | grep -v "...... line finds more then 1 PID, the test command will fail, it expects 1 item.

You can fix that by putting double quotes around the $Numero part. I.e:

if test "$Numero" ; then

As AwesomeMachine already mentioned, you should use awk '{ print $1 }' instead of the cut command.

A working version would look like this:
Code:
#!/bin/bash
# Title: Detection de un proceso
# Author: Jose Miguel Colella
# Description: Inicion de un proceso

while true
do
  Numero=`ps -ea | grep -v "grep" | grep "${1}" | awk '{ print $1 }'`
  if test "$Numero"
  then
    kill $Numero
  fi
done
Hope this helps.

Last edited by druuna; 12-21-2010 at 09:29 AM. Reason: Used echo for testing, changed it back to kill
 
Old 12-21-2010, 09:35 AM   #7
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
I don't really see the need for so many piped commands, when ps options can extract only the necessary information. Moreover it's still not clear if the OP wants to kill multiple instances of the same process (if any).
 
Old 12-22-2010, 05:18 AM   #8
josecolella
Member
 
Registered: Nov 2010
Location: Milan, Italy
Distribution: Linux Mint 13, Debian 6.0
Posts: 62

Original Poster
Rep: Reputation: 3
Thanks Druuna you answer helped me a lot
 
Old 12-22-2010, 07:37 AM   #9
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
Hi,

You're welcome

One more thing: Do have a look at what colucix posted (post #2). Although sometimes having a solution that works and knowing why it does or doesn't work is important, better (more elegant and/or more resource friendly) solutions should not be overlooked.
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Is 'nice' inherited to child processes? e.g. bash script/php script that calls MySQL SirTristan Linux - Newbie 1 12-04-2008 01:57 AM
script to delete some processes Carlos2dub Programming 1 12-14-2007 10:18 AM
How to automate detection & killing of long running ssh2 processes? yeo_harry Linux - Newbie 1 10-29-2007 04:28 AM
script to kill processes tradnbillies Linux - Software 2 04-28-2006 04:00 PM
Shell-Script for killing processes oulevon Programming 4 02-14-2006 11:49 AM


All times are GMT -5. The time now is 08:53 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration