LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 07-11-2011, 02:41 AM   #1
Erco21
LQ Newbie
 
Registered: Jul 2011
Posts: 2

Rep: Reputation: Disabled
Check if process is running, if not, execute another script


Hello!

I have a game server running using java, and it tends to crash sometimes, and only way to start it now is manually, so i thought i could create a script to check periodically if java is running, and if it isn't, run my startup script for the game server

also, if it's possible it should log when it actually started the server startup script into a text file or something, so i can check why it crashed

Thank you in advance!
 
Old 07-11-2011, 04:33 AM   #2
nooneknowme
Member
 
Registered: Feb 2008
Location: Bangalore, India
Posts: 66

Rep: Reputation: 4
Don't think it will be difficult .. you can use the ps command in your script to check the process and restart if the process is not running .. Perhaps set it up using cron. loging should not be a problem as well...
 
Old 07-11-2011, 04:48 AM   #3
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,036

Rep: Reputation: 372Reputation: 372Reputation: 372Reputation: 372
Quote:
Originally Posted by Erco21 View Post
Hello!

I have a game server running using java, and it tends to crash sometimes, and only way to start it now is manually, so i thought i could create a script to check periodically if java is running, and if it isn't, run my startup script for the game server
This can be done in many ways. You could effectively check using ps or any other tool, or you can just do something in the lines of:

Code:
while ! <insert your command here>; do sleep 2; done
This does the following:
  • runs the command
  • sleeps 2 seconds
  • if the command didn't exit with success (exit status equal to '0') then repeat

This might or might not work depending on how the program crashes (plus java tends to act its way, instead of just doing things the right and standard way).

A more complete schema to do this would be something like:

Code:
#!/bin/bash

# Run command, add ampersand to fork to the background
<command> &
# Capture PID
MY_PID=$!
# Wait until process ends, one or another way
wait $MY_PID
# Then you can do whatever you want, including checking the exit status (in $?).
 
Old 07-12-2011, 04:48 AM   #4
Erco21
LQ Newbie
 
Registered: Jul 2011
Posts: 2

Original Poster
Rep: Reputation: Disabled
Hey, thanks for the reply, but i tried a different approach, which didn't quite work

I made a script:
Code:
#!/bin/sh
if [ -z "$(pgrep java)" ]
  then
	echo "Started the server"
     /minecraft/run.sh
  else
	echo "Server is already running"
fi
Which does check if java is running, but can't start the /minecraft/run.sh script, because that script (/minecraft/run.sh) says that it can't access the .jar file of the server
but when i run it manually, it runs just fine








*/3 * * * * /root/check_running.sh
 
Old 07-12-2011, 05:35 AM   #5
nooneknowme
Member
 
Registered: Feb 2008
Location: Bangalore, India
Posts: 66

Rep: Reputation: 4
Try changing it to look like below, or whatever shell your script is in.

Quote:
/bin/sh /minecraft/run.sh
 
Old 07-12-2011, 09:12 AM   #6
SL00b
Member
 
Registered: Feb 2011
Location: LA, US
Distribution: SLES
Posts: 375

Rep: Reputation: 111Reputation: 111
Have you looked at putting this in the inittab with the respawn action?

Code:
Valid actions for the action field are:

respawn
       The  process  will  be  restarted  whenever  it terminates (e.g.
       getty).
 
  


Reply


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
[SOLVED] bash script - check process is running, if not start zee# Linux - Newbie 8 02-07-2010 05:50 PM
Bash script to check if process is running and send email depam Linux - Newbie 2 04-08-2009 12:11 AM
Obtain ip address and check for running process via Bash Script? xconspirisist Programming 10 09-12-2008 01:18 PM
script to check if process is dead or running rspurlock *BSD 6 04-12-2004 11:32 PM
[script] check for a running process mikshaw Linux - Software 2 01-13-2004 08:33 PM


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