Systemd is a default system and service manager for Linux OSs. It is a collection of tools and daemons designed to manage and control the startup process, the system’s services, and processes, and to handle other system events, operations and stuff. Here’s a great idea on how to start running Bash shell scripts as Systemd services, although you are free to do the same thing with a much modern approach, cough Docker.
Prerequisites
- Linux bash environment
Solution
Step 1. Create a basic bash script and save it as test.sh
. For instance:
#!/bin/bash
echo "Hello World!"
Step 2. Make it executable.
chmod +x test.sh
Step 3. Create a new file under /usr/lib/systemd/
named test.service
including the following content:
[Unit]
Description=My first test bash script service
[Service]
ExecStart=/bin/bash /path/to/test.sh
[Install]
WantedBy=multi-user.target
Step 4. Save the file and reload the systemctl daemon.
sudo systemctl daemon-reload
Step 5. Enable the service, so it’ll start on every (re)boot.
sudo systemctl enable test.service
Step 6. Start the service.
sudo systemctl start test.service
Step 7. Check the status.
sudo systemctl status test.service
Example output:
● hello.service - My first test bash script service
Loaded: loaded (/etc/systemd/system/test.service; enabled; vendor preset: enabled)
Active: active (running) since Fri 2023-02-18 14:35:47 UTC; 10s ago
Main PID: 12345 (bash)
Tasks: 1 (limit: 1137)
CGroup: /system.slice/test.service
└─12345 /bin/bash /path/to/test.sh
Feb 18 14:35:47 myhostname systemd[1]: Started My Hello World service.
Feb 18 14:35:47 myhostname bash[12345]: Hello World!
Conclusion
If you have any feedback or constructive criticism, feel free to leave a comment below and if you find this tutorial useful, follow our official channel on Telegram.