where is service file ?

we can find many service in linux.
for example:

$ sudo systemctl status nginx
● nginx.service - The nginx HTTP and reverse proxy server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
   Active: active (running) since Sun 2019-03-31 12:50:17 UTC; 32min ago
 Main PID: 2839 (nginx)
   CGroup: /system.slice/nginx.service
           ├─2839 nginx: master process /usr/sbin/nginx
           └─2840 nginx: worker process

and if we type such command like this

$ sudo systemctl enable nginx
Created symlink from 
/etc/systemd/system/multi-user.target.wants/nginx.service to 
/usr/lib/systemd/system/nginx.service.

A symbolic link will be created.

so

here is where you should write service

/usr/lib/systemd/system/service-name.service

here is service which will be started when boot (enable)

/etc/systemd/system/multi-user.target.wants/service-name.service

how to write service file ?

here is nginx service file.

[Unit]
Description=The nginx HTTP and reverse proxy server
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/run/nginx.pid
# Nginx will fail to start if /run/nginx.pid already exists but has the wrong
# SELinux context. This might happen when running `nginx -t` from the cmdline.
# https://bugzilla.redhat.com/show_bug.cgi?id=1268621
ExecStartPre=/usr/bin/rm -f /run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t
ExecStart=/usr/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
KillSignal=SIGQUIT
TimeoutStopSec=5
KillMode=process
PrivateTmp=true

[Install]
WantedBy=multi-user.target

this is just a little complex.
here is a simple one.

[Unit]
Description=this is a description
After=you can copy from nginx.service

[Service]
Type=simple   
ExecStart=/usr/bin/something /home/your-script
KillMode=process
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=your-program-id-in-syslog
Environment=YOUR_VAR=xxxx
Restart=always

[Install]
WantedBy=multi-user.target
  • Type <- at most time simple is enough, sometimes you may need a forking1

  • Environment <- this is something you may need

  • Restart <- the best thing I like, your service will never down

  • multi-user.target <- this is for “enable”, multi-user.target which means command line use is simple output2

How To Configure a Linux Service to Start Automatically After a Crash or Reboot – Part 1: Practical Examples
How to Auto-start Services on Boot in Linux?
How to set up proper start/stop services
What is the difference between “systemctl start” and “systemctl enable”?
Creating Systemd Service Files
what is the CentOS equivalent of /var/log/syslog (on Ubuntu)?
sudo service status includes bad;
Run node.js service with systemd
How to Install Desktop Environments on CentOS 7
Install MATE or XFCE on CentOS 7
How To Install Xfce GUI In CentOS 7 Linux
man page: systemd.service — Service unit configuration
man page: systemd.unit — Unit configuration


Attribution-NonCommercial 4.0 International (CC BY-NC 4.0)