Unix/Linux Go Back    


UNIX for Dummies Questions & Answers This forum is closed for new posts. Please post beginner questions to learn unix and learn linux in this forum UNIX for Beginners Questions & Answers

Making a Script to Start as a Service

UNIX for Dummies Questions & Answers


 
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 01-22-2007
S.Vishwanath S.Vishwanath is offline
Registered User
 
Join Date: Jan 2001
Last Activity: 20 July 2008, 5:26 AM EDT
Posts: 48
Thanks: 0
Thanked 0 Times in 0 Posts
Making a Script to Start as a Service

Hi,

I have a shell script t1.sh. on my solaris box.

So, what are the steps required to make this script run as a Service, when the system re-starts. (for ex:- at run level 3).
I know that I should use the rc.d folders. But I don't know the exact steps.

Kindly explain,

Thanks in Advance.

Vishwa.
Sponsored Links
    #2  
Old Unix and Linux 01-23-2007
tayyabq8's Unix or Linux Image
tayyabq8 tayyabq8 is offline Forum Advisor  
Ex-Moderator
 
Join Date: Nov 2004
Last Activity: 31 January 2017, 1:29 AM EST
Location: Oman
Posts: 593
Thanks: 8
Thanked 12 Times in 5 Posts
Two ways to acheive this:

1) Put your shell commands in a startup file in /etc/rc2.d directly, keep in mind that files residing in /etc/rc2.d follow some rules i.e. filenames starting with S are startup scripts while starting with K are kill scripts, after S or K prefix there comes a 2 digit number like S10 or S65, it tells rc the sequence of startup scripts, so I'll suggest you to give it a name like S99xyz so that it runs in the last and doesn't disturb any other sequence and also is displayed at the bottom of the list of directory.

2)Second way is that create a symbolic link for your script suppose it is at path /home/t1.sh and /etc/rc2.d/S99t1.sh(Startup file)

Choose the best which suits your situation.

Regards,
Tayyab
Sponsored Links
    #3  
Old Unix and Linux 01-24-2007
sb008 sb008 is offline Forum Advisor  
Registered User
 
Join Date: Jan 2007
Last Activity: 12 January 2013, 3:37 AM EST
Posts: 398
Thanks: 1
Thanked 3 Times in 3 Posts
Quote:
Originally Posted by tayyabq8
Two ways to acheive this:

1) Put your shell commands in a startup file in /etc/rc2.d directly, keep in mind that files residing in /etc/rc2.d follow some rules i.e. filenames starting with S are startup scripts while starting with K are kill scripts, after S or K prefix there comes a 2 digit number like S10 or S65, it tells rc the sequence of startup scripts, so I'll suggest you to give it a name like S99xyz so that it runs in the last and doesn't disturb any other sequence and also is displayed at the bottom of the list of directory.

2)Second way is that create a symbolic link for your script suppose it is at path /home/t1.sh and /etc/rc2.d/S99t1.sh(Startup file)

Choose the best which suits your situation.

Regards,
Tayyab

Do not put the script in the /etc/rc2.d directory, but in the /etc/init.d directory.

In /etc/rc2.d you create a link to the script in /etc/init.d

The script t1.sh should like something like this.

#!/bin/sh
# it is common to write start stop scripts in the bourne shell
# for scripts which are started after all file systems are mounted it doesn't
# really matter. For scripts which are started before, you could end up with
# a problem if that shell is located on a filesystem different than "/"

case $1 in
start) # your code to start the service
;;
stop) # your code to stop the service
esac


or use functions

#!/usr/bin

do_start()
{
# your code to start the service
}

do_stop()
(
# your code to stop the service
}

case $i in
start) do_start
;;
stop) do_stop
;;
esac


Next you create a link in the /etc/rc2.d directory
ln -s /etc/init.d/t1.sh /etc/rc2.d/S99t1.sh

In general files in the /etc/rc<num>.d directory look like:
Snn<name>
Knn<name>

The S indicates the service should be started.
The K indicates the service should be stopped.

nn are 2 digits, and they indicate the order in which services are started or stopped.

Like indicated, 99 would be fine, that way your service is started after all other services.

<name> is usually the same as the name of the script in the /etc/init.d directory.

if /etc/rc2.d/S99t1.sh is linked to /etc/init.d/t1.sh the boot sequence will make sure that when entering run level 2 (that is why the link is in the rc2.d directory), the script /etc/init.d/t1.sh is started with the argument start.

Similar the boot sequence will make sure that all links in a rc<num>.d directory which start with a K will result in executing the script in the /etc/init.d directory they are linked to with as argument "stop" upon entering the run level <num>.

Depending on what kind of service your script will start it is proper to have a "kill" link as well.

Suppose the service is a database, you want the database to be shut down properly, when the system goes down.

To accomplish that you create a K01t1.sh link to /etc/init.d/t1.sh in /etc/rc1.d and/or rc0.d

The reason to put the script in the /etc/init.d directory and to create links in the /etc/rc<num>.d directory is that you do not want to have several copies of the same script in those directories. Instead you use links.

This way, if you want to change your script, you only need to change it in /etc/init.d

Too often it happens that people put the scripts themselves in several /etc/rc<num>.d directories. And after 2 years not even 2 of those copies are identical any more.

/etc/init.d and /etc/rc<num>.d should be used as they are intented to be used.




T
    #4  
Old Unix and Linux 02-08-2007
munna_dude munna_dude is offline
Registered User
 
Join Date: Jan 2007
Last Activity: 19 July 2013, 3:37 AM EDT
Posts: 50
Thanks: 0
Thanked 0 Times in 0 Posts
how

hi all

but how it is possible.
it is not working well.

my application is 'example'

how can i run this is in startup.

it contains the code

#!bin/sh
cd /bin
./example

i am using fedora4

can you please show me the way

thank you in advance
Sponsored Links
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Need help making a start/stop script... phpfreak UNIX for Dummies Questions & Answers 4 10-25-2009 08:28 AM
could not able to start the ssh service plz suggest girish.batra Solaris 1 05-28-2009 03:45 AM
Q: how to start a service when system start acerlinux AIX 4 04-03-2009 09:37 PM
Start a service as user potro Shell Programming and Scripting 5 04-17-2008 02:19 AM



All times are GMT -4. The time now is 07:15 AM.