cd /lib/systemd/system
cat > filebeat.service <<EOF
[Unit]
Description=filebeat
Wants=network-online.target
After=network-online.target
[Service]
User=root
ExecStart=/srv/filebeat-7.11.1-linux-x86_64/filebeat -e -c /srv/filebeat-7.11.1-linux-x86_64/filebeat.yml
# 设置为掉线自动重启,进程强制杀掉后会自动重新启动
Restart=always
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable filebeat
systemctl status filebeat
systemctl start filebeat
systemctl status filebeat
journalctl -f -u filebeat
cd /lib/systemd/system
cat > logstash.service <
systemctl start filebeat #启动filebeat服务
systemctl enable filebeat #设置开机自启动
systemctl disable filebeat #停止开机自启动
systemctl status filebeat #查看服务当前状态
systemctl restart filebeat #重新启动服务
systemctl list-units --type=service #查看所有已启动的服务
启动某服务
systemctl start tomcat.service
停止某服务
systemctl stop tomcat.service
重启某服务
service tomcat restart
systemctl restart tomcat.service
使某服务自动启动(如tomcat服务)
systemctl enable tomcat.service
使某服务不自动启动
systemctl disable tomcat.service
检查服务状态
systemctl status tomcat.service (服务详细信息)
systemctl is-active tomcat.service(仅显示是否Active)
显示所有已启动的服务
systemctl list-units --type=service
[root@localhost redis-7.0.5]# vi /etc/systemd/system/redis.service
编辑文件,然后按Esc按键,输入“:wq”保存并退出。(如果要编辑按“a”按键。)
[root@localhost redis-7.0.5]# systemctl daemon-reload
# 启动
systemctl start redis
# 停止
systemctl stop redis
# 重启
systemctl restart redis
# 查看状态
systemctl status redis
[Unit]部分主要是对这个服务的说明,内容包括Description和After,Description 用于描述服务,After用于描述服务类别
Documentation : 服务文档
Before、After:定义启动顺序。Before=xxx.service,代表本服务在xxx.service启动之前启动。After=xxx.service,代表本服务在xxx.service之后启动。
Requires:这个单元启动了,它需要的单元也会被启动;它需要的单元被停止了,这个单元也停止了。
Wants:推荐使用。这个单元启动了,它需要的单元也会被启动;它需要的单元被停止了,对本单元没有影响。
[Service]部分是服务的关键,是服务的一些具体运行参数的设置.
Type=forking是后台运行的形式,
User=users是设置服务运行的用户,
Group=users是设置服务运行的用户组,
PIDFile为存放PID的文件路径,
ExecStart为服务的具体运行命令,
ExecReload为重启命令,
ExecStop为停止命令,
PrivateTmp=True表示给服务分配独立的临时空间
注意:[Service]部分的启动、重启、停止命令全部要求使用绝对路径,使用相对路径则会报错!
Type=simple(默认值):systemd认为该服务将立即启动。服务进程不会fork。如果该服务要启动其他服务,不要使用此类型启动,除非该服务是socket激活型。
Type=forking:systemd认为当该服务进程fork,且父进程退出后服务启动成功。对于常规的守护进程(daemon),除非你确定此启动方式无法满足需求,使用此类型启动即可。使用此启动类型应同时指定 PIDFile=,以便systemd能够跟踪服务的主进程。
Type=oneshot:这一选项适用于只执行一项任务、随后立即退出的服务。可能需要同时设置 RemainAfterExit=yes 使得 systemd 在服务进程退出之后仍然认为服务处于激活状态。
Type=notify:与 Type=simple 相同,但约定服务会在就绪后向 systemd 发送一个信号。这一通知的实现由 libsystemd-daemon.so 提供。
Type=dbus:若以此方式启动,当指定的 BusName 出现在DBus系统总线上时,systemd认为服务就绪。
Type=idle: systemd会等待所有任务(Jobs)处理完成后,才开始执行idle类型的单元。除此之外,其他行为和Type=simple 类似。
PIDFile:pid文件路径
ExecStart:指定启动单元的命令或者脚本,ExecStartPre和ExecStartPost节指定在ExecStart之前或者之后用户自定义执行的脚本。Type=oneshot允许指定多个希望顺序执行的用户自定义命令。
ExecReload:指定单元停止时执行的命令或者脚本。
ExecStop:指定单元停止时执行的命令或者脚本。
PrivateTmp:True表示给服务分配独立的临时空间
Restart:这个选项如果被允许,服务重启的时候进程会退出,会通过systemctl命令执行清除并重启的操作。
RemainAfterExit:如果设置这个选择为真,服务会被认为是在激活状态,即使所以的进程已经退出,默认的为假,这个选项只有在Type=oneshot时需要被配置。
[Install]部分是服务安装的相关设置,可设置为多用户的
Alias:为单元提供一个空间分离的附加名字。
RequiredBy:单元被允许运行需要的一系列依赖单元,RequiredBy列表从Require获得依赖信息。
WantBy:单元被允许运行需要的弱依赖性单元,Wantby从Want列表获得依赖信息。
Also:指出和单元一起安装或者被协助的单元。
DefaultInstance:实例单元的限制,这个选项指定如果单元被允许运行默认的实例。
注意点
①首先,使用systemctl start [ 服务名(也是文件名) ] 可测试服务是否可以成功运行,如果不能运行则可以使用systemctl status [ 服务名(也是文件名) ]查看错误信息和其他服务信息,然后根据报错进行修改,直到可以start,如果不放心还可以测试restart和stop命令。
②接着,只要使用systemctl enable xxxxx就可以将所编写的服务添加至开机启动即可。
举例tomcat项目
#vim /usr/lib/systemd/system/tomcat.service
[Unit]
Description=java tomcat project
After=tomcat.service
[Service]
Type=forking
User=users
Group=users
PIDFile=/usr/local/tomcat/tomcat.pid
ExecStart=/usr/local/tomcat/bin/startup.sh
ExecReload=
ExecStop=/usr/local/tomcat/bin/shutdown.sh
PrivateTmp=true
[Install]
WantedBy=multi-user.target
systemctl enable tomcat.service
1)首先检查系统上是否安装了systemd以及当前安装的Systemd的版本是什么?
# systemd --version
systemd 215
+PAM +AUDIT +SELINUX +IMA +SYSVINIT +LIBCRYPTSETUP +GCRYPT +ACL +XZ -SECCOMP -APPARMOR
从上面的例子可以清楚地看出,我们已经安装了systemd 215版本。
2)检查systemd和systemctl的二进制文件和库的安装位置。
# whereis systemd
systemd: /usr/lib/systemd /etc/systemd /usr/share/systemd /usr/share/man/man1/systemd.1.gz
# whereis systemctl
systemctl: /usr/bin/systemctl /usr/share/man/man1/systemctl.1.gz
3)检查systemd是否正在运行。
# ps -eaf | grep [s]ystemd
root 1 0 0 16:27 ? 00:00:00 /usr/lib/systemd/systemd --switched-root --system --deserialize 23
root 444 1 0 16:27 ? 00:00:00 /usr/lib/systemd/systemd-journald
root 469 1 0 16:27 ? 00:00:00 /usr/lib/systemd/systemd-udevd
root 555 1 0 16:27 ? 00:00:00 /usr/lib/systemd/systemd-logind
dbus 556 1 0 16:27 ? 00:00:00 /bin/dbus-daemon --system --address=systemd: --nofork --nopidfile --systemd-activation
注意:systemd作为父守护进程运行(PID = 1)。 在上面的命令ps中使用(-e)选择所有进程,( - a)选择除会话前导之外的所有进程和(-f)选择完整格式列表(即-eaf)。
4)分析systemd启动过程
# systemd-analyze
Startup finished in 487ms (kernel) + 2.776s (initrd) + 20.229s (userspace) = 23.493s
5)分析每个进程在引导时花费的时间
# systemd-analyze blame
8.565s mariadb.service
7.991s webmin.service
6.095s postfix.service
4.311s httpd.service
3.926s firewalld.service
3.780s kdump.service
3.238s tuned.service
1.712s network.service
1.394s lvm2-monitor.service
1.126s systemd-logind.service
....
6)如何使用systemctl命令终止服务
# systemctl kill httpd
# systemctl status httpd
httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled)
Active: failed (Result: exit-code) since Tue 2018-04-28 18:01:42 IST; 28min ago
Main PID: 2881 (code=exited, status=0/SUCCESS)
Status: "Total requests: 0; Current requests/sec: 0; Current traffic: 0 B/sec"
Apr 28 17:37:29 tecmint systemd[1]: httpd.service: Got notification message from PID 2881, but recepti...bled.
Apr 28 17:37:29 tecmint systemd[1]: httpd.service: Got notification message from PID 2881, but recepti...bled.
Apr 28 17:37:39 tecmint systemd[1]: httpd.service: Got notification message from PID 2881, but recepti...bled.
Apr 28 17:37:39 tecmint systemd[1]: httpd.service: Got notification message from PID 2881, but recepti...bled.
Apr 28 17:37:49 tecmint systemd[1]: httpd.service: Got notification message from PID 2881, but recepti...bled.
Apr 28 17:37:49 tecmint systemd[1]: httpd.service: Got notification message from PID 2881, but recepti...bled.
Apr 28 17:37:59 tecmint systemd[1]: httpd.service: Got notification message from PID 2881, but recepti...bled.
Apr 28 17:37:59 tecmint systemd[1]: httpd.service: Got notification message from PID 2881, but recepti...bled.
Apr 28 18:01:42 tecmint systemd[1]: httpd.service: control process exited, code=exited status=226
Apr 28 18:01:42 tecmint systemd[1]: Unit httpd.service entered failed state.
Hint: Some lines were ellipsized, use -l to show in full.
7)其他systemctl命令连接
传送门
Process: 2688 ExecStart=/usr/local/tomcat/apache-tomcat-
9.0.36/bin/startup.sh (code=exited, status=217/USER)
[Unit]
Description=Tomcat9 servlet
After=syslog.target network.target
[Service]
Type=forking
User=tomcat
Group=tomcat
Environment="JAVA_HOME=/usr/lib/jvm/jre"
Environment="JAVA_OPTS=-Djava.awt.headless=true"
Environment="CATALINA_BASE=/usr/local/tomcat/apache-tomcat-9.0.36"
Environment="CATALINA_HOME=/usr/local/tomcat/apache-tomcat-9.0.36"
Environment="CATALINA_PID=/usr/local/tomcat/apache-tomcat-9.0.36/temp/tomcat.pid"
Environment="CATALINA_OPTS=-Xms512M -Xmx1024M -server -XX:+UseParallelGC"
ExecStart=/usr/local/tomcat/apache-tomcat-9.0.36/bin/startup.sh
ExecStop=/usr/local/tomcat/apache-tomcat-9.0.36/bin/shutdown.sh
[Install]
WantedBy=multi-user.target
[Unit]
Description=Tomcat9 servlet
After=syslog.target network.target
[Service]
Type=forking
User=root
Group=root
Environment="JAVA_HOME=/usr/lib/jvm/jre"
Environment="JAVA_OPTS=-Djava.awt.headless=true"
Environment="CATALINA_BASE=/usr/local/tomcat/apache-tomcat-9.0.36"
Environment="CATALINA_HOME=/usr/local/tomcat/apache-tomcat-9.0.36"
Environment="CATALINA_PID=/usr/local/tomcat/apache-tomcat-9.0.36/temp/tomcat.pid"
Environment="CATALINA_OPTS=-Xms512M -Xmx1024M -server -XX:+UseParallelGC"
ExecStart=/usr/local/tomcat/apache-tomcat-9.0.36/bin/startup.sh
ExecStop=/usr/local/tomcat/apache-tomcat-9.0.36/bin/shutdown.sh
[Install]
WantedBy=multi-user.target
[root@test101 nginx]# systemctl restart nginx
Job for nginx.service failed because the control process exited
with error code. See "systemctl status nginx.service" and
"journalctl -xe" for details.
[root@test101 nginx]# systemctl status nginx.service
● nginx.service - nginx - high performance web server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
Active: failed (Result: exit-code) since 五 2018-07-20 09:27:01 CST; 8s ago
Docs: http://nginx.org/en/docs/
Process: 50264 ExecStop=/bin/kill -s TERM $MAINPID (code=exited, status=0/SUCCESS)
Process: 50329 ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf (code=exited, status=1/FAILURE)
Main PID: 50243 (code=exited, status=0/SUCCESS)
[root@test101 nginx]# vim /etc/selinux/config
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
# enforcing - SELinux security policy is enforced.
# permissive - SELinux prints warnings instead of enforcing.
# disabled - No SELinux policy is loaded.
SELINUX=enforcing #改成disabled就OK了
# SELINUXTYPE= can take one of three two values:
# targeted - Targeted processes are protected,
# minimum - Modification of targeted policy. Only selected processes are protected.
# mls - Multi Level Security protection.
SELINUXTYPE=targeted
[root@host-172-0-0-11 conf.d]# systemctl start nginx
Job for nginx.service failed because the control process exited with error code. See "systemctl status nginx.service" and "journalctl -xe" for details.
[root@host-172-0-0-11 conf.d]# systemctl status nginx.service
â— nginx.service - nginx - high performance web server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
Active: failed (Result: exit-code) since Thu 2018-08-02 11:11:40 CST; 7s ago
Docs: http://nginx.org/en/docs/
Process: 18995 ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf (code=exited, status=1/FAILURE)
Aug 02 11:11:40 host-172-51-121-11 systemd[1]: Starting nginx - high performance web server...
Aug 02 11:11:40 host-172-51-121-11 nginx[18995]: /usr/sbin/nginx: /lib64/libcrypto.so.10: version `OPENSSL_1.0.2' not found (required by /usr/sbin/nginx)
[root@host-172-51-121-11 conf.d]# rpm -qa openssl
openssl-1.0.1e-60.el7.x86_64
[root@host-172-0-0-11 conf.d]#