I have a custom service that I need to start on reboot on a CentOS system running systemd. Following the guidelines posted here, I was able to do it myself in my environment.
- Create the following file:
/etc/systemd/system/tcp-server.service - Put the following contents in the file:
[Unit]
Description=tcp-server for hhreplay Service
After=network.target
[Service]
Type=simple
User=root
Environment="OPTIONS=--logfile /tmp/tcp-server.log"
ExecStart=/home/jenkins/tcp-server.py $OPTIONS
Restart=on-abort[Install]
WantedBy=multi-user.target- Chown your log file:
chown root:root /tmp/tcp-server.log - Reload systemd:
sudo systemctl daemon-reload - Start your service to ensure it’s a functional service file:
systemctl start tcp-server - Check status:
systemctl status tcp-server - If it looks good, then set it to start at boot:
systemctl enable tcp-server --now
MAN PAGE
In-depth information can be found in the systemd.service man page. Read up on options such as Type, RemainAfterExit, ExecStart, WantedBy, ExecStop , Environment, etc.
Special Considerations
- In some distributions you are required to have a
ExecStartin your service file. This can be set to/bin/trueif you don’t explicitly need something there. RemainAfterExit=truecan be added to tricksystemdinto believing that the service is running. This is helpful when you don’t have aExecStartand still need aExecStopto run after the service, or another service, shutsdown.DefaultDependencies=nomeans ignore all dependencies and run “first” on start and “last” on stop. If you haveBeforeorAfterclauses, they will still be honored.
Additional References
0pointer.net – SystemD for Administrators Part XXI
freedesktop.org – systemd System and Service Manager