LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
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 08-28-2014, 08:44 AM   #1
Saicharan Burle
LQ Newbie
 
Registered: Aug 2014
Posts: 24

Rep: Reputation: Disabled
finding the process of each line in text file


I need some help on the following scenario:

There are some application names that i have placed in a text file called "print.txt", Need to check the process running or not for each application listed in the text file and should through an email in case of any of the application is down. I have written one sample script, could you please help me out to get the actual logic.


#!/bin/bash
#

apps=/opt/apache/qwest/websites
cd $apps
###### This will print list of all applications into one txt file######
ls > print.txt

for username in `awk -F: '{print $1}' /opt/apache/qwest/websites/print.txt`
do
echo "Username $((i++)) : $username"
pr=`ps -ef | grep $username|grep -v 'grep'|head -1|cut -d' ' -f6`
echo " $pr "
done
 
Old 08-28-2014, 08:48 AM   #2
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
not sure why the field separator for awk is : ?
also why are you incrementing i; you dont see to be using it ?

i would use ls -1 to get a single-column list.

with the limited insite i have into your situation i think you just would need to run pgrep for each program.

Last edited by schneidz; 08-28-2014 at 08:49 AM.
 
Old 08-28-2014, 09:07 AM   #3
Saicharan Burle
LQ Newbie
 
Registered: Aug 2014
Posts: 24

Original Poster
Rep: Reputation: Disabled
the file print.txt contains the applications like :

abc-30101-40086
def-54361-54212
etc.....

And now from the above script in my earlier post produces the output as :

1 abc-30101-40086
5434 (PID for the first app)
2 def-54361-54212
6412 (PID for the second app)
etc......


Actually all these applications are hosted on single server, That is the reason i used the for loop to get the PID's of each application from the print.txt file, That's the case i ahve used the increment, Now i got stuck in how to write any loop either for or while, need to check the process of each and every line (applications) in the print.txt and send an email.


Hope that makes sense about my requirements.
 
Old 08-28-2014, 09:18 AM   #4
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
still not following but if you need to email the output this should work:
Code:
cat print.txt | mail -s “running programs” user@host
 
Old 08-28-2014, 09:26 AM   #5
Madhu Desai
Member
 
Registered: Mar 2013
Distribution: Rocky, Fedora, Ubuntu
Posts: 541

Rep: Reputation: 153Reputation: 153
@Saicharan Burle,

Is this you want ?

Code:
#!/bin/bash
#Script: test.sh

apps=/home/madhu/bin/*

for file in $apps ; do
  app=${file##*/}
  if pgrep $app > /dev/null ; then
    echo "$app: Process is running"
  else
    echo "$app: No such process"
 fi
done
Code:
$ ./test.sh 
firefox: Process is running
frd: No such process
mani: No such process
muchhu: No such process
ytview: No such process
 
1 members found this post helpful.
Old 08-28-2014, 09:39 AM   #6
Saicharan Burle
LQ Newbie
 
Registered: Aug 2014
Posts: 24

Original Poster
Rep: Reputation: Disabled
Hi, Can you help me understanding the below command:

pgrep $app > /dev/null ;


Because, I have replaced your code with same thing like below: getting no process for all the applications

#!/bin/bash
#Script: test.sh

apps=/opt/apache/qwest/websites*

for file in $apps ; do
app=${file##*/}
if pgrep $app > /dev/null ; then
echo "$app: Process is running"
else
echo "$app: No such process"
fi
done

Output:

aa99036-eap-int1-30101-40086: No such process

Infact the process of all applications are running if i give ps -ef | grep "app"


Moreover i need the Loop functionality as well
 
Old 08-28-2014, 09:48 AM   #7
Madhu Desai
Member
 
Registered: Mar 2013
Distribution: Rocky, Fedora, Ubuntu
Posts: 541

Rep: Reputation: 153Reputation: 153
pgrep $app > /dev/null ;
This blocks pgrep from displaying PID in terminal.


apps=/opt/apache/qwest/websites*

Replace this with:

apps=/opt/apache/qwest/websites/*
 
Old 08-28-2014, 09:52 AM   #8
Saicharan Burle
LQ Newbie
 
Registered: Aug 2014
Posts: 24

Original Poster
Rep: Reputation: Disabled
I am sorry I forgot to add the ? in the earlier post only, I have already changed in the script and executed but no luck yet,,

apps=/opt/apache/qwest/websites/*
 
Old 08-28-2014, 09:53 AM   #9
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
run it like this and show us the output in #code tags:
Code:
#!/bin/bash
#Script: test.sh

apps=/opt/apache/qwest/websites/*

for file in $apps ; do
echo schneidz: file = $file
  app=${file##*/}
echo schneidz: app = $app
  if pgrep $app > /dev/null ; then
    echo "$app: Process is running"
  else
    echo "$app: No such process"
 fi
done
 
Old 08-28-2014, 09:55 AM   #10
Madhu Desai
Member
 
Registered: Mar 2013
Distribution: Rocky, Fedora, Ubuntu
Posts: 541

Rep: Reputation: 153Reputation: 153
Quote:
Originally Posted by Saicharan Burle View Post
I am sorry I forgot to add the ? in the earlier post only, I have already changed in the script and executed but no luck yet,,
Can you show what is in '/opt/apache/qwest/websites'

Code:
ls -l /opt/apache/qwest/websites/
And also, show how you get PID for the contents in '/opt/apache/qwest/websites' directory.
 
Old 08-28-2014, 10:04 AM   #11
Saicharan Burle
LQ Newbie
 
Registered: Aug 2014
Posts: 24

Original Poster
Rep: Reputation: Disabled
Can you show what is in '/opt/apache/qwest/websites'

ls -l /opt/apache/qwest/websites/

drwxr-x--- 9 foss foss 4096 Apr 30 10:54 aa99036-eap-int1-30101-40086
drwxr-x--- 9 foss foss 4096 Dec 5 2013 somak-30135-40122
drwxr-x--- 9 foss foss 4096 Oct 17 2013 mlevens1-30133-40120
drwxr-x--- 9 foss foss 4096 Aug 4 07:52 mlevens-30099-40084
drwxr-x--- 9 foss foss 4096 Oct 6 2011 aqcb-40015


And also, show how you get PID for the contents in '/opt/apache/qwest/websites' directory.

/opt/apache/qwest/websites$ ps -ef | grep somak-30135-40122
foss 8963 1 0 00:59 ? 00:00:00 /bin/bash /opt/apache/qwest/websites/somak-30135-40122/watchdog httpd.worker -d /opt/apache/qwest/websites/somak-30135-40122 -DSSL
foss 8981 1 0 00:59 ? 00:00:00 httpd.worker -d /opt/apache/qwest/websites/somak-30135-40122 -DSSL
foss 8985 8981 0 00:59 ? 00:00:02 httpd.worker -d /opt/apache/qwest/websites/somak-30135-40122 -DSSL
foss 8991 8981 0 00:59 ? 00:00:01 httpd.worker -d /opt/apache/qwest/websites/somak-30135-40122 -DSSL
foss 30108 19811 0 08:59 pts/0 00:00:00 grep somak-30135-40122


In order to get the PID for this app, I am using the below command in the code :

ps -ef | grep somak-30135-40122|grep -v 'grep'|head -1|cut -d' ' -f7
8963


That is why i have included in the loop , to get the status of all the applications

apps=/opt/apache/qwest/websites
cd $apps
###### This will print list of all applications into one txt file######
ls > print.txt

for username in `awk -F: '{print $1}' /opt/apache/qwest/websites/print.txt`
do
echo "Username $((i++)) : $username"
 
Old 08-28-2014, 10:10 AM   #12
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
those arent programs they are directories. maybe you can pgrep watchdog and then grep for the directory name in a loop but again... limited insite into what you are trying to achieve.

do you have any linux technicians onsite that can program this for you ?
 
Old 08-28-2014, 10:20 AM   #13
Saicharan Burle
LQ Newbie
 
Registered: Aug 2014
Posts: 24

Original Poster
Rep: Reputation: Disabled
Smile @schneidz

Let me re-build my question, forget about the print.txt file, now from the below path:

ls -l /opt/apache/qwest/websites/

somak-30135-40122
mlevens1-30133-40120


FYI, These are the application directories where httpd resides,


Now, Can you help me of mofying/writing a script which will produce output of its process and sends an email to take an action on it, in order to start the httpd.worker process
 
Old 08-28-2014, 10:29 AM   #14
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
this seems very site specific (meaning that every corporation have different methods and programs to monitor things -- even if they are all using linux).

can you provide the output of:
Code:
pgrep watchdog
pgrep httpd.worker
 
Old 08-28-2014, 11:06 AM   #15
Saicharan Burle
LQ Newbie
 
Registered: Aug 2014
Posts: 24

Original Poster
Rep: Reputation: Disabled
It will gives you the list of all the processes that are in the websites folder. The thing is all the apps are located on the same server, if it is on seperate machines i would have written a small if condition and set in cron on daily basis.
 
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Help with awk finding line number and deleteing text puth Programming 1 02-24-2010 11:29 AM
help with c program to read each line from text file, split line , process and output gkoumantaris Programming 12 07-01-2008 12:38 PM
C++ text file line by line/each line to string/array Dimitris Programming 15 03-11-2008 08:22 AM

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

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