LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Check if process is running, if not, execute another script (https://www.linuxquestions.org/questions/linux-newbie-8/check-if-process-is-running-if-not-execute-another-script-891037/)

Erco21 07-11-2011 02:41 AM

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!

nooneknowme 07-11-2011 04:33 AM

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...

i92guboj 07-11-2011 04:48 AM

Quote:

Originally Posted by Erco21 (Post 4411302)
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 $?).


Erco21 07-12-2011 04:48 AM

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

nooneknowme 07-12-2011 05:35 AM

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

Quote:

/bin/sh /minecraft/run.sh

SL00b 07-12-2011 09:12 AM

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).



All times are GMT -5. The time now is 01:37 AM.