Setting Up Startup and Shutdown Scripts in Ubuntu

One of the powerful features of the Linux operating system is the ability to control your system’s behavior by running scripts at startup, shutdown, or reboot. This article serves as a practical guide to setting up these scripts in Ubuntu Linux. From automating routine tasks to initializing services, the potential use cases are virtually limitless. Let’s dive in and explore how to use this feature to your advantage.

Basics of Startup and Shutdown Scripts

Before we delve into the configuration of these scripts, let’s quickly cover the basics. A startup script in Ubuntu Linux is a shell script that runs when the system boots up. On the other hand, a shutdown script runs when the system is going down or rebooting.

These scripts reside in specific directories within the Linux file system. For instance, the `/etc/rc.d/` directory is typically used to store startup scripts. These directories are organized according to ‘runlevels’, which represent the state of a Linux system at any given time. In contrast, `/etc/rc6.d/` and `/etc/rc0.d/` are the directories for scripts that run on shutdown and reboot, respectively.

Setting Up a Startup Script

Here’s a simple way to set up a startup script in Ubuntu Linux:

  1. Create a Script: Open your favorite text editor and write your script. For instance, if you want to update your system every time it starts up, your script could look something like this:
    apt-get update -y && apt-get upgrade -y

    Remember to include `#!/bin/bash` at the beginning of your script to specify that it should be run using the Bash shell.

  2. Make the Script Executable: Save your script to a file, for example, ``, and make it executable by running the following command:
    chmod +x /path/to/your/script/ 
  3. Move the Script to the Startup Directory: Move your script into the `/etc/init.d/` directory, which is used for startup scripts. You can use the mv command for this:
    sudo mv /path/to/your/script/ /etc/init.d/ 
  4. Register the Script: Lastly, register your script with the system to run at startup. You can do this with the `update-rc.d` command:
    sudo update-rc.d defaults 

    The defaults keyword here specifies that the script should be run in the default runlevels.

Setting Up a Shutdown Script

Setting up a shutdown script is much the same as a startup script, with just a slight difference. Here are the steps:

  1. Create a Script: Write your shutdown script. For instance, a script that sends a backup of a specific directory to a remote server upon shutdown might look like this:
    rsync -av /path/to/directory user@remote:/path/to/backup
  2. Make the Script Executable: As before, save your script to a file, for instance, ``, and make it executable:
    chmod +x /path/to/your/script/ 
  3. Move the Script to the Shutdown Directory: Move your script into the /etc/rc0.d/ directory, which is used for shutdown scripts. You can use the mv command for this:
    sudo mv /path/to/your/script/ /etc/rc0.d/ 

    Similarly, if you want your script to run on reboot, you should move it to the `/etc/rc6.d/` directory.

  4. Rename the Script: Shutdown and reboot scripts require a specific naming convention: `KXX`, where `XX` is a two-digit number representing the order in which the scripts are run (lower numbers run first) and is the name of the script. You can rename your script using the mv command:
    sudo mv /etc/rc0.d/ /etc/rc0.d/K99backup 


In conclusion, startup and shutdown scripts offer a powerful way to automate and control system processes in Ubuntu Linux. Whether you’re managing a personal machine or an enterprise server, understanding how to create and manage these scripts is an essential skill.

Please note that working with system scripts can have profound impacts on your system. Always double-check your scripts, and make sure to test them in a safe environment before deploying them on your main system. Happy scripting!

The post Setting Up Startup and Shutdown Scripts in Ubuntu appeared first on TecAdmin.

Linux System Administration TecAdmin