Let's execute a command on Linux system boot!
Midhun M Nair
SDE - 1@ Beebom || Node.js || React.js || Next.js || An Aspiring Architect Focused on Scalable & User-Centric Systems || Cloud Enthusiast || Google Cloud Certified Associate Cloud Engineer || #MERN Stack
If you're someone who enjoys tinkering with your system setup (like me!), you probably know that getting your server or service to start automatically on system boot can save a lot of manual work.
I’ve been exploring how to make my servers start automatically on system boot. After some recent experiments and a few facepalms along the way, I finally cracked the code on how to make this happen using systemd.
I have a Redis server and a Node.js app managed by PM2, and I figured out a way to start both of them using a shell script and systemd.
This approach is perfect when you want to run custom commands like starting multiple services together at boot.
So, let me walk you through it step by step!
Step 1: Create Your Shell Script
The first step is to write a shell script that starts both your Redis server and your Node.js app using PM2.
Here’s a basic example of the script (start-server.sh):
#!/bin/bash
# Start Redis server
redis-server
# Start Node.js server using PM2
pm2 start /path/to/your/app.js --name "my-node-server"
# Ensure Redis and Node.js started
echo "Redis and Node.js servers started successfully!"
Breaking it down:
Make sure your script is executable by running:
chmod +x /home/mn/startup/start-server.sh
Let me introduce "systemd"
It’s an init system that controls how services are started, stopped, and managed in Linux.
It also helps ensure that your services get started when your system boots up. If you want your custom service or command to run automatically when your machine boots.
Step 2: Create the systemd Service File
Now that you have the shell script, the next step is to create a systemd service file that will run this script at boot.
Create a new service file:
sudo nano /etc/systemd/system/server.service
Add the below content:
[Unit]
Description=Server Start Script with PM2
After=network.target
[Service]
ExecStart=/bin/bash /home/midhun/startup/start-server.sh
Type=forking
User=midhun
Restart=on-failure
RemainAfterExit=true
[Install]
WantedBy=multi-user.target
Breaking it down:
Type=forking: This means the service will fork into the background (since pm2 is forking itself).
User=midhun: The service will run under your user account (replace midhun with your username).
Restart=on-failure: Restarts the service if it fails.
RemainAfterExit=true: Ensures the service is marked active even if the script completes.
领英推荐
Step 3: Enable and Start the Service
After saving your service file, you need to tell systemd to recognize the new service and enable it to start on boot.
First, reload systemd:
sudo systemctl daemon-reload
Then enable the service:
sudo systemctl enable server
You can also manually start the service to test it out:
sudo systemctl start server
Check the status to see if everything is working correctly:
sudo systemctl status server
Step 4: Reboot and Test
The final step is to reboot your system and verify that both Redis and your Node.js server start automatically.
Reboot your machine:
sudo reboot
After reboot, you can check if the services started successfully by running:
pm2 ls
This should show your Node.js app running, and you can check Redis by running:
redis-cli ping
If it replies with "PONG", you’re good to go!
The PM2 also does provide a super cool command that generates a startup script for you that works with systemd. But this blog was mainly intended for systemd.
Wrapping Up
And that’s it! ?? Now, both your Redis server and Node.js app will automatically start on system boot using a simple shell script and systemd. This setup is super flexible and can be adapted for any commands or processes you want to run on startup.
I hope this guide was helpful! If you have any questions or want to share your own experiences, feel free to drop a comment. Until next time, happy coding!
#Redis #NodeJS #PM2 #systemd #Linux #Automation #DevOps #TechTips #ServerLife
IT Professional Specializing in Python, AI/ML Technologies, and Web Development
5 个月Saving it for Later, Sir ??