[1108] | 1 | Writing System V init scripts for Red Hat Linux |
---|
| 2 | =============================================== |
---|
| 3 | |
---|
| 4 | All System V init scripts are named /etc/rc.d/init.d/<servicename> |
---|
| 5 | where <servicename> is the name of the service. There must be no |
---|
| 6 | ".init" suffix. |
---|
| 7 | |
---|
| 8 | This path will very likely be moved to /etc/init.d in the future. |
---|
| 9 | Once Red Hat Linux 7.0 is installed, you can access scripts as |
---|
| 10 | /etc/init.d/<servicename>, via symlinks. |
---|
| 11 | |
---|
| 12 | Sample Script |
---|
| 13 | ============= |
---|
| 14 | |
---|
| 15 | #!/bin/bash |
---|
| 16 | # |
---|
| 17 | # /etc/rc.d/init.d/<servicename> |
---|
| 18 | # |
---|
| 19 | # <description of the *service*> |
---|
| 20 | # <any general comments about this init script> |
---|
| 21 | # |
---|
| 22 | # <tags -- see below for tag definitions. *Every line* from the top |
---|
| 23 | # of the file to the end of the tags section must begin with a # |
---|
| 24 | # character. After the tags section, there should be a blank line. |
---|
| 25 | # This keeps normal comments in the rest of the file from being |
---|
| 26 | # mistaken for tags, should they happen to fit the pattern.> |
---|
| 27 | |
---|
| 28 | # Source function library. |
---|
| 29 | . /etc/rc.d/init.d/functions |
---|
| 30 | |
---|
| 31 | <define any local shell functions used by the code that follows> |
---|
| 32 | |
---|
| 33 | start() { |
---|
| 34 | echo -n "Starting <servicename>: " |
---|
| 35 | <start daemons, perhaps with the daemon function> |
---|
| 36 | touch /var/lock/subsys/<servicename> |
---|
| 37 | return <return code of starting daemon> |
---|
| 38 | } |
---|
| 39 | |
---|
| 40 | stop() { |
---|
| 41 | echo -n "Shutting down <servicename>: " |
---|
| 42 | <stop daemons, perhaps with the killproc function> |
---|
| 43 | rm -f /var/lock/subsys/<servicename> |
---|
| 44 | return <return code of stopping daemon> |
---|
| 45 | } |
---|
| 46 | |
---|
| 47 | case "$1" in |
---|
| 48 | start) |
---|
| 49 | start |
---|
| 50 | ;; |
---|
| 51 | stop) |
---|
| 52 | stop |
---|
| 53 | ;; |
---|
| 54 | status) |
---|
| 55 | <report the status of the daemons in free-form format, |
---|
| 56 | perhaps with the status function> |
---|
| 57 | ;; |
---|
| 58 | restart) |
---|
| 59 | stop |
---|
| 60 | start |
---|
| 61 | ;; |
---|
| 62 | reload) |
---|
| 63 | <cause the service configuration to be reread, either with |
---|
| 64 | kill -HUP or by restarting the daemons, in a manner similar |
---|
| 65 | to restart above> |
---|
| 66 | ;; |
---|
| 67 | condrestart) |
---|
| 68 | <Restarts the servce if it is already running. For example:> |
---|
| 69 | [ -f /var/lock/subsys/<service> ] && restart || : |
---|
| 70 | probe) |
---|
| 71 | <optional. If it exists, then it should determine whether |
---|
| 72 | or not the service needs to be restarted or reloaded (or |
---|
| 73 | whatever) in order to activate any changes in the configuration |
---|
| 74 | scripts. It should print out a list of commands to give to |
---|
| 75 | $0; see the description under the probe tag below.> |
---|
| 76 | ;; |
---|
| 77 | *) |
---|
| 78 | echo "Usage: <servicename> {start|stop|status|reload|restart[|probe]" |
---|
| 79 | exit 1 |
---|
| 80 | ;; |
---|
| 81 | esac |
---|
| 82 | exit $? |
---|
| 83 | |
---|
| 84 | Notes: |
---|
| 85 | |
---|
| 86 | - The restart and reload functions may be (and commonly are) |
---|
| 87 | combined into one test, vis: |
---|
| 88 | restart|reload) |
---|
| 89 | - You are not prohibited from adding other commands; list all commands |
---|
| 90 | which you intend to be used interactively to the usage message. |
---|
| 91 | - Notice the change in that stop() and start() are now shell functions. |
---|
| 92 | This means that restart can be implemented as |
---|
| 93 | stop |
---|
| 94 | start |
---|
| 95 | instead of |
---|
| 96 | $0 stop |
---|
| 97 | $0 start |
---|
| 98 | This saves a few shell invocations. |
---|
| 99 | |
---|
| 100 | Functions in /etc/rc.d/init.d/functions |
---|
| 101 | ======================================= |
---|
| 102 | |
---|
| 103 | daemon [ --check <name> ] [ --user <username>] |
---|
| 104 | [+/-nicelevel] program [arguments] [&] |
---|
| 105 | |
---|
| 106 | Starts a daemon, if it is not already running. Does |
---|
| 107 | other useful things like keeping the daemon from dumping |
---|
| 108 | core if it terminates unexpectedly. |
---|
| 109 | |
---|
| 110 | --check <name>: |
---|
| 111 | Check that <name> is running, as opposed to simply the |
---|
| 112 | first argument passed to daemon(). |
---|
| 113 | --user <username>: |
---|
| 114 | Run command as user <username> |
---|
| 115 | |
---|
| 116 | killproc program [signal] |
---|
| 117 | |
---|
| 118 | Sends a signal to the program; by default it sends a SIGTERM, |
---|
| 119 | and if the process doesn't die, it sends a SIGKILL a few |
---|
| 120 | seconds later. |
---|
| 121 | |
---|
| 122 | It also tries to remove the pidfile, if it finds one. |
---|
| 123 | |
---|
| 124 | pidofproc program |
---|
| 125 | |
---|
| 126 | Tries to find the pid of a program; checking likely pidfiles, |
---|
| 127 | and using the pidof program. Used mainly from within other |
---|
| 128 | functions in this file, but also available to scripts. |
---|
| 129 | |
---|
| 130 | status program |
---|
| 131 | |
---|
| 132 | Prints status information. Assumes that the program name is |
---|
| 133 | the same as the servicename. |
---|
| 134 | |
---|
| 135 | |
---|
| 136 | Tags |
---|
| 137 | ==== |
---|
| 138 | |
---|
| 139 | # chkconfig: <startlevellist> <startpriority> <endpriority> |
---|
| 140 | |
---|
| 141 | Required. <startlevellist> is a list of levels in which |
---|
| 142 | the service should be started by default. <startpriority> |
---|
| 143 | and <endpriority> are priority numbers. For example: |
---|
| 144 | # chkconfig: 2345 20 80 |
---|
| 145 | Read 'man chkconfig' for more information. |
---|
| 146 | |
---|
| 147 | Unless there is a VERY GOOD, EXPLICIT reason to the |
---|
| 148 | contrary, the <endpriority> should be equal to |
---|
| 149 | 100 - <startpriority> |
---|
| 150 | |
---|
| 151 | # description: <multi-line description of service> |
---|
| 152 | |
---|
| 153 | Required. Several lines of description, continued with '\' |
---|
| 154 | characters. The initial comment and following whitespace |
---|
| 155 | on the following lines is ignored. |
---|
| 156 | |
---|
| 157 | # description[ln]: <multi-line description of service in the language \ |
---|
| 158 | # ln, whatever that is> |
---|
| 159 | |
---|
| 160 | Optional. Should be the description translated into the |
---|
| 161 | specified language. |
---|
| 162 | |
---|
| 163 | # processname: |
---|
| 164 | |
---|
| 165 | Optional, multiple entries allowed. For each process name |
---|
| 166 | started by the script, there should be a processname entry. |
---|
| 167 | For example, the samba service starts two daemons: |
---|
| 168 | # processname: smdb |
---|
| 169 | # processname: nmdb |
---|
| 170 | |
---|
| 171 | # config: |
---|
| 172 | |
---|
| 173 | Optional, multiple entries allowed. For each static config |
---|
| 174 | file used by the daemon, use a single entry. For example: |
---|
| 175 | # config: /etc/httpd/conf/httpd.conf |
---|
| 176 | # config: /etc/httpd/conf/srm.conf |
---|
| 177 | |
---|
| 178 | Optionally, if the server will automatically reload the config |
---|
| 179 | file if it is changed, you can append the word "autoreload" to |
---|
| 180 | the line: |
---|
| 181 | # config: /etc/foobar.conf autoreload |
---|
| 182 | |
---|
| 183 | # pidfile: |
---|
| 184 | |
---|
| 185 | Optional, multiple entries allowed. Use just like the config |
---|
| 186 | entry, except that it points at pidfiles. It is assumed that |
---|
| 187 | the pidfiles are only updated at process creation time, and |
---|
| 188 | not later. The first line of this file should be the ASCII |
---|
| 189 | representation of the PID; a terminating newline is optional. |
---|
| 190 | Any lines other than the first line are not examined. |
---|
| 191 | |
---|
| 192 | # probe: true |
---|
| 193 | |
---|
| 194 | Optional, used IN PLACE of processname, config, and pidfile. |
---|
| 195 | If it exists, then a proper reload-if-necessary cycle may be |
---|
| 196 | acheived by running these commands: |
---|
| 197 | |
---|
| 198 | command=$(/etc/rc.d/init.d/SCRIPT probe) |
---|
| 199 | [ -n "$command" ] && /etc/rc.d/init.d/SCRIPT $command |
---|
| 200 | |
---|
| 201 | where SCRIPT is the name of the service's sysv init script. |
---|
| 202 | |
---|
| 203 | Scripts that need to do complex processing could, as an |
---|
| 204 | example, return "run /var/tmp/<servicename.probe.$$" |
---|
| 205 | and implement a "run" command which would execute the |
---|
| 206 | named script and then remove it. |
---|
| 207 | |
---|
| 208 | Note that the probe command should simply "exit 0" if nothing |
---|
| 209 | needs to be done to bring the service into sync with its |
---|
| 210 | configuration files. |
---|
| 211 | |
---|
| 212 | Copyright (c) 2000 Red Hat Software, Inc. |
---|