Run Multiple Bash Scripts with Systemd (Step-by-Step)

Run Multiple Bash Scripts with Systemd (Step-by-Step)

This tutorial guides you through setting up a systemd service to efficiently run multiple Linux Bash scripts. You’ll learn how to create a service that automatically starts, stops, and restarts your scripts. Let’s dive in!

Prerequisites

  • Linux System: You need a Linux system (e.g., Ubuntu, CentOS, Debian) with systemd installed.
  • Bash Scripts: Prepare the Bash scripts you want to run. Ensure they are executable and located in a specific directory (e.g., /path-to-bash-script/).

Steps to Create the Systemd Service

Create a Template Unit File:

Decide how many script instances you need (e.g., three instances).

Create a template unit file (e.g., /etc/systemd/system/foo@.service). Note the ”@” symbol, which allows for parameterization.

Add the following content to the file, replacing placeholders with your information:

/etc/systemd/system/foo@.service
1
[Unit]
2
Description=Running Bash Script for %I
3
4
[Service]
5
Type=simple
6
ExecStart=/bin/bash /path-to-bash-script/script-name-%i.sh
7
ExecStop=/bin/bash /path-to-bash-script/script-name-%i.sh
8
Restart=on-failure
9
10
[Install]
11
WantedBy=foo.target
  • %I represents the instance identifier passed during service activation.
  • %i represents the instance identifier converted to lowercase.

Replace /path-to-bash-script/ and script-name- with your actual paths and script name prefixes.

Enable Instances:

Enable the desired number of instances using systemctl enable:

Terminal window
1
systemctl enable foo@{1..3}.service

This command creates symlinks for instances foo@1.service, foo@2.service, and foo@3.service based on the template.

Start the Instances:

Start individual instances with systemctl start, providing the instance identifier:

Terminal window
1
systemctl start foo@1.service
2
systemctl start foo@2.service
3
systemctl start foo@3.service

Create a Target Dependency:

To group instances under a common target for easier management, create a target unit file (e.g., /etc/systemd/system/foo.target):

/etc/systemd/system/foo.target
1
[Unit]
2
Description=foo target
3
Requires=multi-user.target
4
After=multi-user.target
5
AllowIsolate=yes

Modify your .service units to be WantedBy=foo.target.

Reload and Start:

Reload systemd to apply changes:

Terminal window
1
sudo systemctl daemon-reload

Start the target (if created):

Terminal window
1
sudo systemctl start foo.target

Testing the Service

To test an instance, run:

Terminal window
1
sudo systemctl start foo@1.service

Check the status:

Terminal window
1
sudo systemctl status foo@1.service

If everything is configured correctly, you’ll see the output of your Bash script.

Conclusion

Congratulations! You’ve successfully set up a systemd service to manage multiple Bash script instances. This approach provides a structured way to automate and control your scripts.

Remember to adapt the paths, filenames, and instance numbers to your specific needs. Happy scripting! 🚀🐧