LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
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 05-20-2010, 03:29 PM   #1
rickstyles
LQ Newbie
 
Registered: May 2010
Posts: 1

Rep: Reputation: 0
Shell Script searches processes running


I'm new to Linux and shell scripting and need to figure out how to write a shell script the searches for processes running on my system. I really don't know where to start. can anyone give me a hand and explain how the script works?
 
Old 05-20-2010, 03:44 PM   #2
griffey
Member
 
Registered: Jan 2004
Location: East Central Illinois
Distribution: RHEL 4/5/6 and Fedora
Posts: 81

Rep: Reputation: 15
Does the script need to do something after finding the process it is looking for?

# ps aux

will show you all the processes that are running. Likewise

# top

will show you a bunch of live information about running processes.

If you want to find the process ID without looking through the output of the previous two ideas, pipe the output of ps through grep. Suppose you are looking for the http daemon. Try:

# ps aux | grep httpd

(Note that it will return a line for itself--the grep utility running--so typically if you get one line back, what you're looking for isn't running...)

Do any of those solve your situation?

Good luck.

G.--
 
Old 05-20-2010, 03:46 PM   #3
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,043
Blog Entries: 14

Rep: Reputation: 1213Reputation: 1213Reputation: 1213Reputation: 1213Reputation: 1213Reputation: 1213Reputation: 1213Reputation: 1213Reputation: 1213
There are many tutorials on the net.

http://www.linuxconfig.org/Bash_scripting_Tutorial

A script can be as simply as executing the same commands you type on the screen:

Code:
who am i
pwd
echo $PATH
For example would tell you information about your current login. Which directory you're sitting in and which directories will be searched when you type a command.

So say you wanted to do something like determine if a specific process (say "fred" was running).

Your script could be as simple as:
Code:
ps -ef |grep fred
However since "fred" appears in the process table as an argument to the command "grep" (the one you just piped into) you might want to augment it by telling it NOT to show any line containing the word "grep":

Code:
ps -ef |grep fred |grep -v grep
Check out one of the tutorials as they'll walk you through doing scripts - just remember at their basic form they can simply be a set of commands in a file that you might have otherwise typed at command line. They can get much more complex than that but there is no reason not to start doing basic things and add or modify as you learn.
 
Old 05-20-2010, 07:31 PM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,411

Rep: Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397
Bookmark these
http://rute.2038bug.com/index.html.gz
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://www.tldp.org/LDP/abs/html/
 
  


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
can you do threading or multiple processes in a shell script? BrianK Programming 8 08-07-2006 05:40 PM
Shell-Script for killing processes oulevon Programming 4 02-14-2006 11:49 AM
shell script to kill processes running on port number varunbihani Linux - Newbie 13 12-06-2005 09:46 AM
shell script to kill all processes on specified port varunbihani Linux - General 1 04-19-2005 06:39 AM
Starting processes from remote shell and keep them running... johannes Linux - Networking 5 07-23-2003 10:38 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:42 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration