Networking

Unix and Linux network configuration. Multiple network interfaces. Bridged NICs. High-availability network configurations.

Applications

Reviews of latest Unix and Linux software. Helpful tips for application support admins. Automating application support.

Data

Disk partitioning, filesystems, directories, and files. Volume management, logical volumes, HA filesystems. Backups and disaster recovery.

Monitoring

Distributed server monitoring. Server performance and capacity planning. Monitoring applications, network status and user activity.

Commands & Shells

Cool Unix shell commands and options. Command-line tools and application. Things every Unix sysadmin needs to know.

Home » Commands & Shells, Featured

Writing a Linux Startup Script

Submitted by on December 16, 2011 – 2:11 am 11 Comments

As basic as the task of creating a startup script may sound, even experienced sysadmins sometimes run into problems with having services start at the right time during the boot process or stop during the shutdown. Two major reasons for this: the procedure is a bit convoluted due to linking. Also, writing startup scripts is not something you have to do very often these days. So a quick overview of the bootup process is in order. We will use RHEL 5.5 in this example, but the structure and process are nearly identical for most Linux flavors and versions.

Runlevels

Linux uses System V-style initialization with seven runlevels – modes of the OS operation. These are:

0 – runlevel 0 is used for shutdown. Entering “init 0” at the command prompt as root will cause the system to shut down and, in most cases, to power off.

1 – this runlevel is also known as the single-user mode. It is used primarily for troubleshooting. Only the most fundamental services are started. You will have access to all locally-attached storage. Network services are disabled, so this mode can only be used if you have direct access to the system console.

2 – similar to level 1, this runlevel is used mainly for troubleshooting. Networking is enabled and multiple users can log in. However, network file-sharing services like NFS or SMB are disabled. This mode can be useful for troubleshooting systems that hang during bootup on trying to mount remote filesystems.

3 – this runlevel is most commonly used by Unix servers. It starts all services with the exception of X-Windows. Multiple users can connect to the system, but only from command line. There is no support for GUIs.

4 – runlevel 4 is rarely used. It usually includes everything from runlevel 3, plus some custom services than may be needed for development or testing purposes.

5 – this runlevel is used by personal computers and some servers. It offers the X-Windows service and GUI desktop environment.

6 – runlevel 6 is used to reboot the system. Typing “init 6” at the command prompt as root is usually equivalent to typing “reboot”.

The default runlevel is specified in the /etc/inittab file. In the case of our test system, the default runlevel is 3, as shown below:

[root@deathstar]# grep initdefault /etc/inittab | grep -v "#"
id:3:initdefault:

A common mistake: when sysadmins add service startup and shutdown scripts, they usually concentrate on runlevels 3 and 5, forgetting to include runlevels 0 and 6 (shutdown and reboot). As a result, services are not properly shut down when the system is being powered down.

Init Directories and Files

Startup/shutdown scripts are located in /etc/rc.d/init.d

There are seven other sub-directories under /etc/rc.d

[root@deathstar]# ls /etc/rc.d | grep rc..d
rc0.d
rc1.d
rc2.d
rc3.d
rc4.d
rc5.d
rc6.d

Each of these directories contain soft links to the startup/shutdown scripts located in /etc/rc.d/init.d

[root@deathstar]# ls -als /etc/rc.d/rc3.d

8 drwxr-xr-x  2 root root 4096 Dec 15 16:03 .
8 drwxr-xr-x 10 root root 4096 Sep 26 00:29 ..
4 lrwxrwxrwx  1 root root   17 Sep 25 23:48 K00ipmievd -> ../init.d/ipmievd
4 lrwxrwxrwx  1 root root   17 Sep 25 23:51 K05wdaemon -> ../init.d/wdaemon
4 lrwxrwxrwx  1 root root   14 Sep 26 00:28 K10cups -> ../init.d/cups
...
4 lrwxrwxrwx  1 root root   22 Sep 25 23:54 S97yum-updatesd -> ../init.d/yum-updatesd
4 lrwxrwxrwx  1 root root   19 Sep 25 23:53 S99firstboot -> ../init.d/firstboot
4 lrwxrwxrwx  1 root root   11 Sep 25 23:48 S99local -> ../rc.local
4 lrwxrwxrwx  1 root root   23 Sep 26 00:48 S99OVCtrl -> /etc/rc.d/init.d/OVCtrl
4 lrwxrwxrwx  1 root root   16 Sep 25 23:51 S99smartd -> ../init.d/smartd

The links starting with “K” pass the “stop” argument to the startup/shutdown scripts. The links starting with “S” pass the “start” argument. The two-digit number following “K” or “S” determines the order in which the scripts are executed.

A common mistake: when adding a startup/shutdown script for a service that requires network access or access to a network-mounted filesystem, sysadmins often forget to make sure the service doesn’t try to start before network or network file-sharing services or that it shuts down before network.

Writing a Startup Script

Below is a simple example of a startup/shutdown script. These scripts can get very complex, but the basic structure is always the same. At the very minimum, your script needs to have the “start” and “stop” directives. If your startup script is for service that runs under a graphical desktop, such as Gnome or KDE, a “status” directive may also be needed.

#! /bin/sh
# Sample Unix System V-style initialization script

case "$1" in
  start)
        echo -n "Starting your_service"
        # To launch a process as root"
        /path/to/command/to/start/your_service
        # To launch a process as another user:
        /bin/su - username -c "/path/to/command/to/start/your_service argument1 argument2"
        echo "."
        ;;
  stop)
        echo -n "Stopping your_service"
        # To stop a process as root:
        /path/to/command/to/stop/your_service
        # To stop a process as another user:
        /bin/su - username -c "/path/to/command/to/stop/your_service argument1 argument2"
        echo "."
        ;;
  *)
        echo "Usage: /sbin/service your_service {start|stop}"
        exit 1
esac
exit 0

Modify this script as needed and place it in /etc/rc.d/init.d
In most cases, permissions for the startup/shutdown scripts should be 755.

A common mistake: some processes need to be started under a username other than root. The syntax for doing this is shown above. Sysadmins often forget to use double-quotes around the command containing space-separated argument. For example, the command below will work:

/bin/su - username -c "/path/to/command/to/start/your_service argument1 argument2"

And this command will not work:

/bin/su - username -c /path/to/command/to/start/your_service argument1 argument2

Creating Runlevel Links

Now that your startup/shutdown script has been written and tested, it is time to create soft links to the script from the appropriate /etc/rc.d/rc#.d directories. Let’s say you need your service to start in runlevels 3 and 5. Take a look in /etc/rc.d/rc3.d and in /etc/rc.d/rc5.d and determine when you want your service to start. In most cases, you will want your new service to start after everything else and to stop before everything else, when the system is being shut down or rebooted.

The script below will help you create appropriate soft links for starting and stopping your new service in runlevels 3 and 5, as well as for stopping them in runlevels 0 and 6.

#!/bin/sh

# Create shutdown links for runlevels 0, 3, 5 and 6
for i in 0 3 5 6
do
   ln -s /etc/rc.d/init.d/your_service /etc/rc.d/rc${i}.d/K01your_service
done

# Create startup links for runlevels 3 and 5
for i in 3 5
do
   ln -s /etc/rc.d/init.d/your_service /etc/rc.d/rc${i}.d/S99your_service
done

Always test your startup/shutdown scripts before rebooting the system. A simple typo in the script may cause your system to hang on startup or shutdown, requiring direct console access to fix the problem. Once you are convinced that your script works, do a reboot to verify. Never assume everything will work as expected just because the great you is on the job.

Print Friendly, PDF & Email

11 Comments »

  • KIRAN HARIDAS says:

    I wrote a start-up script for REDHAT but its not enable booting. he script is working on booing of the machine, so how can i tell them to start after booting.

    #! /bin/sh
    #
    ###
    #chkconfig: 235 98 55
    . /etc/rc.d/init.d/functions
    # 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:
    /install-dir/bin/catalina.sh run
    #Or to run it as some other user:
    /bin/su – dbsync -c /install-dir/bin/catalina.sh run
    echo “.”
    ;;
    stop)
    echo -n “Stopping new-service”
    #To run it as root:
    /install-dir/bin/catalina.sh stop
    #Or to run it as some other user:
    /bin/su – dbsync -c /install-dir/bin/catalina.sh stop
    echo “.”
    ;;

    *)
    echo “Usage: /sbin/service new-service {start|stop}”
    exit 1
    esac

    exit 0

    please help me with this. please send me a mail

  • jdfan says:

    Well I don’t wanna sound crazy or psycho, but I know my bf is cheating on me. I have this gut feeling but no concrete proof. He never let’s me see his phone but he gets mad if I dont let him TAKE mine. Its stupid, I know…but he is literally crazy and if I have evidence that he’s talking to other girls, I can break up with him & he can’t threaten me or my family (yes, he threatens me everytime I try and end things). So with that said, any advice? Does anyone know how to hack a Facebook profile?

  • Rassling Fundamentals says:

    The script will be designed to run at user login and to analyse the output of the quota command to determine what percentage of the quota is currently in use.
    Basically i need to write a script that produces the following output:
    > ./quota-warn
    You are currently using 91% of your quota
    Consider reducing your usage

    Can anyone give me any ideas..i appreciate your help..
    thanks..

  • liza says:

    I want to make it say a message and some cool things! I need help finding the login script

  • Jeremy Xargor is my gamertag says:

    I have a problem with the keyboard input into bash shells, this affects xterminals, text terminals (gettys) and remote shells too (ssh).

    When I am in a bash shell, I can’t type the (british) pound sign “£”. Instead it types a “#” followed by a newline. In emacs, I can type “£” fine, so it doesn’t seem to be a problem with xorg. Indeed, this is being written in firefox on the same system and I can write lots of ££££s!

    The $LANG is set to en_GB.UTF-8. I have tried other $LANG variables but this doesn’t seem to be the problem.

    Anyone got any ideas?

  • Jairo says:

    Feel like pranking someone but I need a free keylogger that wont hurt there keyboards. On top of that I need one that works on Ubuntu 8.10

  • MAK & CHEESE says:

    Hey everyone,
    I am planning to buy a laptop soon. I was going for the Apple Macbook Pro 13, but a friend told me about the difficulties in working with the Mac OS X. I want to about the user experience of both OS’s.
    Is Mac OS X usage and functions similar to that of Windows? Please provide me any website or suggestion regarding this topic.
    Thanks in advance:-)
    I intend to use the laptop for some audio & video editing with professional editors like Lightworks, etc. Apart from that I will use it for normal everyday things. But greater usage is of the media softwares and internet browsing using USB dongles. Please suggest.

  • Noe R says:

    I have a sony vaio laptop i use for managing my ipod, surfing the web, and writing some papers. Windows has proven to be a bit unreliable (had to wipe the laptop twice due to registry errors from different viruses) I kind of want to use linux for its stability and customisability. But on the other hand i want to keep windows intact for when i want to use a windows only application like itunes. With a 500 gig harddrive, do i have enough space to install 2 operating systems that i can choose from upon startup? Will it slow down or speed up my computer using a different os? If its possible, is it difficult to do? (I’m fairly tech savvy but clearly no genious) any help would be greatly appreciated.

  • zigg3ns says:

    I want to build a computer into my car, Is there a app or operating system that when i start the computer it will just show a list of songs from the hard drive? Or something similar? I dont want to click around to find itunes or something in windows or linux. Thanks!

  • Echo says:

    I was installing Mint Linux in my computer and it was not suppose to delete my Windows Vista or my 60000 mp3s but it did, and argh, I need to see if I can recover the data. This happened yesterday. What is the best program, where can I get it? I need one that is bootable or I guess one that would work in Linux, I can burn a cd on the laptop I’m borrowing right now. i care most for my mp3s, though I would like to recover Windows if possible too. Please help me someone. Oh also Mint will not recognize my Internet connection I will be trying is it ndiswrapper once I get the other situation worked out though I am wary of starting the computer unless I am going after that lost information as I seem to remember someone saying once somewhere, probably online, that the best thing to do is turn it off until you can get some sort of help. Money is an issue here with me so I can’t afford any outside help though if a 30 dollar program or so can do it, please tell me my options. Thank you.

  • simply complicated says:

    http://news.bbc.co.uk/1/hi/technology/7594249.stm
    I hear loads about linux, is it really better than Windows and if so, how?

Leave a Reply

%d bloggers like this: