Creating script in rc.d

Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Creating script in rc.d
# 1  
Old 01-15-2020
Creating script in rc.d


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.
# 2  
Old 01-15-2020
Any reason why "not" to put your scripts in rc.local ?

Why did you choose rc0.d for your custom scripts?
# 3  
Old 01-15-2020
But rc.local is not a directory. It's a file. How do I put my script there?
# 4  
Old 01-15-2020
rc.local is for compatibility with BSD Unix, not well supported in Linux Smilie

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.
# 5  
Old 01-15-2020
Thanks for the information.

This is my script :

# 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/
# 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.

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

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

# Start httpd in the C locale by default.

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

# 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.

# 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 = 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 = 0 ] && rm -f ${lockfile} ${pidfile}
reload() {
    echo -n $"Reloading $prog: "
    if ! LANG=$HTTPD_LANG $httpd $OPTIONS -t >&/dev/null; then
        echo $"not reloading due to configuration syntax error"
        failure $"not reloading $httpd due to configuration syntax error"
        # Force LSB behaviour from killproc
        LSB=1 killproc -p ${pidfile} $httpd -HUP
        if [ $RETVAL -eq 7 ]; then
            failure $"httpd shutdown"

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

exit $RETVAL

I believe this script already has the dependency headers, and thus if I run the commands
#chkconfig --add myscript

#chkconfig myscript on

it should be enabled during boot?
# 6  
Old 01-15-2020
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.
# 7  
Old 01-15-2020
Originally Posted by MadeInGermany
rc.local is for compatibility with BSD Unix, not well supported in Linux Smilie
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:

The script /etc/rc.local is for use by the system administrator. It is traditionally executed after all the normal system services are started, at the end of the process of switching to a multiuser runlevel. You might use it to start a custom service, for example a server that's installed in /usr/local. Most installations don't need /etc/rc.local, it's provided for the minority of cases where it's needed
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.

Creating script in rc.d-screen-shot-2020-01-15-71513-pmjpg

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 Smilie

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

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


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.

Smilie Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help with creating a script

Hi everyone, I am completely new to this forum and I have some questions regarding a script I am writing. I would be happy if anyone could help me with the small and precise script which should include if, then, else, while until, case and select. The scenario is as follows: 1) A user... (3 Replies)
Discussion started by: codenotfound
3 Replies

2. Shell Programming and Scripting

Creating IN list in PLSQL script dynamically by using shell script

Hi all, I have a PLSQL script which has a IN list where it takes some ids as input. For example SELECT * FROM EMPLOYEE WHERE EMPLOYEE_ID IN (comma separated list ) I want to run this quest inside a shell script but I would like to prepare the IN list dynamically where the employee ids... (1 Reply)
Discussion started by: LoneRanger
1 Replies

3. Shell Programming and Scripting

Need help in creating file restoration script from a backup script.

Hi all i am struggling in creating a restore of env files while doing applications clone. the first file i created for copying the important configurations file which is running perfect now for reverting the changes i mean when i am restoring these files to its original places i have to do... (7 Replies)
Discussion started by: javeedkaleem
7 Replies

4. UNIX for Dummies Questions & Answers

Creating a script

Alright, well I did some more research since I originally posted this thread, and as much as I'd like to delete it, I can't, so I'll just extend my initial question a little. Right now I have 3 scripts: 1#!/bin/bash # script1 - Write all files modfied x days ago find .. -daystart -mtime 0... (2 Replies)
Discussion started by: Aussiemick
2 Replies

5. Programming

need help with creating a sh script

Hi everyone I’m not a programmer and my knowledge of scripting is very poor, now I’m stock in a task at work and would really appreciate it if someone could help me out. Here is the problem: 1. I have a file with 9 million entries that look like this : 611424167 610864581 611881523 609585386... (3 Replies)
Discussion started by: hiker1064
3 Replies

6. Shell Programming and Scripting

help needed with creating challenging bash script with creating directories

Hi, Can someone help me with creating a bash shell script. I need to create a script that gets a positive number n as an argument. The script must create n directories in the current directory with names like map_1, map_2 etcetera. Each directory must be contained within its predecessor. So... (7 Replies)
Discussion started by: I-1
7 Replies

7. Shell Programming and Scripting

(Urgent):Creating flat file using sql script and sqlplus from UNIX Shell Script

Hi, I need help urgently for following issue. Pls help me to resolve this issue. I am calling sql script file(file1.sql) from UNIX Shell Script(script1.ksh) using sql plus and trying to create flat file that contains all records returned from SQL query in SQL script(file1.sql) I given... (6 Replies)
Discussion started by: praka
6 Replies

8. Shell Programming and Scripting

Need help creating a script

I need to automate the following process: I have a list of ip address for printers in a file called iplist.txt, I need to take that list and run the command snmpget -v 1 -c public ip address sysName.0 for each ip address to see if the printer is running snmp, I want to the create a file... (4 Replies)
Discussion started by: inLine6
4 Replies

9. UNIX for Dummies Questions & Answers

creating a script

I am trying to create a application in OSX through UNIX that will run a script to mount an image from a CD-ROM and run the application which it corresponds to, all with double clicking on a icon in OSX. Any thoughts or ideas? -Mad (3 Replies)
Discussion started by: madknowledge
3 Replies

10. Programming

creating a new C script

All right. Heres the deal, I need to know everysingle command or funtion there is to create a new c file (file.c). Heres the catch: I cannot use text editors!!!:mad: I heard of a "gcc" command is that any good?:confused: Thanks..:cool: (2 Replies)
Discussion started by: AbRa-KaDabRa
2 Replies
Login or Register to Ask a Question

Featured Tech Videos