nixCraft Linux Forum

nixCraft

Linux Tech Support Forum

start and stop service in crontab.

This is a discussion on start and stop service in crontab. within the Shell scripting forums, part of the Development/Scripting category; Hi all, I am unix newbie, but currently my boss required me to write a scripts as below: * An ...


Go Back   nixCraft Linux Forum > Development/Scripting > Shell scripting

Register FAQ Members List Calendar Forgotten your password? Mark Forums Read
  #1 (permalink)  
Old 04-27-2007, 02:09 PM
Junior Member
User
 
Join Date: Apr 2007
Posts: 7
Rep Power: 0
gthian is on a distinguished road
Default start and stop service in crontab.

Hi all,

I am unix newbie, but currently my boss required me to write a scripts as below:



* An automated script to bring down our ids, ims, iws & ics
daemons in a correct sequence
* Script to bring up the above, also by the correct sequence

where ids=iplanet directory server,
ims=iplanet messaging server,
iws=iplanet webserver
ics=iplanet calendar server

I know all the command to start and stop the service, but I do not know how to start writing. Please help.

Many many thanks
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 04-27-2007, 04:34 PM
nixcraft's Avatar
Never say die
User
 
Join Date: Jan 2005
Location: BIOS
My distro: Ubuntu
Posts: 1,061
Rep Power: 10
nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute
Default

Welcome to our forum!

It is pretty simple. Here is a sample script:

Code:
#!/bin/bash
IMS_START="/path/to/start command"
IWS_START="/path/to/start command"

IMS_STOP="/path/to/stop command"
IWS_STOP="/path/to/stop command"

case "$1" in
    start)
        $IMS_START
        $IWS_START
    ;;
    stop)
        $IMS_STOP
        $IWS_STOP
    ;;
    restart)
    $0 stop
    $0 start
    ;;
    *)
    echo "Usage: $0 {start|stop|restart}"
    exit 1
    ;;
esac
Set executable permission
Code:
chmod +X script
To start all server
Code:
./script start
To Stop all server
Code:
./script stop
__________________
Vivek | My personal blog
Linux Evangelist
Play hard stay cool
Reply With Quote
  #3 (permalink)  
Old 04-27-2007, 10:12 PM
Junior Member
User
 
Join Date: Apr 2007
Posts: 7
Rep Power: 0
gthian is on a distinguished road
Default thanks

Many many thanks. This is really a great forum.



Quote:
Originally Posted by nixcraft View Post
Welcome to our forum!

It is pretty simple. Here is a sample script:

Code:
#!/bin/bash
IMS_START="/path/to/start command"
IWS_START="/path/to/start command"

IMS_STOP="/path/to/stop command"
IWS_STOP="/path/to/stop command"

case "$1" in
    start)
        $IMS_START
        $IWS_START
    ;;
    stop)
        $IMS_STOP
        $IWS_STOP
    ;;
    restart)
    $0 stop
    $0 start
    ;;
    *)
    echo "Usage: $0 {start|stop|restart}"
    exit 1
    ;;
esac
Set executable permission
Code:
chmod +X script
To start all server
Code:
./script start
To Stop all server
Code:
./script stop
Reply With Quote
  #4 (permalink)  
Old 04-27-2007, 10:17 PM
Junior Member
User
 
Join Date: Apr 2007
Posts: 7
Rep Power: 0
gthian is on a distinguished road
Default Modify

Btw, if i want to put it in crontab and set a time so that the server will start and stop at that particular time, which part to modify?
Reply With Quote
  #5 (permalink)  
Old 04-28-2007, 04:24 AM
raj raj is offline
Contributors
User
 
Join Date: Jun 2005
Location: Hyderabad
Posts: 151
Rep Power: 4
raj is on a distinguished road
Default

Quote:
Originally Posted by gthian View Post
Btw, if i want to put it in crontab and set a time so that the server will start and stop at that particular time, which part to modify?
no need to modify script, just setup cron job, eg stop work at 10 pm on weekdays:
Code:
0 22 * * 1-5    /path/to/above/script stop
Chkout FAQ section: How do I add jobs to cron under Linux or UNIX oses? | Frequently Asked Questions for more info
__________________
Raj
Linux rulz.
I have never turned back in my life ; I shall not do so today.. haha
Reply With Quote
  #6 (permalink)  
Old 04-28-2007, 03:05 PM
Junior Member
User
 
Join Date: Apr 2007
Posts: 7
Rep Power: 0
gthian is on a distinguished road
Default Thanks

Thank you very much. The scripts are running fine.
Reply With Quote
  #7 (permalink)  
Old 04-30-2007, 03:30 PM
Junior Member
User
 
Join Date: Apr 2007
Posts: 7
Rep Power: 0
gthian is on a distinguished road
Default Time

Hi,

There is one thing I would like to know. What if I want to start the IWS 10 mins after the IMS started? I need all the service started in the sequence 10 mins and all in one scripts.

Any idea? Please help =)
Reply With Quote
  #8 (permalink)  
Old 05-31-2007, 12:04 PM
Junior Member
User
 
Join Date: May 2007
Posts: 8
Rep Power: 0
NVRAM is on a distinguished road
Default

Quote:
Originally Posted by gthian View Post
There is one thing I would like to know. What if I want to start the IWS 10 mins after the IMS started? I need all the service started in the sequence 10 mins and all in one scripts.
You could try to add a second cron job 10 minutes later, or you could simply add sleep 600 between the steps.

Also, a few other thoughts:
  1. Return Codes - you should probably consider checking the return codes of the commands. Do you start the 2nd server if the 1st fails? Check this reference: http://www.tldp.org/LDP/abs/html/tests.html
  2. If a cron script has any output to stdout/stderr, it will be sent in an email to the user (if the MTA is set up properly). One common practice is to send all output to a temporary file: on failure "cat" the file; on success to either echo nothing or a short message (since failure for cron to run the job will not be indicated).
  3. Also, in the USA it is ill advised to schedule important cron jobs between 1AM-3AM, since on days of daylight savings changes your job may run twice or not at all, IIRC.
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 
Thread Tools
Display Modes

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads

Thread Thread Starter Forum Replies Last Post
crontab click007 Getting started tutorials 6 10-19-2007 05:45 PM
Crontab lacloai Linux software 2 06-01-2007 04:44 AM
Redhat Enterprise Linux RHEL - Start service on boot chimu Getting started tutorials 0 01-26-2007 08:29 PM
start up and stop the running script mala_un Shell scripting 9 07-26-2006 08:10 AM
ubuntu linux start, stop, restart networking toor Linux software 3 07-17-2006 12:32 PM


All times are GMT +5.5. The time now is 02:55 AM.


Powered by vBulletin® Version 3.7.4 - Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36