LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 11-09-2010, 12:32 AM   #1
vishnukumar
LQ Newbie
 
Registered: Jul 2009
Posts: 21

Rep: Reputation: 0
Showing process id but there is no such process


Hi,

I have a shell script to identify whether the process is running or not. If the process is not running, then I execute another script file to run my application. Below is my script and saved this script as monitorprocess.sh

Code:
#!/bin/bash

result=$(ps -ef | grep -v grep | grep "applicationname.sh" | awk '{print $2}')
echo $result

if [ "$result" == "" ];
then
echo "process is not running"
sh applicationname.sh
fi
When I execute the script file as "sh monitorprocess.sh". I got printed with process id, something like 4726 4727. But, if I execute this ps -ef | grep -v grep | grep "applicationname.sh" | awk '{print $2}' in the shell. I got no output.

Actually, executing shell script should not print the process id, because the actual process is not running. How to fix it?

Thanks
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 11-09-2010, 08:31 AM   #2
MensaWater
Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 6,007
Blog Entries: 5

Rep: Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783
Your script works fine for me though I'd reverse the two greps (have the "grep -v grep" after the one for application.sh - that way the second grep is processing less information).

The output is telling you PIDs 4726 and 4727 match "application.sh". What happens if you run "ps -fp<PID>" directly after running the script (e.g. ps -fp4726 or ps -fp4727)?

How long does application.sh take to run? Is it supposed to always be running - it could simply be that it application.sh is dying before you do your command line grep.

Also how are you calling the monitorprocess.sh script. Is it an infinite loop like:
while true
do monitorprocess.sh
done

Or maybe a cron job? Run every minute?

It could very well be that monitorprocess.sh is starting the PIDs you're seeing on its first run but then the application.sh is dying so it isn't there when you check from command line. The fact that the PIDs you show are 1 appart suggests one is the child of the other. Does application.sh call itself with a while loop perhaps?

