LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 09-10-2010, 09:59 AM   #1
kav
Member
 
Registered: May 2006
Location: USA
Distribution: FreeBSD Ubuntu Debian
Posts: 137

Rep: Reputation: 15
How to automate starting a process in gnu screen and restart it when it crashes


I have a java service that needs to be run in a gnu screen session so that I can -r into it to access it's interactive shell. I would like this to run at start-up. More importantly, it crashes frequently so I need the service to restart itself when it crashes.

How do I go about doing this myself? My previous experience with shell scripting amounts to putting ./service in a file and marking it executable.

This will be running on a FreeBSD server, but I might put it on a Linux server soon also. So any caveats on the differences would be useful information.

Thank you
 
Old 09-10-2010, 11:23 AM   #2
goldenbarb
Member
 
Registered: Aug 2010
Distribution: Fedora, Centos, Debian
Posts: 49

Rep: Reputation: 7
1. To restart service you can use simple bash script with infinite loop. It should check if your script is running and restart it if not.
2. Simplest way to run script at startup is init.d (for Linux) or rc.d (for FreeBSD) script with dtach. There a lot of examples.
It's something like screen but with minimal functionality.
 
Old 09-10-2010, 12:26 PM   #3
kav
Member
 
Registered: May 2006
Location: USA
Distribution: FreeBSD Ubuntu Debian
Posts: 137

Original Poster
Rep: Reputation: 15
I've used dtach before, it shouldn't take much to re-familiarize myself. The loop should be easy enough, but how do I perform a check to see if the process is running?
 
Old 09-10-2010, 12:32 PM   #4
goldenbarb
Member
 
Registered: Aug 2010
Distribution: Fedora, Centos, Debian
Posts: 49

Rep: Reputation: 7
For Linux:
Code:
while (:) ; do ps axu | grep -q your_script || echo "not  running" ; sleep 60 ; done
FreeBSD version:
Code:
while (:) ; do ps -ax | grep -q your_script || echo "not  running " ; sleep 60 ; done
 
1 members found this post helpful.
Old 09-10-2010, 01:04 PM   #5
kav
Member
 
Registered: May 2006
Location: USA
Distribution: FreeBSD Ubuntu Debian
Posts: 137

Original Poster
Rep: Reputation: 15
Actually ps aux works fine in freebsd too.

I figured I'd need some kind of way to set the pid and check for it. Your simpler solution is much better and so obvious I feel foolish for not thinking of it myself.

When the process dies so does the dtach session right?
 
Old 09-10-2010, 01:25 PM   #6
goldenbarb
Member
 
Registered: Aug 2010
Distribution: Fedora, Centos, Debian
Posts: 49

Rep: Reputation: 7
Quote:
Originally Posted by kav View Post
When the process dies so does the dtach session right?
I suppose so.
 
Old 09-12-2010, 03:26 PM   #7
kav
Member
 
Registered: May 2006
Location: USA
Distribution: FreeBSD Ubuntu Debian
Posts: 137

Original Poster
Rep: Reputation: 15
This works perfectly. Thanks a lot.
 
  


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
LXer: Speaking Unix, Part 6: Automate, automate, automate! LXer Syndicated Linux News 0 01-04-2007 09:54 AM
How to automate an FTP process? ksakamuri Linux - Newbie 6 11-05-2006 05:52 PM
Automate Application Restart wfernley Linux - Software 2 07-20-2006 06:24 PM
GNU Screen Error: Could Not Write /var/run/utmp: No such process DarkElf109 Linux - Software 3 06-10-2006 02:13 AM
gdm crashes but ok after restart ArchNGEL Linux - Software 6 07-31-2005 02:48 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 12:07 PM.

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