Help scripting to start, check, and restart processes


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help scripting to start, check, and restart processes
# 8  
Old 06-23-2011
Well I didn't actually lose sleep but thought of it. As I said, and tested, using -r started new screens each time chkscreen is ran if I was attached, because it checks for attachable screens and it is not tagged multiattach (maybe yours is in .screenrc?) But -S would just check if the session is running. I see no flaw in such method but maybe someone elses opinion would help.

But if you'd like to check PID, I can do that too. Screen would exit when jd.sh exits, so we'd have jd.sh report its PID with an echo $$ > $PIDFILE

I can write it up later. Mostly when I need my own service script like this, I'd copy one from a simple service in /etc/init.d. I am not famaliar with CentOS's as much, as I use Debian.
This User Gave Thanks to neutronscott For This Post:
# 9  
Old 06-23-2011
Seems I'm on to something, but can't get the correct PID. The PID that is saved is of the process that executes everything and not the running command's PID.

chkscr
Code:
#!/bin/sh
# chkscr: checks if the server is running.

CBPATH=/home/*user*/*serverdir*
SESSION="server1"
DAEMON="screen -d -m -S $SESSION /home/*user*/*serverdir*/exec"

if test -r $CBPATH/cb.pid; then
    CBPID=$(cat $CBPATH/cb.pid)
    if $(kill -CHLD $CBPID >/dev/null 2>&1)
    then
	exit 0
    fi
fi

echo "Restarting Server"
$DAEMON & echo $! > cb.pid

exec
Code:
#!/bin/bash

while true; do
java *server variables*
wait
done

When I type
# pidof /bin/bash /home/*user*/*serverdir*/exec
I'm given 2 PIDs. The first is for -bash and the second is the correct one. Maybe this command is returning the parent and child PIDs? I've been searching how to get a command's PID, but with no luck. When I type # ps x, I'm shown this:

PID...TTY...STAT...TIME...COMMAND
8658 pts/2 Ss+... 0:00 .. /bin/bash /home/*user*/*serverdir*/exec

This is the command's PID I need saved to cb.pid at the bottom of the chkscr script. Smilie
# 10  
Old 06-23-2011
screen will spawn a child, so I was thinking using $$ from 'exec'


You'll be checking if jd.sh is running. If it's running, java must be.. If it's not running, screen would exit (unless you add other windows I guess).

Remove the echo from chkscr and use

Code:
echo $$ >$CBPATH/cb.pid

at top of 'exec'
This User Gave Thanks to neutronscott For This Post:
# 11  
Old 06-23-2011
Finally got it with your help neutronscott. Thank you for all of your help. Smilie

I removed the " & echo $! > cb.pid" from the bottom of the chkscr script and added "ps -aef | grep -v grep | grep '/bin/bash /home/*user*/*serverdir*/exec' | awk '{print $2}' > cb.pid" to the next line.

Now it runs perfectly.

---------- Post updated at 04:38 PM ---------- Previous update was at 03:25 PM ----------

It's not working correctly. Spoke too soon, because my test didn't run long enough. It keeps making chkscr and exec processes. The check in the chkscr script is invalid.

I'm now looking for a check that checks the value in the file cb.pid to the actual PID of the process.

I can get the actual value of the processes PID by using "CBPID=$(ps -aef | grep -v grep | grep '/bin/bash /home/*user*/*serverdir*/exec' | awk '{print $2}')".

I can't find how to extract the PID number stored in the cb.pid file and make a valid "if actual PID = file's PID then end" statement.

Any help would be greatly appreciated.

Last edited by MacG32; 06-23-2011 at 05:56 PM..
# 12  
Old 06-23-2011
I thought using screen -ls was more elegant. This is what I came up with:

Code:
#!/bin/sh
# chkscreen: checks if a screen session is running.
WORKINGDIR=/home/mute/test
PIDFILE=cb.pid
SESSION="server1"
DAEMON="/home/mute/test/java-daemon.sh"

is_running()
{
    pid=$1
    name=$2
    [ -z "$pid" ] && return 1
    [ ! -d /proc/$pid ] &&  return 1
    cmd=`cat /proc/$pid/cmdline | tr "\000" "\n"|tail -n 1 |cut -d : -f 1`
    [ "$cmd" != "$name" ] &&  return 1
    return 0
}

# does the session exist?
#screen -S $SESSION -ls -q 2>&1 >/dev/null

# check java-daemon.sh's pid instead?
pid=$(cat $PIDFILE 2>/dev/null)
is_running "$pid" "$DAEMON"

if [ $? != 0 ]; then
        echo "Restarting $DAEMON"
        screen -d -m -S $SESSION $DAEMON
fi

Code:
#!/bin/sh
# java-daemon.sh: keeps restarting a buggy server... ;)

WORKINGDIR=/home/mute/test
PIDFILE=cb.pid

cd $WORKINGDIR
echo $$ >$PIDFILE

while true; do
top
done


or a more simple way without checking that it is the correct command running (it's not often the PIDs wrap around and get used for something else...?)

Code:
# check java-daemon.sh's pid instead?
pid=$(cat $PIDFILE 2>/dev/null)
kill -0 $pid 2>/dev/null

if [ "$?" != 0 ]; then
        echo "Restarting $DAEMON"
        screen -d -m -S $SESSION $DAEMON
fi

This User Gave Thanks to neutronscott For This Post:
# 13  
Old 06-24-2011
After extensive testing, it's finally all worked out.

Both files are saved in the server's directory and a crontab job running every minute with * * * * * /home/user/serverdir/chkscr >/dev/null 2>&1
here's the finished scripts.

chkscr
Code:
#!/bin/bash
# chkscr: Checks if server is running, if not, restarts it

# screen name
SESSION="server1"

# screen session and exec script
DAEMON="screen -d -m -S $SESSION /home/user/serverdir/exec"

# Gets exec's PID
PROPID=$(ps -aef | grep -v grep | grep '/bin/bash /home/user/serverdir/exec' | awk '{print $2}')

# Get PID value from file
read val < cb.pid

# Checks if exec's PID and file PID are same, sends message, then exits
if [ "$PROPID" -eq "$val" ]; then
screen -S $SESSION -X exec .! echo 'message Test complete.'
exit 0
fi

# If above check fails, deletes PID file
rm cb.pid

# Sends message, restarts server, remakes PID file with exec's PID for above check
echo "Restarting the server now."
$DAEMON
ps -aef | grep -v grep | grep '/bin/bash /home/user/serverdir/exec' | awk '{print $2}' > cb.pid

exec
Code:
#!/bin/bash
# exec: Starts the server from inside the screen and restarts it, if stopped

while true; do
java server variables
wait
done

Thanks neutronscott for all of your help and suggestions.

Last edited by MacG32; 06-24-2011 at 04:10 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Red Hat

Service restart and check if running

Hello, I'l like to create a script that restart a service (/etc/init.d/httpd restart) and also check if after restart the service is actually running. Sometimes it happen that at the first try the service fails to restart. Thanks (2 Replies)
Discussion started by: bazzola
2 Replies

2. Emergency UNIX and Linux Support

Check hung process and restart

Hi all I have networker running on a RHEL 5.7 and over time it hangs. So the solution backup team proposed is to check if the process is hung, to stop and start it. Unfortunately for me, the rc script only allows three commands, start, stop and status (no restart option) so I managed to set... (15 Replies)
Discussion started by: hedkandi
15 Replies

3. UNIX for Dummies Questions & Answers

Stop/Start vs. Restart

Is there any functional difference between: issuing separate stop/start commands like this; super (handler) (instance) stop super (handler) (instance) start versus issuing a single recycle command like this; super (handler) (instance) restart (3 Replies)
Discussion started by: Newbix
3 Replies

4. Solaris

How to start/stop processes

Please anyone tell me In my last interview the HR asks me how to monitor, start,stop & kill the various processes and subprocesses. Please anyone explain me clearly. It's my personal request (3 Replies)
Discussion started by: suneelieg
3 Replies

5. Shell Programming and Scripting

guarantee to start before restart...

Hi All, is there a way or script that i can check my AIX 5.3 OS will restart before i made restart? is there a script that can check all the startup files are ok before restarting. it is because i was stuck last time when i restart my PC because some startup files were missing:o. (2 Replies)
Discussion started by: malcomex999
2 Replies

6. Shell Programming and Scripting

Start Stop Restart

I'm wondering how I should make a script that can start, stop, and restart another script. What I need to be able to do, is start and stop a perl script from the command line. The easiest way of doing this seems to be to have another script, starting and stopping the other script. I have BASH,... (7 Replies)
Discussion started by: Bakes
7 Replies

7. UNIX for Advanced & Expert Users

Log 'syslog start/stop/restart' messages

How can I tell my syslog.conf to log "syslog start/stop/restart" messages on a Solaris box? (1 Reply)
Discussion started by: SunnyK
1 Replies

8. AIX

How to start/stop/restart NFS on AIX

Hi, Very new to aix How to start/stop/restart NFS on AIX thanks, (2 Replies)
Discussion started by: jredx
2 Replies

9. Shell Programming and Scripting

need help: shell script to restart apache when no. of processes keeps growing

I need a shell script to kill apache and restart it, in case the number of processes keeps growing. The logic is like the below, but I don't know how to get the number and neither the syntax. Could somebody kindly help? if no_of_processes (ps ax ¦ grep httpd) > 200 then killall httpd... (14 Replies)
Discussion started by: _joshua_
14 Replies

10. Shell Programming and Scripting

How can I make a program start up automatically after the computer restart/startup?

hi all How can I make a program start up automatically after the computer restart/startup in fedora? something like: ... Establish a shell then run some of command code. Thanks for Help!! (1 Reply)
Discussion started by: munna_dude
1 Replies
Login or Register to Ask a Question