How can I create a service in CentOS 6?

37,427

I'll provide the basic service script but anyone is invited to improve my answer.

This is for CentOS 6.10

The service scripts are placed in this directory: /etc/rc.d/init.d

There's a template provided by the system, open the file with your editor of choice (example vi): vi /usr/share/doc/initscripts-*/sysvinitfiles

Here's a simple example of my kafka script:

#!/bin/bash
#/etc/rc.d/init.d/kafka

# Source function library.
. /etc/init.d/functions


start() {
        echo -n "Starting kafka... "
        nohup /home/kafka/kafka/bin/kafka-server-start.sh /home/kafka/kafka/config/server.properties &
        touch /var/lock/subsys/kafka
        return 0
}

stop() {
        echo -n "Shutting down kafka... "
        nohup /home/kafka/kafka/bin/kafka-server-stop.sh &
        rm -f /var/lock/subsys/kafka
        return 0
}

case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    status)
        ;;
    restart)
        stop
        start
        ;;
    reload)
        ;;
    *)
        echo "Usage: kafka {start|stop|status|reload|restart}"
 exit 1
        ;;
esac
exit $?

Based on this kafka example, this file should be saved as /etc/rc.d/init.d/kafka

I run it by typing: service kafka start

Share:
37,427

Related videos on Youtube

Faryal Gohar
Author by

Faryal Gohar

Updated on September 18, 2022

Comments

  • Faryal Gohar
    Faryal Gohar almost 2 years

    I need to create ratchet service on my dedicated server (CentOS 6). I have successfully created this service on local CentOS 7. The problem is that the server has got old version of CentOS and I need to create a service there.

    I have taken the following steps to create a service in CentOS 7:

    Created a file named ratchet.service in /etc/systemd/system/ and put the following in it:

    [Unit]
    Description=Ratchet Service
    After=network.target
    
    [Service]
    Type=simple
    ExecStart=/usr/bin/php -f /path/to/ratchet/server$
    Restart=on-abort
    
    [Install]
    WantedBy=multi-user.target
    

    To start:

    service ratchet start
    

    Can anyone please advise how can this be achieved in CentOS 6?

    UPDATE: What i am trying to achieve is to run Ratchet Service written in php.I want to run this service on Centos 6 so that it can run persistently on my server. I need to execute server file script (named above as /path/to/ratchet/server.php).

    I am following this tutorial and code : https://github.com/amirsanni/Video-Call-App

    • Kamil Maciorowski
      Kamil Maciorowski over 6 years
      Init system in your Centos 6 is probably not systemd and you need to work with /etc/init.d/ instead of systemd units. Please study How to find out if a system uses SysV, Upstart or Systemd initsystem, Detect init system using the shell and edit your question to tell us what it is.
    • Faryal Gohar
      Faryal Gohar over 6 years
      Thanks @KamilMaciorowski Yes.i understand i would need to use /etc/init.d .My question was how can i write the above service in init.d in centos 6?
    • munjal
      munjal over 5 years
      @FaryalGohar did you found the answer of this question? I also need this service in CENTOS 6.