custom systemd service
Setting up a custom systemd service that keeps a script / binary running at all times is simple, yet very powerful, so I wanted to make a simple post about it.
Our goal in this post:
- create a systemd service that runs our custom script or binary
- make the service start on system boot and check status
Systemd will automatically restart our binary if it dies, which very convenient and easier than us having to write this logic ourselves :)
systemd service file⌗
Lets write our service file:
Create a service file in /etc/systemd/system/
. Use something unique as a name, this is the name you’ll use later to enable/start/restart the service.
For example: /etc/systemd/system/reverse-tunnel.service
[Unit]
Description=<DESCRIPTION>
[Service]
User=root
WorkingDirectory=<PATH_TO_BINARY>
ExecStart=<BINARY_NAME>
Restart=always
[Install]
WantedBy=multi-user.target
Make sure to edit the description, working directory and script name. You can also change the User if you don’t want this to run as root.
For example:
WorkingDirectory=/usr/local/bin
ExecStart=reverse_tunnel.sh
enable and start the service⌗
# you need to run that after changing service files
systemctl daemon-reload
# make it start on boot
systemctl enable reverse-tunnel
# start it now
systemctl start reverse-tunnel
Check that things worked:
# systemctl status reverse-tunnel
That’s it. Enjoy!