essay | tech | year-summary | about
日期:2019-03-31T00:00:00Z
Let's take a look at Nginx.service
$ 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
$ 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.
here is where you should write service
/usr/lib/systemd/system/service-name.service
then use the following command
$ sudo systemctl enable service-name
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
[Unit]
Description=A rule based proxy in Go for %i.
After=network.target
[Service]
Type=simple
User=%i
Restart=on-abort
ExecStart=/usr/bin/clash
[Install]
WantedBy=multi-user.target
systemctl daemon-reload
systemctl start clash@user
Write a Original Service File, and put it into
/usr/lib/systemd/system/
then just start & enable it via systemctl.
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