Sponsored Content
Top Forums Shell Programming and Scripting Help scripting to start, check, and restart processes Post 302532650 by MacG32 on Tuesday 21st of June 2011 02:38:24 PM
Old 06-21-2011
Help scripting to start, check, and restart processes

Here it goes from my unexperienced point of view. I am using CentOS 5.6. I have a Java based server that needs to be running 24/7/365. To begin from the machine the server is on rebooting; I SSH in to a shell, cd to the server dir, screen -S server1, and execute ./exec (listed below) in the screen. This runs the Java server and restarts the it's process if/when stopped.

exec
Code:
#!/bin/bash
while true; do
java (*all variables*)
wait
done

I would like to automate this starting process with a script using a crontab job. This script(s) would need to do what I do manually to start the server, perform checks to make sure the 3 processes are always running (screen, exec, java) and be able to restart them in their appropriate environment (command line or screen).

I have tried the psybnc psybncchk script with some adjustments to try and do some of this, but my knowledge of scripting is very minimal.

cbchk
Code:
CBPATH=/home/*user*/*dir*

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
cd $CBPATH
SCREEN -S server1
./exec

When I kill all of the processes, then run the above script, the shell screen gets spammed with arrows and the server's starting info displays inside the never ending arrows. When I attach to the screen, Java is continually stating errors nonstop. I've also tried adding " & echo $! > cb.pid" after the ./exec to automate getting the PID, but that makes the screen unusable for some reason.

While exec is running, it's keeping the Java server running, but when all of the processes are killed, I'm not sure how to script the starting sequence and have it make sure everything stays running. Any and all help will be greatly appreciated. Thank you.

Edit: Edited for clarity, hopefully.

Last edited by MacG32; 06-21-2011 at 07:34 PM..
 

10 More Discussions You Might Find Interesting

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

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

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

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

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

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

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

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

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

10. 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
Control(3pm)						User Contributed Perl Documentation					      Control(3pm)

NAME
App::Control - Perl module for apachectl style control of another script or executable SYNOPSIS
use App::Control; my $ctl = App::Control->new( EXEC => $exec, ARGS => @args, PIDFILE => $pidfile, SLEEP => 1, VERBOSE => 1, ); my $pid = $ctl->pid; if ( $ctl->running ) { print "$pid is running "; } else { print "$pid is not running "; } # or alternatively ... print $ctl->status; $ctl->start; # or alternatively ... $ctl->cmd( 'start' ); $ctl->stop; $ctl->hup; $ctl->restart; DESCRIPTION
App::Control is a simple module to replicate the kind of functionality you get with apachectl to control apache, but for any script or exe- cutable. There is a very simple OO interface, where the constructor is used to specify the executable, command line arguments, and pidfile, and various methods (start, stop, etc.) are used to control the executable in the obvious way. The module is intended to be used in a simple wrapper control script. Currently the module does a fork and exec to start the executable, and sets the signal handler for SIGCHLD to 'IGNORE' to avoid zombie processes. CONSTRUCTOR
The constructor is called with a hash of options in the standard way. The options are as follows: EXEC Path to the executable to be controlled. This option is REQUIRED. ARGS Command line arguments for the executable. This option is OPTIONAL, but if set, should be an ARRAY reference. PIDFILE Path to the pidfile for the executable. This need not exists, but the constructor will die if it thinks it can't create it. If the path where the pidfile lives doesn't exist the constructor will try to create it. This option is REQUIRED. IGNOREFILE The ignore file allows you to temporarily disable the control functionality. Suppose you have a chkdaemon / crontab entry that restarts a service; specifying an IGNOREFILE means that you can disable this wihtout having to edit the relevant config files. CREATE_PIDFILE By default, App::Control depends on the application to manage the pid file. This is consistent will analogous utilities (apachectl, chk- daemon, etc.), but if you would like App::Control to create and remove pid files for you, then set this option to a true value. SLEEP Number of seconds to sleep before checking that the process has been started. If the start fails, the control script will loop with a SLEEP delay per iteration until it has (see <"LOOP">). Default is 1 second. head2 LOOP Number of times to loop before giving up on starting the process. VERBOSE If set to a true value, the module will output verbose messages to STDERR. METHODS
start Start the executable specified in the constructor. This method waits until it is convinced that the executable has started. It then writes the new pid to the pidfile. stop Stop the executable specified in the constructor. It assumes that the pid listed in the pidfile specified in the constructor is the process to kill. This method waits until it is convinced that the executable has stopped. hup Send a SIGHUP to the executable. restart Basically; stop if running, and then start. status Returns a status message along the lines of "$exec ($pid) is / is not running". cmd All of the above methods can also be invoked using cmd; i.e.: $ctl->start; is equivilent to: $ctl->cmd( 'start' ); give or take a call to AUTOLOAD! pid Returns the current value of the pid in the pidfile. running returns true if the pid in the pidfile is running. AUTHOR
Ave Wrigley <Ave.Wrigley@itn.co.uk> COPYRIGHT
Copyright (c) 2001 Ave Wrigley. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. perl v5.8.8 2003-08-27 Control(3pm)
All times are GMT -4. The time now is 11:26 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy