LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 09-15-2006, 09:53 AM   #1
nny0000
Member
 
Registered: Aug 2003
Distribution: Slackware, Ubuntu
Posts: 158

Rep: Reputation: 30
create a bash script that restarts?


I am trying to run shoutcast on my shared hosting server (it runs Debian). Every once and a while the shoutcast server dies. I am not always there to turn it back on. Is there a way to create a script that if dies will start back up?

The outline for the script is:

shoutcast server
sleep 5
DSP Plugin

If this script dies how can I set it up so it restarts again in 30 seconds?

Thanks in advance!
 
Old 09-15-2006, 10:52 AM   #2
Denes
Member
 
Registered: Mar 2004
Distribution: CentOS 4.3/4.5
Posts: 72

Rep: Reputation: 15
I would do an infinite while loop in a bash script that does a grep on ps -A and if the the main process name disappears (I am assuming it does when it dies), it restarts it again asynchronously (with an appended &). After this check it sleeps for 5 to 30 seconds. You could run the server from this script initially.
 
Old 09-15-2006, 02:01 PM   #3
haertig
Senior Member
 
Registered: Nov 2004
Distribution: Debian, Ubuntu, LinuxMint, Slackware, SysrescueCD, Raspbian, Arch
Posts: 2,331

Rep: Reputation: 357Reputation: 357Reputation: 357Reputation: 357
Quote:
Originally Posted by nny0000
If this script dies how can I set it up so it restarts again in 30 seconds?
You can use a respawn event in /etc/inittab, but that won't wait for 30 seconds. When the script dies, it would be restarted immediately.

As an example, here is how I keep the program "mythbackend" always running as userid "mythtv" when I'm at runlevel 2, 3, 4, or 5. If it ever dies (which it never does), then it would be restarted ("respawned") immediately. Here's the line I manually added to /etc/inittab:
Code:
mt:2345:respawn:/bin/su - mythtv "/usr/local/bin/mythbackend -l /var/log/mythtv/mythbackend"
 
Old 09-15-2006, 04:06 PM   #4
nny0000
Member
 
Registered: Aug 2003
Distribution: Slackware, Ubuntu
Posts: 158

Original Poster
Rep: Reputation: 30
I am on shared hosting. I doubt that I have access to the main inittab. Is their a way to do it and confine it to one bash script?

Thanks
 
Old 09-15-2006, 04:19 PM   #5
haertig
Senior Member
 
Registered: Nov 2004
Distribution: Debian, Ubuntu, LinuxMint, Slackware, SysrescueCD, Raspbian, Arch
Posts: 2,331

Rep: Reputation: 357Reputation: 357Reputation: 357Reputation: 357
Quote:
Originally Posted by nny0000
I am on shared hosting. I doubt that I have access to the main inittab. Is their a way to do it and confine it to one bash script?
Denes described how to do that in his/her post above. You could also use a cron job to check-and-restart, but you probably don't have access to cron on a shared host either. There's no such thing as a "personal inittab", so the inittab route would be out for you if you don't have priviledged access to the system. If you want to get fancy you could have a parent fork and exec a child (which is the actual server). The parent loops and waits on the child, and when the child terminates, a new one is forked. The high-level functional equivalent of a looping bash script as Denes described, but implemented differently.
 
Old 09-15-2006, 04:46 PM   #6
Denes
Member
 
Registered: Mar 2004
Distribution: CentOS 4.3/4.5
Posts: 72

Rep: Reputation: 15
Bash is easier then dealing with fork and exec and a c compiler for this type of application. Assuming your process name is Test located in the directory this script was run from, the code below will do it. You may want to echo date when it restarts just so you know when it happens.

#!/bin/sh
while true;
do
PROCESS=`ps -A | grep Test`
if [ "$PROCESS" = "" ]
then
# Not found - start process
./Test &
else
# Found it - don't do anything
fi
sleep 30
done
 
Old 09-15-2006, 05:02 PM   #7
haertig
Senior Member
 
Registered: Nov 2004
Distribution: Debian, Ubuntu, LinuxMint, Slackware, SysrescueCD, Raspbian, Arch
Posts: 2,331

Rep: Reputation: 357Reputation: 357Reputation: 357Reputation: 357
Quote:
Originally Posted by Denes
Bash is easier then dealing with fork and exec and a c compiler...
True ... but I wouldn't do it in C. I'd do it in PERL:
Code:
#!/bin/perl
while (1) { fork && wait && next; exec 'script'; }
Might want to add a bit of error checking, but the above one-liner covers the basics.
 
Old 09-15-2006, 05:14 PM   #8
dive
Senior Member
 
Registered: Aug 2003
Location: UK
Distribution: Slackware
Posts: 3,467

Rep: Reputation: Disabled
You could just make a script:

Code:
while ( true )
do
shoutcast server
sleep 5 
DSP Plugin
sleep 30 # or however long you want it to wait
done
Then you have an endless loop which you will have to 'kill' or ctrl-c to stop.

Last edited by dive; 09-15-2006 at 05:15 PM.
 
  


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
Cannot create folders with bash script called from php keyF Linux - Software 4 06-25-2006 10:58 AM
Computer restarts after syslogd restarts birdseye Linux - General 2 03-05-2006 04:27 AM
How to create a bash script to automatically disown a process. jon_k Linux - Software 5 06-19-2005 05:53 AM
How do I create application launcher using bash script msgclb Programming 2 01-30-2005 06:28 AM
send automatic input to a script called by another script in bash programming jorgecab Programming 2 04-01-2004 12:20 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

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