Setting Up a New Service in SystemD on CentOS 7

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.

  1. Create the following file: /etc/systemd/system/tcp-server.service
  2. Put the following contents in the file:
  3. [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
  4. Chown your log file: chown root:root /tmp/tcp-server.log
  5. Reload systemd: sudo systemctl daemon-reload
  6. Start your service to ensure it’s a functional service file: systemctl start tcp-server
  7. Check status: systemctl status tcp-server
  8. 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

  1. In some distributions you are required to have a ExecStart in your service file.  This can be set to /bin/true if you don’t explicitly need something there.
  2. RemainAfterExit=true can be added to trick systemd into believing that the service is running.  This is helpful when you don’t have a ExecStart and still need a ExecStop to run after the service, or another service, shutsdown.
  3. DefaultDependencies=no means ignore all dependencies and run “first” on start and “last” on stop.  If you have Before or After clauses, they will still be honored.

Additional References

0pointer.netSystemD for Administrators Part XXI
freedesktop.orgsystemd System and Service Manager