Creating script in rc.d

Hi,

I have created customized scripts to start httpd and postgres (For CentOS 6) in /etc/init.d. However for it to work even after reboot, I have to put the script in /etc/rc.d/rc0.d, rc1.d, etc.

[root@dev3-pdx rc.d]# ls -lrt
total 60
-rwxr-xr-x  1 root root 20199 Oct  4  2017 rc.sysinit
-rwxr-xr-x  1 root root   220 Oct  4  2017 rc.local
-rwxr-xr-x  1 root root  2617 Oct  4  2017 rc
drwxr-xr-x. 2 root root  4096 May 14  2019 rc1.d
drwxr-xr-x. 2 root root  4096 May 14  2019 rc0.d
drwxr-xr-x. 2 root root  4096 May 14  2019 rc6.d
drwxr-xr-x. 2 root root  4096 May 14  2019 rc2.d
drwxr-xr-x. 2 root root  4096 May 14  2019 rc5.d
drwxr-xr-x. 2 root root  4096 May 14  2019 rc4.d
drwxr-xr-x. 2 root root  4096 May 14  2019 rc3.d
drwxr-xr-x. 2 root root  4096 Jan 15 11:49 init.d
[root@dev3-pdx rc.d]#

However, the naming convention for the scripts, for eg, in the directory rc0.d is like this :

[root@dev3-pdx rc0.d]# ls
K01certmonger  K10cups               K15svnserve        K50kdump       K73winbind           K75quota_nld       K84wpa_supplicant  K88rsyslog      K92iptables      K99rngd
K01smartd      K10psacct             K16abrt-ccpp       K50xinetd      K74acpid             K75udev-post       K85mdmonitor       K88sssd         K92pppoe-server  K99sysstat
K02oddjobd     K10saslauthd          K16abrtd           K60crond       K74haldaemon         K76ypbind          K85messagebus      K89netconsole   K95firstboot     S00killall
K03rhnsd       K15htcacheclean       K25sshd            K60nfs         K74ntpd              K83bluetooth       K87irqbalance      K89portreserve  K95rdma          S01halt
K04osad        K15htcacheclean-myeg  K30postfix         K61nfs-rdma    K75blk-availability  K83nfslock         K87restorecond     K89rdisc        K99cpuspeed
K05atd         K15httpd              K30spice-vdagentd  K69rpcsvcgssd  K75netfs             K83rpcgssd         K87rpcbind         K90network      K99lvm2-monitor
K05wdaemon     K15httpd-myeg         K50dnsmasq         K72autofs      K75ntpdate           K84NetworkManager  K88auditd          K92ip6tables    K99oscap-scan
[root@dev3-pdx rc0.d]#

How do I know what number to put for my script? Like K16.....etc.

Any reason why "not" to put your scripts in rc.local ?

Why did you choose rc0.d for your custom scripts?

But rc.local is not a directory. It's a file. How do I put my script there?

rc.local is for compatibility with BSD Unix, not well supported in Linux :frowning:

In your start/stop script reate a dependency header - comments with certain keywords that belong to the LSB (Linux Standard Base).
Then put it in the init.d directory so it is seen by the chkconfig command.

chkonfig -add myscript
chkonfig --list
chkonfig myscript on
chkconfig --list

The on command creates suitable S and K numbers for the run levels, trying to fulfill the dependency in the LSB header.

Thanks for the information.

This is my script :

#!/bin/bash
#
# httpd        Startup script for the Apache HTTP Server
#
# chkconfig: - 85 15
# description: The Apache HTTP Server is an efficient and extensible  \
#              server implementing the current HTTP standards.
# processname: httpd
# config: /etc/httpd/conf/httpd.conf
# config: /etc/sysconfig/httpd
# pidfile: /var/run/httpd/httpd.pid
#
### BEGIN INIT INFO
# Provides: httpd
# Required-Start: $local_fs $remote_fs $network $named
# Required-Stop: $local_fs $remote_fs $network
# Should-Start: distcache
# Short-Description: start and stop Apache HTTP Server
# Description: The Apache HTTP Server is an extensible server
#  implementing the current HTTP standards.
### END INIT INFO

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

if [ -f /etc/sysconfig/httpd ]; then
        . /etc/sysconfig/httpd
fi

# Start httpd in the C locale by default.
HTTPD_LANG=${HTTPD_LANG-"C"}

# This will prevent initlog from swallowing up a pass-phrase prompt if
# mod_ssl needs a pass-phrase from the user.
INITLOG_ARGS=""

# Set HTTPD=/usr/sbin/httpd.worker in /etc/sysconfig/httpd to use a server
# with the thread-based "worker" MPM; BE WARNED that some modules may not
# work correctly with a thread-based MPM; notably PHP will refuse to start.

# Path to the apachectl script, server binary, and short-form for messages.
apachectl=/usr/local/apache29-PDRM/bin/apachectl
httpd=${HTTPD-/usr/local/apache29-PDRM/bin/httpd}
prog=httpd
pidfile=${PIDFILE-/usr/local/apache29-PDRM/logs/httpd.pid}
lockfile=${LOCKFILE-/var/lock/subsys/httpd}
RETVAL=0
STOP_TIMEOUT=${STOP_TIMEOUT-10}

