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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
11-09-2010, 12:32 AM
|
#1
|
|
LQ Newbie
Registered: Jul 2009
Posts: 21
Rep:
|
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.
|
11-09-2010, 08:31 AM
|
#2
|
|
Guru
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 5,644
|
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.
|
11-09-2010, 09:59 AM
|
#3
|
|
Moderator
Registered: May 2001
Posts: 24,970
|
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.
|
11-10-2010, 03:51 AM
|
#4
|
|
LQ Newbie
Registered: Jul 2009
Posts: 21
Original Poster
Rep:
|
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
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.
|
|
|
|
|
11-10-2010, 04:00 AM
|
#5
|
|
Member
Registered: Aug 2007
Location: Vietnam
Distribution: RedHat based, Debian based, Slackware, Gentoo
Posts: 724
Rep:
|
Quote:
Originally Posted by vishnukumar
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.
|
11-10-2010, 04:54 AM
|
#6
|
|
LQ Newbie
Registered: Jul 2009
Posts: 21
Original Poster
Rep:
|
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
|
|
|
|
11-10-2010, 05:13 AM
|
#7
|
|
LQ Newbie
Registered: Jul 2009
Posts: 21
Original Poster
Rep:
|
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
|
|
|
|
11-10-2010, 09:30 AM
|
#8
|
|
LQ 5k Club
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian Squeeze (server), Slackware 13.37 (netbook), Slackware64 14.0 (desktop),
Posts: 8,367
|
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
|
|
|
|
11-10-2010, 12:13 PM
|
#9
|
|
Moderator
Registered: May 2001
Posts: 24,970
|
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 won't work as you want it to unless you supply the required arguments to the script.
|
|
|
|
11-11-2010, 12:18 AM
|
#10
|
|
LQ Newbie
Registered: Jul 2009
Posts: 21
Original Poster
Rep:
|
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
|
|
|
|
11-11-2010, 10:10 PM
|
#11
|
|
LQ Newbie
Registered: Jul 2009
Posts: 21
Original Poster
Rep:
|
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
|
|
|
|
11-11-2010, 10:43 PM
|
#12
|
|
LQ 5k Club
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian Squeeze (server), Slackware 13.37 (netbook), Slackware64 14.0 (desktop),
Posts: 8,367
|
Quote:
Originally Posted by vishnukumar
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.
|
|
|
|
11-12-2010, 12:03 AM
|
#13
|
|
LQ Newbie
Registered: Jul 2009
Posts: 21
Original Poster
Rep:
|
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
|
|
|
|
11-12-2010, 12:13 AM
|
#14
|
|
LQ 5k Club
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian Squeeze (server), Slackware 13.37 (netbook), Slackware64 14.0 (desktop),
Posts: 8,367
|
Quote:
Originally Posted by vishnukumar
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.
|
11-12-2010, 01:47 AM
|
#15
|
|
LQ Newbie
Registered: Jul 2009
Posts: 21
Original Poster
Rep:
|
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
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 04:45 AM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|