How is application.sh started normally? (That is to say if monitorprocess.sh didn't originally start it what did?) Does application.sh run without dying if you start it from command line? Do you try to start it from cron?

If you're using cron for any of this (or init scripts) remember that the environment you use at command line inherits lots of variables from your login (most importantly PATH) but cron and init scripts do NOT. They have minimal environments so it is important to set the variables you need within the script itself.
 
1 members found this post helpful.
Old 11-09-2010, 09:59 AM   #3
unSpawn
Moderator
 
Registered: May 2001
Posts: 27,305
Blog Entries: 54

Rep: Reputation: 2857Reputation: 2857Reputation: 2857Reputation: 2857Reputation: 2857Reputation: 2857Reputation: 2857Reputation: 2857Reputation: 2857Reputation: 2857Reputation: 2857
I'd ditch the greps completely and use something like this:
Code:
#!/bin/sh --
pgrep /path/to/applicationname.sh >/dev/null 2>&1 || { logger "process is not running"; /bin/sh /path/to/applicationname.sh &
 disown %1; }
exit 0
* Note that if "applicationname.sh" starts another application and then exits while that application runs in the background (as daemon) if would be easier to use the full or partial command line for that application. For example if your script results in running '/usr/sbin/some-daemon -DSSL --some-more-args' you could use 'pgrep -f 'n/some-daemon -DSSL';' to ensure you "grep" the right process details.
 
2 members found this post helpful.
Old 11-10-2010, 03:51 AM   #4
vishnukumar
LQ Newbie
 
Registered: Jul 2009
Posts: 21

Original Poster
Rep: Reputation: 0
Hi Unspawn,

Your code doesn't work for me.

Below is my progress,

1. I have executed applicationname.sh via "sh applicationname.sh"
2. I saved your script "#!/bin/sh --
pgrep /path/to/applicationname.sh >/dev/null 2>&1 || { logger "process is not running"; /bin/sh /path/to/applicationname.sh &
disown %1; }
exit 0" in another file
3. Now, I have executed the saved script. But, it prints "process is not running" and also started the process. However, the process is already running, I have checked this by executing "ps -ef | grep applicationname.sh" and got the result as
Code:
root      4351  2848  0 12:02 pts/1    00:00:00 sh applicationname.sh
root      4420  2848  0 12:11 pts/1    00:00:00 grep applicationname.sh
Have I did any mistake?

Thanks

Quote:
Originally Posted by unSpawn View Post
I'd ditch the greps completely and use something like this:
Code:
#!/bin/sh --
pgrep /path/to/applicationname.sh >/dev/null 2>&1 || { logger "process is not running"; /bin/sh /path/to/applicationname.sh &
 disown %1; }
exit 0
* Note that if "applicationname.sh" starts another application and then exits while that application runs in the background (as daemon) if would be easier to use the full or partial command line for that application. For example if your script results in running '/usr/sbin/some-daemon -DSSL --some-more-args' you could use 'pgrep -f 'n/some-daemon -DSSL';' to ensure you "grep" the right process details.
 
Old 11-10-2010, 04:00 AM   #5
quanta
Member
 
Registered: Aug 2007
Location: Vietnam
Distribution: RedHat based, Debian based, Slackware, Gentoo
Posts: 724

Rep: Reputation: 100Reputation: 100
Quote:
Originally Posted by vishnukumar View Post
2. I saved your script "#!/bin/sh --
pgrep /path/to/applicationname.sh >/dev/null 2>&1 || { logger "process is not running"; /bin/sh /path/to/applicationname.sh &
disown %1; }
exit 0" in another file
[/CODE]

Have I did any mistake?

Thanks
Change to your right path or simply keep 'applicationname.sh'.
 
1 members found this post helpful.
Old 11-10-2010, 04:54 AM   #6
vishnukumar
LQ Newbie
 
Registered: Jul 2009
Posts: 21

Original Poster
Rep: Reputation: 0
Hi all,

Thanks for your reply.

Quanta, I have tried with your answer. Still, it is not working. I have an application called ReportGen. It has a shell script to run the application. I call the shell script with parameters. I executed the shell script as a background process and checked whether it exists via "ps -ef | grep reportgen" and found that, it is still running. Then I have executed your script and prints that "process is not running" and it has tried to execute my application once again. But, actually it should not print "process is not running".

Below are the steps I performed

Code:
[root@localhost ReportGen]# sh reportgen.sh 12 16:00:00 03:59:59 '(7_AM_-_7_PM)' 0 3 05/15/2010 &
[root@localhost ReportGen]# ps -ef | grep reportgen
root      6732  6555  0 16:10 pts/4    00:00:00 sh reportgen.sh 12 16:00:00 03:59:59 (7_AM_-_7_PM) 0 3 05/15/2010
root      6752  6555  0 16:12 pts/4    00:00:00 grep reportgen
[root@localhost ReportGen]# pgrep reportgen.sh >/dev/null 2>&1 || { echo "process is not running"; /bin/sh reportgen.sh & disown %1; }
process is not running
[1] 6755
[root@localhost ReportGen]# ps -ef | grep reportgen
root      6732  6555  0 16:10 pts/4    00:00:00 sh reportgen.sh 12 16:00:00 03:59:59 (7_AM_-_7_PM) 0 3 05/15/2010
root      6758  6555  0 16:12 pts/4    00:00:00 grep reportgen
Thanks
 
Old 11-10-2010, 05:13 AM   #7
vishnukumar
LQ Newbie
 
Registered: Jul 2009
Posts: 21

Original Poster
Rep: Reputation: 0
Hi all,

One more thing,

Below script is working properly in my local machine.
Code:
#!/bin/bash

result=$(ps -ef | grep -v grep | grep "applicationname.sh" | awk '{print $2}')
echo $result

if [ "$result" == "" ];
then
echo "process is not running"
sh applicationname.sh
fi
However, if I use this script in my server, it is not working because it prints some process id (echo $result). I am sure that "applicationname.sh" is not running in the server while I execute this above script. To ensure that the process is not running, I have pasted this code in the terminal "ps -ef | grep -v grep | grep "applicationname.sh" | awk '{print $2}'" and there is no output.

Below are my bash version of my local machine and server

Local machine
GNU bash, version 3.2.25(1)-release (i686-redhat-linux-gnu)
Copyright (C) 2005 Free Software Foundation, Inc.

Server
GNU bash, version 3.2.25(1)-release (x86_64-redhat-linux-gnu)
Copyright (C) 2005 Free Software Foundation, Inc.

Thanks
 
Old 11-10-2010, 09:30 AM   #8
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,546
Blog Entries: 28

Rep: Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176
How about dumping the ps -ef output? It might help to know what the rest of the pipeline is working on.
Code:
#!/bin/bash

result=$(ps -ef | tee <some file name> | grep -v grep | grep "applicationname.sh" | awk '{print $2}')
echo $result

if [ "$result" == "" ];
then
echo "process is not running"
sh applicationname.sh
fi
 
Old 11-10-2010, 12:13 PM   #9
unSpawn
Moderator
 
Registered: May 2001
Posts: 27,305
Blog Entries: 54

Rep: Reputation: 2857Reputation: 2857Reputation: 2857Reputation: 2857Reputation: 2857Reputation: 2857Reputation: 2857Reputation: 2857Reputation: 2857Reputation: 2857Reputation: 2857
It seems you actually run the command you need with lots of arguments (" 12 16:00:00 03:59:59 '(7_AM_-_7_PM)' 0 3 05/15/2010"). So
Code:
pgrep -f 'sh reportgen.sh .*$'
should be able to pick up the running instance. But the
Code:
sh reportgen.sh
won't work as you want it to unless you supply the required arguments to the script.
 
Old 11-11-2010, 12:18 AM   #10
vishnukumar
LQ Newbie
 
Registered: Jul 2009
Posts: 21

Original Poster
Rep: Reputation: 0
Hi Unspawn,

I'm sorry, I didn't send the correct code to you. Below is my code
Code:
#!/bin/bash

result=$(ps -ef | grep -v grep | grep "reportgen.sh" | awk '{print $2}')

if [ "$result" == "" ];
then
sh reportgen.sh $1 $2 $3 $4 $5 $6 $7
fi

Thank you
 
Old 11-11-2010, 10:10 PM   #11
vishnukumar
LQ Newbie
 
Registered: Jul 2009
Posts: 21

Original Poster
Rep: Reputation: 0
Hi,

I have fixed the issue with the script. The fix is, I have added the correct path to the shell script within another shell script. Below is my script
#!/bin/bash

result=$(ps -ef | grep -v grep | grep "reportgen.sh" | awk '{print $2}')

if [ "$result" == "" ];
then
sh /var/www/Reportgen/reportgen.sh $1 $2 $3 $4 $5 $6 $7
fi
 
Old 11-11-2010, 10:43 PM   #12
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,546
Blog Entries: 28

Rep: Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176
Quote:
Originally Posted by vishnukumar View Post
Hi,

I have fixed the issue with the script. The fix is, I have added the correct path to the shell script within another shell script. Below is my script
#!/bin/bash

result=$(ps -ef | grep -v grep | grep "reportgen.sh" | awk '{print $2}')

if [ "$result" == "" ];
then
sh /var/www/Reportgen/reportgen.sh $1 $2 $3 $4 $5 $6 $7
fi
So you chose to ignore two good suggestions: unSpawn's suggestion of using pgrep and MensaWater's suggestion of swapping the grep command order. You've got something that works and that's good but people may be less inclined to offer suggestions in future.
 
Old 11-12-2010, 12:03 AM   #13
vishnukumar
LQ Newbie
 
Registered: Jul 2009
Posts: 21

Original Poster
Rep: Reputation: 0
Hi all,

I didn't ignore suggestions. I am newbie to Linux. I am sure that the fix is due to your suggestions to help me find a solution. Actually, I tried of those suggestions, but I don't know how to make fit in my environment. Also, Mensawater questions on cron helped me a lot because I actually run the script via cron.

Sometimes it is difficult to express correctly. Sorry for my bad english.

Thanks
 
Old 11-12-2010, 12:13 AM   #14
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,546
Blog Entries: 28

Rep: Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176
Quote:
Originally Posted by vishnukumar View Post
Hi all,

I didn't ignore suggestions. I am newbie to Linux. I am sure that the fix is due to your suggestions to help me find a solution. Actually, I tried of those suggestions, but I don't know how to make fit in my environment. Also, Mensawater questions on cron helped me a lot because I actually run the script via cron.

Sometimes it is difficult to express correctly. Sorry for my bad english.

Thanks
Understood. When learning something new, sometimes you need to learn a few things well and not try to do too much. That could apply to unSpawn's pgrep.

Regards MensaWater's grep ordering, it is nothing new, just changing grep -v grep | grep "reportgen.sh" to grep "reportgen.sh" | grep -v grep. The idea is to put the filter that gives least output first so the second filter has little work.

That running via cron was the key issue; glad you solved it.

Welcome to the Linux adventure
 
1 members found this post helpful.
Old 11-12-2010, 01:47 AM   #15
vishnukumar
LQ Newbie
 
Registered: Jul 2009
Posts: 21

Original Poster
Rep: Reputation: 0
Hi Catkin,

Yes, the script "grep "reportgen.sh" | grep -v grep" works fine.

I also want to tell another thing to all. I have used this script for preventing multiple instances of a same service (may be an application) which is initiated by cron. I got this issue from my client, saying that their server is slow due to multiple instances of a same service. So, this interlock script helped me to resolve this issue.

This message will help others.

Thanks
 
  


Reply

Tags
shell script, shell scripting


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
Sending Signal from Child Process to Parent Process : Not getting desired output thelink123 Linux - General 4 10-26-2012 09:05 PM
Finding the Process ID of a Process While Initiating the Process senthilmuthiah Linux - Newbie 7 04-02-2009 10:37 AM
ps showing duplicate process same PID dooahhdoo Red Hat 0 07-09-2004 05:52 AM


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

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration