Add Startup Script in RHEL 7

Janny Hou
2 min readSep 16, 2020

This article is about my experience of executing a custom startup script in the RHEL 7 system. Many good articles already covered most of the steps, mine adds a few more tips in case people also run into the same problems.

I followed the tutorial

and finally got my startup script running with the advice in

The first tutorial explains the detailed instructions of booting your own code when RHEL 7 system starts by “create setup script”, “create startup service”, “load and start the service”. Here is a brief summary of them:

  1. Create a setup script
# vi /var/tmp/test_script.sh

As an example, we simply create a .txt file in the script. So if you see the .txt file created it means the script is executed successfully.

#!/bin/bash
: > /var/tmp/test.txt #Create an empty file

IMPORTANT: Make sure your setup script has the shebang line (may vary per system).

Set the file permission:

chmod +x /var/tmp/test_script.sh

2. Create a startup service

Create file:

# vi /etc/systemd/system/sample.service

with content:

[Unit]
Description=Description for sample script goes here
After=network.target

[Service]
Type=simple
ExecStart=/var/tmp/test_script.sh
TimeoutStartSec=0

[Install]
WantedBy=default.target

3. Load and start the service

Load service:

# systemctl daemon-reload

Enable it:

systemctl enable sample.service

IMPORTANT: In my case, I need an additional step to create a symlink under `/etc/systemd/system/multi-user.target.wants` as the second article mentions:

ln -s /etc/systemd/system/sample.service /etc/systemd/system/multi-user.target.wants/sample.service

Finally, start it:

systemctl start sample.service

Then reboot your system systemctl reboot and hope the .txt file gets created!

If your script doesn’t seem run properly, checking the service status could help you investigate the reason.

Run the following command to see whether your service is active, or was active and exited properly:

systemctl status sample.service -l

An error I encountered was“status/203 EXEC”. TL:DR, it’s usually caused by the script itself or its interpreter cannot be executed, like missing shebang in the bash file.

The answer of question below explains most of the possibilities why this error happens:

https://stackoverflow.com/questions/12296308/shell-script-working-fine-without-shebang-line-why

And another good article that talks about the service status:

Hope your startup service runs now :)

--

--