essay | tech | year-summary | about

返回上级菜单

about systemd service


日期:2019-03-31T00:00:00Z

Service File Location

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.

Put Your Service File Here

here is where you should write service

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

then use the following command

$ sudo systemctl enable service-name

Service File Layout

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

Write Your Service File

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

user based service 2024 June 3

https://owenyk.github.io/2021/12/15/%E5%9C%A8Debian11%E4%B8%8A%E5%AE%89%E8%A3%85clash%E7%9A%84%E8%AE%B0%E5%BD%95/#comment-waline

[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

Conclusion

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