Close Menu
    Facebook X (Twitter) Instagram
    TecAdmin
    • Home
    • FeedBack
    • Submit Article
    • About Us
    Facebook X (Twitter) Instagram
    TecAdmin
    You are at:Home»Linux System Administration»Setting Up Startup and Shutdown Scripts in Ubuntu

    Setting Up Startup and Shutdown Scripts in Ubuntu

    By RahulJune 27, 20234 Mins Read

    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:

      1
      2
      #!/bin/bash
      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, `update.sh`, and make it executable by running the following command:
      chmod +x /path/to/your/script/update.sh 
      
    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/update.sh /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 update.sh 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:

      1
      2
      #!/bin/bash
      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, `backup.sh`, and make it executable:
      chmod +x /path/to/your/script/backup.sh 
      
    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/backup.sh /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<name>`, 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/backup.sh /etc/rc0.d/K99backup 
      

    Conclusion

    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!

    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp

    Related Posts

    Essential Linux Server Maintenance Checklist

    What is Fish (Friendly Interactive SHell)?

    How to Increase Sudo Session Duration in Linux

    Add A Comment

    Leave A Reply Cancel Reply

    Advertisement
    Recent Posts
    • Find Objects Between Two Dates in MongoDB: A Practical Guide
    • How to Check Packages Update History in Ubuntu
    • How to Grep for Contents after a Matching Pattern
    • How to Change Port in Next.Js
    • Ubuntu 24.04 LTS: The Future of Open-Source Excellence
    Facebook X (Twitter) Instagram Pinterest
    © 2023 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.