0

I am starting a service (httpd) by using this command:

/etc/init.d/'name of service' start

How can I start a service by using the command below, if service keyword is not configure for above httpd service?

2)service 'nameofservice' start e.g: service httpd start

How can I configure a service which can start and stop by using service keyword i.e: "service 'nameofservice' start" (service keyword like in option 2) instead of /etc/init.d/nameofservice?

2 Answers 2

1

The service(8) command looks for a script in /etc/init.d. If no such script exists, you may need to write your own. On the web you can find guides that will help you do that.

0
0

Below script tested in Centos 5. We will create a script that print the current date and time and output directed in to log file and save with name timed.

vim /opt/timed     


 #!/bin/bash 
while true;do
      echo  `date` >> /tmp/timed.log 
done #script finish here below line enable execute permission of script

 chmod +x /opt/timed

Now we will write System V script to start and stop the timed script.

vim /etc/init.d/time (save the script only in /etc/init.d directory only with the name of your choice we use name time here)


  #!/bin/bash  
  # chkconfig: 345 80 20      
  # description: startup script for /opt/timed daemon  
start() {  
nohup /opt/timed &  
}  
stop() {  
pkill timed  
}  
case "$1" in  
             start)   
                   start  #calling the start () function  
             ;;  
             stop)  
                   stop # calling the stop() function
             ;;  
             *)
                   echo "Usage: $0 {start|stop}"  
                   RETVAL=1 #we return the value 1 bcz cmd is not sucessfull  
  esac
  exit 0



 chmod +x /etc/init.d/time  (enabling the execute permission of script)
 service time start    (it will start the script timed)
 ps –ef | grep timed (we can check that script is running with this command)

Explanation of Script

Time script must be in the /etc/init.d directory. chkconfig: 345 80 20 is necessary component of script 345 represent the 3,4 & 5 runlevels. 20 means start command will have the number 20 (S20) in /etc/rc3/ directory. 80 means stop command will have the number 80 (k80) in /etc/rc3/ directory.

start() and stop() are functions for starting and stopping the daemon. When you execute a Unix job in the background (using &, bg command), and logout from the session, your process will get killed. You can avoid this using several methods — executing the job with nohup, or making it as batch job using at, batch or cron command. The PKill command allows you to kill a program simply by specifying the name. $1 takes the first argument. $0 means name of the script. RETVAL is the environment variable that is like exit status of the script if it is 0 script is run successfully and 1 means script not run successfully. If we specify the command other than start or stop then the usage message is printed.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .