• From time to time, people want me to create LINUX init scripts for them. I usually just take an existing one for another service and change it up to work for my new application, but most of them have become so long these days that I end up having to hack out a ton of code just to reduce them down to the very basic script I need. I decided to create this very simple template so I wouldn’t have to keep trimming down the more complex scripts that one tends to find in /etc/init.d these days.

    This script is chkconfig compatible, so call it the name of your new service and put it in /etc/init.d

    The chkconfig: 235 section indicates the the default runlevels. For instance, if we called this script /etc/init.d/new-service and ran chkconfig new-service on, it would be active in runlevels 2,3 and 5.

    The 98 and 55 numbers indicate the order of startup and kill. This means that using this tag, the startup symbolic link would be named S98new-service and the symbolic link to kill the process would be named K55new-service.

    #### SNIP ####

    #! /bin/sh
    # Basic support for IRIX style chkconfig
    ###
    # chkconfig: 235 98 55
    # description: Manages the services you are controlling with the chkconfig command
    ###
    
    case "$1" in
      start)
            echo -n "Starting new-service"
            #To run it as root:
            /path/to/command/to/start/new-service
            #Or to run it as some other user:
            /bin/su - username -c /path/to/command/to/start/new-service
            echo "."
            ;;
      stop)
            echo -n "Stopping new-service"
            #To run it as root:
            /path/to/command/to/stop/new-service
            #Or to run it as some other user:
            /bin/su - username -c /path/to/command/to/stop/new-service
            echo "."
            ;;
    
      *)
            echo "Usage: /sbin/service new-service {start|stop}"
            exit 1
    esac
    
    exit 0
    


    #### /SNIP ####

    Obviously change all instances of “new-service” to the name of your actual service… Enjoy!

    This entry was posted on Monday, July 23rd, 2007 at 5:22 pm and is filed under Data and Technology. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.
  • 2 Comments

    Take a look at some of the responses we've had to this article.

    1. [...] Example LINUX init Script | spiralbound.net Example LINUX init Script. Data and Technology. Posted on July 23rd, … startup, unix. From time to time, people want me to create LINUX init scripts for them. [...]

    2. One issue I have with the sample template you’ve provided is that unless you have a very good reason not to (as per the RedHat sysvinitfiles documentation), the end priority should always be equal to 100 – start priority. So if your start priority is 20, then your end priority should be 80.

  • Leave a Reply

    Let us know what you thought.

  • Name (required):

    Email (required):

    Website:

    Message:

Visitors have tagged this post: t (921) - Linux Startup Script (266) - centos init (116) - su example (116) - sample init script (86) - su examples (78) - rhel startup script (70) - linux service script (59) - init script example (50) - startup script Linux (48) - f (48) - centos init script (47) - init script chkconfig (45) - linux script (41) - chkconfig init (39) - chkconfig +init (35) - chkconfig init script (34) - linux script example (33) - linux script examples (33) - centos init scripts (32) - rhel startup scripts (31) - rhel init script (27) - centos startup (26) - RHEL init scripts (25) - centos startup script (25) - script linux (24) - example init script (23) - centos startup script (23) - foreach linux (23) - linux boot script (22) - linux init (21) - linux init script example (21) - linux script su (21) - centos startup scripts (20) - bash init script (20) - linux su example (20) - example of linux (20) - chkconfig init.d (19) - linux init script (19) - init script tutorial (19) - sample linux script (19) - init chkconfig (18) - linux foreach example (18) - linux scripts examples (18) - centos init script template (18) - linux startup scripts (18) - linux startup script example (17) - centos init script howto (17) - centos create init script (17) - centos add service (17) - linux foreach (16) - linux start script (16) - mss (15) - linux startup script howto (15) - init centos (15) - linux user startup script (14) - init.d tutorial (14) - start script linux (14) - linux script foreach (13) - linux run script as other user (13) - centos init.d (13) - chkconfig scripts (12) - chkconfig script (12) - linux add new service (12) - linux script location (12) - named init script (12) - chkconfig script example (12) - linux run script (12) - Linux Scripting examples (12) - linux execute script (12) - chkconfig examples (11) - centos create service (11) - startup script centos (11) - startup script in linux (11) - chkconfig startup script (11) - init script howto (11) - sample Linux startup script (11) - linux script sample (11) - init script sample (11) - init d script template (11) - service (10) - linux run script as user (10) - example-linux-init-script (10) - sample linux scripts (10) - scrip linux (10) - init script linux (10) - su linux example (10) - centos init d script (10) - centos init.d script (10) - example of a script (10) - all (9) - linux init scripts (9) - centos create startup script (9) - script linux example (9) - linux scrip (9) - bash script examples (9) - run script linux (9) - create startup script linux (9) - su command example (9) - chkconfig script howto (8) - chkconfig init scripts (8) - linux INIT.D (8) - linux initscript (8) - foreach linux script (8) - linux startup script path (8) - bash script example (8) - create chkconfig script (8) - centos add startup script (8) - startup scripts linux (8) - linux add service (8) - linux init d (8) - init.d script template (8) - chkconfig example (7) - simple linux startup script (7) - linux init.d script (7) - linux runscript (7) - linux service startup script (7) - linux startup script chkconfig (7) - linux service scripts (7) - rhel init script template (7) - spiralbound net (7) - startup script example (7) - create chkconfig (7) - add new service in linux (7) - #! script linux centos (7) - execute script linux (7) - linux script comment ## (7) - rhel4 startup script (7) - su linux script (7) - centos init d (7) - centos add new service (7) - service script linux (7) - init script examples (7) - init script template (7) - chkconfig tutorial (6) - how to create a linux startup script (6) - centos run script at boot (6) - linux init 7 (6) - bash startup script (6) - create linux service (6) - creating a script in linux (6) - startup script + chkconfig (6) - init script user (6) - bash init scripts (6) - example linux script (6) - linux script user (6) - foreach linux example (6) - run script in linux (6) - init script centos (6) - linux script comments (6) -