Erodov.com Forums | India's Top Technology forum.
Loading

navigation
  • Home
  • Forums
  • Articles
  • News
  • Gaming
  • Bazaar
  • Community
  • What's New
  • My Erodov
  • Help
navigation
Go Back   Erodov.com Forums | India's Top Technology forum. > Software Zone > Open Source
Register Forgot Password?

 
 
Thread Tools Search this Thread
Old 04-11-08   #1
ਦਿਲ ਆਪਣਾ ਪੰਜਾਬੀ !!!
Pr[e]f[e]rr[e]d M[e]mb[e]r
I don't wanna set any status..
 
 
vasishta.sushant's Avatar
Rig Gallery
Need help creating a shel script


I need a simple shell script which can show what all processes are being run by a user. I can pass it a name or the script itself can read the name from "whoami".

I have lst touch with this stuff so need some help.

Anyone ?
__________________
Intel Core i5 3550s | Asus P8B75-V | GSkill 8GB DDR3 CL7 1333MHz | MSI 660Ti Power Edition | Wetern Digital Green 2TB | Intel 330 128GB SSD | CoolerMaster Hyper 212 Evo | Corsair TX650V2 | CM Gladiator 600 | Logitech G500 | Steelseries QcK Mass | Logitech Z506 | Samsung 40EH5000 LED TV
vasishta.sushant is offline   Reply With Quote
Old 04-11-08   #2
teh pretendeR!
[E]onian
I havent set my status yet.
 
 
codereverser's Avatar

 
Join Date: Nov 2007
Posts: 161
codereverser is highly Admirablecodereverser is highly Admirablecodereverser is highly Admirablecodereverser is highly Admirablecodereverser is highly Admirablecodereverser is highly Admirable
Re: Need help creating a shel script

here you go :
PHP Code:
#!/usr/bin/env bash
if [ -"$1" ]
then
user
=$1
else
user=`whoami`
fi

ps 
-fu user 
NOTE: Remove the space between the $ and user in the last line. For some reason, VB keeps on censoring them.

It's a simple script, and does not have any checks for eg:- the existence of user in the system etc.
__________________

Last edited by codereverser; 04-11-08 at 03:50 PM. Reason: damn these censoring X-(
codereverser is offline   Reply With Quote
Old 04-11-08   #3
ਦਿਲ ਆਪਣਾ ਪੰਜਾਬੀ !!!
Pr[e]f[e]rr[e]d M[e]mb[e]r
I don't wanna set any status..
 
 
vasishta.sushant's Avatar
Rig Gallery
 
Join Date: Dec 2007
Posts: 7,037
vasishta.sushant is simply Greatvasishta.sushant is simply Greatvasishta.sushant is simply Greatvasishta.sushant is simply Greatvasishta.sushant is simply Greatvasishta.sushant is simply Greatvasishta.sushant is simply Greatvasishta.sushant is simply Greatvasishta.sushant is simply Greatvasishta.sushant is simply Great
Re: Need help creating a shel script

O that was fast. Thanks. I created somethign else. Nonetheless, I appreciate your effort.

Now another thing.
I baiscally want to check if a particular process is being run by any user or not.

Here is what I am planning:
Get the current user name (done)

Next I want to use a simple command like "ps -aef | grep username" to list all the processes a user is running.

Next I want to filter the results further to search for a pattern "sasexe" and want to get a count of it.

I think I can do it till here.

The enxt task is to check for the PIDs of the found processes. And after user's permission, i want to kill thsoe processes.

A little too much but I am trying the same. Might need your help.
vasishta.sushant is offline   Reply With Quote
Old 04-11-08   #4
teh pretendeR!
[E]onian
I havent set my status yet.
 
 
codereverser's Avatar

 
Join Date: Nov 2007
Posts: 161
codereverser is highly Admirablecodereverser is highly Admirablecodereverser is highly Admirablecodereverser is highly Admirablecodereverser is highly Admirablecodereverser is highly Admirable
Re: Need help creating a shel script

you might have done it by now , if not here it is :- (I have not tested it well, but should work with a little more tweaking )

PHP Code:
#!/usr/bin/env bash
if [ -"$1" ]
then
user
=$1
else
user=`whoami`
fi
for i in `ps -Csasexe -o pid=,user= | awk -F' ' -v user="$ user" '$0 ~ user { print $1 }'`
do
echo -
"Do you want to kill job  $ i (y/n) : "
read inp
if [  $ inp == 'y' ]
then
echo $ i
fi
done 
NOTE : remove the space between the $ symbol and the variables in lines 8,10,12 and 14 . Also command in #14 to whatever you want kill -9 $ i or something.

Last edited by codereverser; 04-11-08 at 05:08 PM.
codereverser is offline   Reply With Quote
Old 04-11-08   #5
ਦਿਲ ਆਪਣਾ ਪੰਜਾਬੀ !!!
Pr[e]f[e]rr[e]d M[e]mb[e]r
I don't wanna set any status..
 
 
vasishta.sushant's Avatar
Rig Gallery
 
Join Date: Dec 2007
Posts: 7,037
vasishta.sushant is simply Greatvasishta.sushant is simply Greatvasishta.sushant is simply Greatvasishta.sushant is simply Greatvasishta.sushant is simply Greatvasishta.sushant is simply Greatvasishta.sushant is simply Greatvasishta.sushant is simply Greatvasishta.sushant is simply Greatvasishta.sushant is simply Great
Re: Need help creating a shel script

^^
Thanks a lot dude.
No buddy. I am still struggling. Haven't used AWK before so using Brute Force Method

Get online sometimes on gtalk. My E id is my gtalk id. Want to talk to you about this. You need to give me a little more explanation about this. I would then be in a position to tweak it.

Thanks a lot again. Already repped.
vasishta.sushant is offline   Reply With Quote
Old 04-11-08   #6
teh pretendeR!
[E]onian
I havent set my status yet.
 
 
codereverser's Avatar

 
Join Date: Nov 2007
Posts: 161
codereverser is highly Admirablecodereverser is highly Admirablecodereverser is highly Admirablecodereverser is highly Admirablecodereverser is highly Admirablecodereverser is highly Admirable
Re: Need help creating a shel script

tried adding , but i think gtalk clients need email domain also ( using psi in linux here ).

anyways add me :- mine is [E] id .at. gmail .dot. com
codereverser is offline   Reply With Quote
Old 04-11-08   #7
ਦਿਲ ਆਪਣਾ ਪੰਜਾਬੀ !!!
Pr[e]f[e]rr[e]d M[e]mb[e]r
I don't wanna set any status..
 
 
vasishta.sushant's Avatar
Rig Gallery
 
Join Date: Dec 2007
Posts: 7,037
vasishta.sushant is simply Greatvasishta.sushant is simply Greatvasishta.sushant is simply Greatvasishta.sushant is simply Greatvasishta.sushant is simply Greatvasishta.sushant is simply Greatvasishta.sushant is simply Greatvasishta.sushant is simply Greatvasishta.sushant is simply Greatvasishta.sushant is simply Great
Re: Need help creating a shel script

Ok will do that.

BTW i made some changes.
I think " ps -u svasis -o pid=,comm= " will be fine than using "ps -Csasexe -o pid=,user="
Now explain that AWK stuff a bit. Rest is fine.


vasishta.sushant is offline   Reply With Quote
Old 04-11-08   #8
ਦਿਲ ਆਪਣਾ ਪੰਜਾਬੀ !!!
Pr[e]f[e]rr[e]d M[e]mb[e]r
I don't wanna set any status..
 
 
vasishta.sushant's Avatar
Rig Gallery
 
Join Date: Dec 2007
Posts: 7,037
vasishta.sushant is simply Greatvasishta.sushant is simply Greatvasishta.sushant is simply Greatvasishta.sushant is simply Greatvasishta.sushant is simply Greatvasishta.sushant is simply Greatvasishta.sushant is simply Greatvasishta.sushant is simply Greatvasishta.sushant is simply Greatvasishta.sushant is simply Great
Re: Need help creating a shel script

^^
* svasis is my userid so hardcoded
vasishta.sushant is offline   Reply With Quote
Old 04-11-08   #9
teh pretendeR!
[E]onian
I havent set my status yet.
 
 
codereverser's Avatar

 
Join Date: Nov 2007
Posts: 161
codereverser is highly Admirablecodereverser is highly Admirablecodereverser is highly Admirablecodereverser is highly Admirablecodereverser is highly Admirablecodereverser is highly Admirable
Re: Need help creating a shel script

But ps -u svasis will search all the commands by the user svasis while ps -Csasexe will search for all processes with sasexe in their command line ( I thought the latter one was what you wanted ).

anyways here's the AWK stuff :
1. the ps command before will search for all processes with "sasexe" in their command line and then print it in a table with format "pid user". This output is then passed to the awk as input.

2. -v user=$ user sets the awk variable user from the bash variable of same name.

3. $0 ~ user searches each line for the user (awk variable). ( $0 stands for the whole input to the awk )

4. -F' ' then splits each line with delimiter ' ' ( one space char ). pid becomes first column and user becomes second column

5. print $1 , prints the first column from the split ( i.e. the pid )

hope this makes sense. otherwise, tell me , I'll try to simplify it more. ( I'm not good at explaining my snippets. kindo' notorious in office for the same )

codereverser is offline   Reply With Quote
Old 04-11-08   #10
teh pretendeR!
[E]onian
I havent set my status yet.
 
 
codereverser's Avatar

 
Join Date: Nov 2007
Posts: 161
codereverser is highly Admirablecodereverser is highly Admirablecodereverser is highly Admirablecodereverser is highly Admirablecodereverser is highly Admirablecodereverser is highly Admirable
Re: Need help creating a shel script

^^^ no need to hard code your userid . the script if executed without any arguments it takes your username as default ( or whatever is the output of whoami )
codereverser is offline   Reply With Quote
Old 04-11-08   #11
ਦਿਲ ਆਪਣਾ ਪੰਜਾਬੀ !!!
Pr[e]f[e]rr[e]d M[e]mb[e]r
I don't wanna set any status..
 
 
vasishta.sushant's Avatar
Rig Gallery
 
Join Date: Dec 2007
Posts: 7,037
vasishta.sushant is simply Greatvasishta.sushant is simply Greatvasishta.sushant is simply Greatvasishta.sushant is simply Greatvasishta.sushant is simply Greatvasishta.sushant is simply Greatvasishta.sushant is simply Greatvasishta.sushant is simply Greatvasishta.sushant is simply Greatvasishta.sushant is simply Great
Re: Need help creating a shel script

^^ Are nahin re. I was just testing so added ahrdcoded stuff. Will change that to a token later.
vasishta.sushant is offline   Reply With Quote
Old 04-11-08   #12
ਦਿਲ ਆਪਣਾ ਪੰਜਾਬੀ !!!
Pr[e]f[e]rr[e]d M[e]mb[e]r
I don't wanna set any status..
 
 
vasishta.sushant's Avatar
Rig Gallery
 
Join Date: Dec 2007
Posts: 7,037
vasishta.sushant is simply Greatvasishta.sushant is simply Greatvasishta.sushant is simply Greatvasishta.sushant is simply Greatvasishta.sushant is simply Greatvasishta.sushant is simply Greatvasishta.sushant is simply Greatvasishta.sushant is simply Greatvasishta.sushant is simply Greatvasishta.sushant is simply Great
Re: Need help creating a shel script

Ok thanks for the exhaustive detail. Will finalize my script and send over to your for peer review
vasishta.sushant is offline   Reply With Quote
Old 04-11-08   #13
ਦਿਲ ਆਪਣਾ ਪੰਜਾਬੀ !!!
Pr[e]f[e]rr[e]d M[e]mb[e]r
I don't wanna set any status..
 
 
vasishta.sushant's Avatar
Rig Gallery
 
Join Date: Dec 2007
Posts: 7,037
vasishta.sushant is simply Greatvasishta.sushant is simply Greatvasishta.sushant is simply Greatvasishta.sushant is simply Greatvasishta.sushant is simply Greatvasishta.sushant is simply Greatvasishta.sushant is simply Greatvasishta.sushant is simply Greatvasishta.sushant is simply Greatvasishta.sushant is simply Great
Re: Need help creating a shel script

Check this

`ps -u svasis -o pid=,comm= | awk -F' ' -v comm="sas" '$0 ~ comm { print $1 }'`

I am getting trouble debugging this. Please help.
vasishta.sushant is offline   Reply With Quote
Old 04-11-08   #14
teh pretendeR!
[E]onian
I havent set my status yet.
 
 
codereverser's Avatar

 
Join Date: Nov 2007
Posts: 161
codereverser is highly Admirablecodereverser is highly Admirablecodereverser is highly Admirablecodereverser is highly Admirablecodereverser is highly Admirablecodereverser is highly Admirable
Re: Need help creating a shel script

try this: (sometimes giving the output format in comma-separated format does not work. dont know why)
PHP Code:
ps -u svasis -o pid= -o comm
Also if you are using hardcoded value to search you can use
Code:
awk -F' ' '/sas/ { print $1 }'
the earlier code is when the value to be searched is a bash variable.
codereverser is offline   Reply With Quote
Old 04-11-08   #15
ਦਿਲ ਆਪਣਾ ਪੰਜਾਬੀ !!!
Pr[e]f[e]rr[e]d M[e]mb[e]r
I don't wanna set any status..
 
 
vasishta.sushant's Avatar
Rig Gallery
 
Join Date: Dec 2007
Posts: 7,037
vasishta.sushant is simply Greatvasishta.sushant is simply Greatvasishta.sushant is simply Greatvasishta.sushant is simply Greatvasishta.sushant is simply Greatvasishta.sushant is simply Greatvasishta.sushant is simply Greatvasishta.sushant is simply Greatvasishta.sushant is simply Greatvasishta.sushant is simply Great
Re: Need help creating a shel script

Actually I am not suer about thsi part : '$0 ~ comm { print $1 }'`
vasishta.sushant is offline   Reply With Quote
Post New Thread  Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes



Tags
creating, script, shel
All times are GMT +5.5. The time now is 08:57 PM.
Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.6.0 PL2 ©2011, Crawlability, Inc.