# The semantics of these two functions differ from the way apachectl does
# things -- attempting to start while running is a failure, and shutdown
# when not running is also a failure.  So we just do it the way init scripts
# are expected to behave here.
start() {
        echo -n $"Starting $prog: "
        LANG=$HTTPD_LANG daemon --pidfile=${pidfile} $httpd $OPTIONS
        RETVAL=$?
        echo
        [ $RETVAL = 0 ] && touch ${lockfile}
        return $RETVAL
}

# When stopping httpd, a delay (of default 10 second) is required
# before SIGKILLing the httpd parent; this gives enough time for the
# httpd parent to SIGKILL any errant children.
stop() {
        echo -n $"Stopping $prog: "
        killproc -p ${pidfile} -d ${STOP_TIMEOUT} $httpd
        RETVAL=$?
        echo
        [ $RETVAL = 0 ] && rm -f ${lockfile} ${pidfile}
}
reload() {
    echo -n $"Reloading $prog: "
    if ! LANG=$HTTPD_LANG $httpd $OPTIONS -t >&/dev/null; then
        RETVAL=6
        echo $"not reloading due to configuration syntax error"
        failure $"not reloading $httpd due to configuration syntax error"
    else
        # Force LSB behaviour from killproc
        LSB=1 killproc -p ${pidfile} $httpd -HUP
        RETVAL=$?
        if [ $RETVAL -eq 7 ]; then
            failure $"httpd shutdown"
        fi
    fi
    echo
}

# See how we were called.
case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  status)
        status -p ${pidfile} $httpd
        RETVAL=$?
        ;;
  restart)
        stop
        start
        ;;
  condrestart|try-restart)
        if status -p ${pidfile} $httpd >&/dev/null; then
                stop
                start
        fi
        ;;
  force-reload|reload)
        reload
        ;;
  graceful|help|configtest|fullstatus)
        $apachectl $@
        RETVAL=$?
        ;;
  *)
        echo $"Usage: $prog {start|stop|restart|condrestart|try-restart|force-reload|reload|status|fullstatus|graceful|help|configtest}"
        RETVAL=2
esac

exit $RETVAL

I believe this script already has the dependency headers, and thus if I run the commands

#chkconfig --add myscript

and

#chkconfig myscript on

it should be enabled during boot?

Yes, now you can see the respective links in the /etc/rc.d/rc?d/ directories.
First you can test your script with e.g.

service myscript stop
service myscript start

And if this works fine, do a system reboot to test that it auto-starts okay.

Many people, use rc.local on Linux. I don't see any benefit in "trashing it" as "not well supported". Also, this file (rc.local) does not require any special "support" since it is not a part of some broader framework. It's simple and works. It's a flat file which is executed at a particular time in the boot sequence.

Also, rc.local is not designed to require a lot of support (to my knowledge), it is simply a place where system admins can put startup scripts they want to start after the other scripts run:

On some versions of Linux, the default

linux #cat rc.local

used to be:

#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.

exit 0

In addition to the above, for completeness, if you search Google "rc.local" with Linux keywords you get around 833,000 results.

I did not find any "memos" (quick scan) where people were saying "rc.local" is not "well supported".... What support does it need? It's a flat file designed to be edited run as described. How do you "support" a flat file executed at the end of each multiuser runlevel?

Why does it need "support? Inquiring minds want to know :slight_smile:

OBTW, on my ubuntu distro, it's blank waiting for someone to add what they want to it:

ubuntu:/etc# ls -l rc*
-rwxr-xr-x 1 root root    0 Sep  5  2018 rc.local

rc0.d:
total 0
lrwxrwxrwx 1 root root 17 Sep  5  2018 K01apache2 -> ../init.d/apache2
lrwxrwxrwx 1 root root 29 Sep  5  2018 K01apache-htcacheclean -> ../init.d/apache-htcacheclean
lrwxrwxrwx 1 root root 18 Sep 26 07:02 K01fail2ban -> ../init.d/fail2ban
lrwxrwxrwx 1 root root 20 Sep  5  2018 K01irqbalance -> ../init.d/irqbalance
lrwxrwxrwx 1 root root 15 Sep  5  2018 K01mdadm -> ../init.d/mdadm
lrwxrwxrwx 1 root root 24 Sep  5  2018 K01mdadm-waitidle -> ../init.d/mdadm-waitidle
lrwxrwxrwx 1 root root 19 Sep  5  2018 K01memcached -> ../init.d/memcached
lrwxrwxrwx 1 root root 15 Sep  5  2018 K01mysql -> ../init.d/mysql
lrwxrwxrwx 1 root root 18 Dec 22 08:26 K01onioncat -> ../init.d/onioncat
lrwxrwxrwx 1 root root 18 Sep  5  2018 K01plymouth -> ../init.d/plymouth
lrwxrwxrwx 1 root root 17 Sep  5  2018 K01postfix -> ../init.d/postfix
lrwxrwxrwx 1 root root 17 Sep  5  2018 K01rsyslog -> ../init.d/rsyslog
lrwxrwxrwx 1 root root 13 Dec 22 08:26 K01tor -> ../init.d/tor
lrwxrwxrwx 1 root root 29 Sep  5  2018 K01unattended-upgrades -> ../init.d/unattended-upgrades
lrwxrwxrwx 1 root root 15 Sep  5  2018 K01uuidd -> ../init.d/uuidd

(truncated)

In fact, all my ubuntu distributions have rc.local available as a "blank" for sys admins who want to use it. It seems "supported to me" since it is there and blank for sys admins to edit and use.

:confused: :confused: