LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
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 01-29-2011, 01:54 PM   #1
zx712
LQ Newbie
 
Registered: Jan 2011
Posts: 4

Rep: Reputation: 0
Shell script- how to restart applications?


Hello,

I have two applications: p1 and p2.
I need to ensure that p2 keeps running while p1 is running.
If p2 completes before p1, I need to restart p2.

While( p1 is still running)
do
run p2
done


Can someone tell how to do that?

Thank you

Last edited by zx712; 01-29-2011 at 02:06 PM.
 
Old 01-29-2011, 02:02 PM   #2
stress_junkie
Senior Member
 
Registered: Dec 2005
Location: Massachusetts, USA
Distribution: Ubuntu 10.04 and CentOS 5.5
Posts: 3,873

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
You could have a script that starts both jobs and that monitors both jobs. That script could restart p2 if required.
 
Old 01-29-2011, 02:03 PM   #3
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
I know there have been many threads similar to this in this forum in the past. They had to do with automatically restarting a crashed process, which is essentially the same thing.
Kevin Barry

Last edited by ta0kira; 01-29-2011 at 02:04 PM.
 
Old 01-29-2011, 03:45 PM   #4
zx712
LQ Newbie
 
Registered: Jan 2011
Posts: 4

Original Poster
Rep: Reputation: 0
Please provide an example.
 
Old 01-29-2011, 04:02 PM   #5
stress_junkie
Senior Member
 
Registered: Dec 2005
Location: Massachusetts, USA
Distribution: Ubuntu 10.04 and CentOS 5.5
Posts: 3,873

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Quote:
Originally Posted by zx712 View Post
Please provide an example.
No. You have been given the concept. You should be able to implement it on your own. If you try to implement it and run into problems then show your work and explain what you are having trouble with.

The concept is completely trivial.
 
Old 01-29-2011, 04:14 PM   #6
zx712
LQ Newbie
 
Registered: Jan 2011
Posts: 4

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by stress_junkie View Post
No. You have been given the concept. You should be able to implement it on your own. If you try to implement it and run into problems then show your work and explain what you are having trouble with.

The concept is completely trivial.
How can I check whether or not p1 is still running?

I tried this:

P1 &
pid1=$! #this is id of the first process

While( the status of pid1 is not zero) #how do I get the status of pid1?
do
run p2
done
 
Old 01-29-2011, 04:15 PM   #7
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Here are a few related threads:
http://www.linuxquestions.org/questi...cation-841731/
http://www.linuxquestions.org/questi...h-help-641783/
http://www.linuxquestions.org/questi...h-bash-769488/

Here's a one-liner for bash, though:
Code:
{ { program1; kill 0; } 1>&3 | while true; do program2 < /dev/null; done; } 3>&1
Kevin Barry
 
Old 01-29-2011, 04:26 PM   #8
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Quote:
Originally Posted by zx712 View Post
I tried this:

P1 &
pid1=$! #this is id of the first process

While( the status of pid1 is not zero) #how do I get the status of pid1?
do
run p2
done
Provided you only have P1 in the background, you don't need the process number; jobs will list the background processes for you.
Code:
P1 &

while jobs | grep -cq .; do
  P2
done
There are two differences between this and my previous post:
  1. This won't kill P2 when P1 exits; my one-liner will
  2. This won't allow P1 foreground access to the terminal; my one-liner will
Either can easily be made into a daemon script. Neither allow for P1 to be started independently of the script, though.
Kevin Barry

PS One-line version of this one is straightforward:
Code:
P1 & while jobs | grep -cq .; do P2; done

Last edited by ta0kira; 01-29-2011 at 04:28 PM.
 
Old 01-29-2011, 04:51 PM   #9
zx712
LQ Newbie
 
Registered: Jan 2011
Posts: 4

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by ta0kira View Post
Provided you only have P1 in the background, you don't need the process number; jobs will list the background processes for you.
Code:
P1 &

while jobs | grep -cq .; do
  P2
done
There are two differences between this and my previous post:
  1. This won't kill P2 when P1 exits; my one-liner will
  2. This won't allow P1 foreground access to the terminal; my one-liner will
Either can easily be made into a daemon script. Neither allow for P1 to be started independently of the script, though.
Kevin Barry

PS One-line version of this one is straightforward:
Code:
P1 & while jobs | grep -cq .; do P2; done
Thank you very much.
 
  


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
restart iptables from shell script hangs unitl button is pressed saiyen2002 Programming 4 11-01-2010 12:12 PM
Shell script to delete file if a pattern is found and restart servers sun81 Linux - Newbie 2 07-22-2010 02:24 PM
start applications with shell script ufmale Linux - Newbie 3 01-13-2010 06:13 PM
Shell script to check process and restart if "hung" georage Programming 5 10-29-2008 07:10 PM
restart the shell from within a shell script win32sux Linux - General 4 05-21-2006 04:07 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:01 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