- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: Shell script to restart service automatically
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-13-2006 03:42 PM
тАО08-13-2006 03:42 PM
Is there anybody have shell script that can check running services and automatically start the service if it goes down?
TIA.
Regards,
Cahyo
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-13-2006 06:21 PM
тАО08-13-2006 06:21 PM
Re: Shell script to restart service automatically
Here is an example
export UNIX95=1
smproc=$(ps -C sendmail | wc -l)
if [ $smproc -le 0 ]
then
/sbin/init.d/sendmail start
fi
You have to write it yourself for the service you wish to monitor.
SEP
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-13-2006 08:20 PM
тАО08-13-2006 08:20 PM
Re: Shell script to restart service automatically
assuming you want to not test once, schedule Steven's script via cron to run at specified intervals. See man cron and man crontab
And/or see Steven's loop solution at:
http://forums1.itrc.hp.com/service/forums/bizsupport/questionanswer.do?threadId=1024003
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-14-2006 12:51 AM
тАО08-14-2006 12:51 AM
Re: Shell script to restart service automatically
I believe "if [ $smproc -le 0 ]" should be "if [ $smproc -le 1 ]", as 'ps' also returns column headings:
# UNIX95= ps -C foo123xyz | wc -l
1
Cahyo,
Another (less elegant) option:
# while :; do sleep 10; /sbin/init.d/your_daemon start; done > /dev/null 2>&1 &
'nohup' will allow it to persist after your session terminates.
PCS
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-14-2006 01:01 AM
тАО08-14-2006 01:01 AM
Re: Shell script to restart service automatically
one simple and clean solution would be to use cron to run the check script at regular interval.
e.g. Cron entry that will check your service every 15 minutes.
15 * * * * /users/scripts/check_service.sh 1>/users/scripts/logfiles/output-checkscript.crn 2>/users/scripts/logfiles/error-checkscript.crn
#
#*******************************************************************************
# min|hour |day |month|day |script
# | |of mo| |of wk|
#----|-----|-----|-----|-----|--------------------------------------------------
#*******************************************************************************
#*******************************************************************************
# END OF TABLE day0->Sunday day6->Saturday
#*******************************************************************************
hope this helps!
kind regards
yogeeraj
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-14-2006 01:33 AM
тАО08-14-2006 01:33 AM
Solution> export UNIX95=1
> smproc=$(ps -C sendmail | wc -l)
> if [ $smproc -le 0 ]
> then
> /sbin/init.d/sendmail start
> fi
It's best never to export the UNIX95 variable, even in a script. UNIX95 enables more options in ps but also affects several other commands and libraries. Use UNIX95 as a temporary variable on the command line as in:
UNIX95=1 ps -C sendmail
Now to eliminate ps headers, use the -o option as in:
UNIX95=1 ps -C sendmail -o pid= -o args=
This eliminates the header line and makes the count correct. So the script would look like this:
MYPROC=sendmail
COUNT=$(UNIX95=1 ps -C $MYPROC -o pid= -o args= | wc -l)
if [ $COUNT -lt 1 ]
then
/sbin/init.d/$MYPROC start
fi
Now the above script is only an example. For instance, if a process is unexpectedly not running, there may be some cleanup tasks that must be accomplished first. With any automated 'fix' for a broken program, you need to investigate why the process is stopping unexpectedly and eliminate the problem rather than kickstarting it from cron.
Bill Hassell, sysadmin
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-14-2006 09:04 PM
тАО08-14-2006 09:04 PM
Re: Shell script to restart service automatically
Thank you for your help :)
Rgds,
Cahyo