How to run a shell script at startup

Cover Image for How to run a shell script at startup
Matheus Mello
Matheus Mello
published a few days ago. updated a few hours ago

📝 Running a Shell Script at Startup Made Easy 🚀

So, you have a Linux instance on Amazon S3 and want to automate the process of running your Node.js application using shell scripts. You've successfully created two scripts, start_my_app and stop_my_app, to manually start and stop your application. But now, you're faced with the challenge of running start_my_app automatically at system startup. Fear not! We've got your back with this easy-to-follow guide.

The Initial Setup 🛠️

Before we dive into running the script at startup, we need to take care of a few preliminary steps:

1️⃣ Create the shell scripts: Make sure you have your start_my_app and stop_my_app shell scripts ready. These scripts should contain the necessary commands to start and stop your Node.js application, utilizing tools like forever or any other method you prefer.

2️⃣ Access init.d and rc.d directories: These directories hold the key to automating script execution at startup. You need to be familiar with their locations and permissions:

  • init.d: This directory contains system startup scripts. Typically, it's located at /etc/init.d/.

  • rc.d: This directory includes the runlevel-specific symbolic links to the scripts in init.d. It can vary across different Linux distributions. For example, on Debian-based systems, the location is /etc/rc.d/.

Now that we have the groundwork laid out, let's proceed to the main event!

Automating the Startup 🚀

To run your start_my_app script automatically at system startup, follow these steps:

1️⃣ Create a startup script: In the init.d directory, create a new file (let's name it my_app_startup) using your favorite text editor:

sudo nano /etc/init.d/my_app_startup

2️⃣ Populate the startup script: Inside my_app_startup, you need to include the necessary commands to run start_my_app. An easy way to achieve this is by adding the following lines:

#!/bin/bash

case $1 in
    start)
        /path/to/your/start_my_app
        ;;
    stop)
        /path/to/your/stop_my_app
        ;;
    *)
        echo "Usage: /etc/init.d/my_app_startup {start|stop}"
        exit 1
        ;;
esac

exit 0

Make sure to replace /path/to/your/start_my_app and /path/to/your/stop_my_app with the actual file paths to your shell scripts.

3️⃣ Make the script executable: Once you've populated my_app_startup, you need to make it executable. Run the following command:

sudo chmod +x /etc/init.d/my_app_startup

4️⃣ Create symbolic links: The last step is to create symbolic links from rc.d to init.d. This allows your system to execute my_app_startup at the appropriate runlevel. Run the following command (note the different paths based on your Linux distribution):

sudo ln -s /etc/init.d/my_app_startup /etc/rc.d/

5️⃣ Test it out: You're all set! Restart your system, and your start_my_app script should be automatically executed upon boot. Feel the automation magic kick in!

Troubleshooting Common Issues 🤔

Issue 1: Permission denied error: If you encounter a "permission denied" error while running the script, ensure that the script files (start_my_app and stop_my_app) have the executable permission. You can grant it by running:

sudo chmod +x /path/to/your/start_my_app /path/to/your/stop_my_app

Issue 2: Wrong file paths: Double-check that the paths to your start_my_app and stop_my_app scripts in my_app_startup are correct. Incorrect paths can prevent proper script execution at startup.

Share Your Experience! 📢

Congratulations! 🎉 Now you can sit back and enjoy the convenience of running your Node.js application automatically at system startup. If you encountered any issues or have further questions, don't hesitate to reach out. Share your experience in the comments below and let us know how running scripts at startup has improved your workflow!

Happy automating! 🤖💨


More Stories

Cover Image for How can I echo a newline in a batch file?

How can I echo a newline in a batch file?

updated a few hours ago
batch-filenewlinewindows

🔥 💻 🆒 Title: "Getting a Fresh Start: How to Echo a Newline in a Batch File" Introduction: Hey there, tech enthusiasts! Have you ever found yourself in a sticky situation with your batch file output? We've got your back! In this exciting blog post, we

Matheus Mello
Matheus Mello
Cover Image for How do I run Redis on Windows?

How do I run Redis on Windows?

updated a few hours ago
rediswindows

# Running Redis on Windows: Easy Solutions for Redis Enthusiasts! 🚀 Redis is a powerful and popular in-memory data structure store that offers blazing-fast performance and versatility. However, if you're a Windows user, you might have stumbled upon the c

Matheus Mello
Matheus Mello
Cover Image for Best way to strip punctuation from a string

Best way to strip punctuation from a string

updated a few hours ago
punctuationpythonstring

# The Art of Stripping Punctuation: Simplifying Your Strings 💥✂️ Are you tired of dealing with pesky punctuation marks that cause chaos in your strings? Have no fear, for we have a solution that will strip those buggers away and leave your texts clean an

Matheus Mello
Matheus Mello
Cover Image for Purge or recreate a Ruby on Rails database

Purge or recreate a Ruby on Rails database

updated a few hours ago
rakeruby-on-railsruby-on-rails-3

# Purge or Recreate a Ruby on Rails Database: A Simple Guide 🚀 So, you have a Ruby on Rails database that's full of data, and you're now considering deleting everything and starting from scratch. Should you purge the database or recreate it? 🤔 Well, my

Matheus Mello
Matheus Mello