systemd有系统和用户区分:
系统(/user/lib/systemd/system/)
用户(/etc/lib/systemd/user/)
一般系统管理员手工创建的单元文件建议存放在/etc/systemd/system/目录下面。
/usr/lib/systemd/system目录自动存放启动文件的配置位置,里面一般包含有XXXXX.service。
当我们利用systemctl查看服务状态时,如下:
[admin@localhost system]$ systemctl status mysqld
● mysqld.service - MySQL Community Server
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
Active: active (running) since Sun 2020-05-17 13:45:54 CST; 7 months 20 days ago
Process: 4441 ExecStartPost=/usr/bin/mysql-systemd-start post (code=exited, status=0/SUCCESS)
Process: 4425 ExecStartPre=/usr/bin/mysql-systemd-start pre (code=exited, status=0/SUCCESS)
Main PID: 4440 (mysqld_safe)
Memory: 6.6G
CGroup: /system.slice/mysqld.service
├─4440 /bin/sh /usr/bin/mysqld_safe --basedir=/usr
└─4702 /usr/sbin/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --log-error=/var/log/mysqld.log --pid-file=/var/run/mysq...
如上出现的/usr/lib/systemd/system/mysqld.service,其实我们用systemctl命令操作mysqld服务其实就是操作mysql.service文件
,其实上面命令中的mysqld也可以用mysqld.service,调用该文件即可启动该服务。
mysqld.service文件内容:
[admin@localhost system]$ cat mysqld.service
#
# Simple MySQL systemd service file
#
# systemd supports lots of fancy features, look here (and linked docs) for a full list:
# http://www.freedesktop.org/software/systemd/man/systemd.exec.html
#
# Note: this file ( /usr/lib/systemd/system/mysql.service )
# will be overwritten on package upgrade, please copy the file to
#
# /etc/systemd/system/mysql.service
#
# to make needed changes.
#
# systemd-delta can be used to check differences between the two mysql.service files.
#
[Unit]
Description=MySQL Community Server
After=network.target
After=syslog.target
[Install]
WantedBy=multi-user.target
Alias=mysql.service
[Service]
User=mysql
Group=mysql
# Execute pre and post scripts as root
PermissionsStartOnly=true
# Needed to create system tables etc.
ExecStartPre=/usr/bin/mysql-systemd-start pre
# Start main service
ExecStart=/usr/bin/mysqld_safe --basedir=/usr
# Don't signal startup success before a ping works
ExecStartPost=/usr/bin/mysql-systemd-start post
# Give up if ping don't get an answer
TimeoutSec=600
Restart=always
PrivateTmp=false
具体可参考:
《Linux中systemd的service文件说明》