CentOS 7使用systemctl添加服务启动
CentOS 7继承了RHEL 7的新的特性,例如强大的systemctl,而systemctl的使用也使得以往系统服务的/etc/init.d的启动脚本的方式就此改变,也大幅提高了系统服务的运行效率。但服务的配置和以往也发生了极大的不同,说实在的,变的简单而易用了许多。
CentOS 7的服务systemctl脚本存放在:/usr/lib/systemd/,有系统(system)和用户(user)之分,像需要开机不登陆就能运行的程序,最好还是存在系统服务里面,即:/usr/lib/systemd/system目录下,每一个服务以.service结尾,一般会分为3部分:[Unit]、[Service]和[Install]
我们可以使用systemctl -a来查看所有服务,如果列表里面没有Nginx,PHP、Mysql,又想借助于systemctl来进行统一管理的话,就到上述所说的/usr/lib/systemd/system目录下面创建以下文件吧
vim /usr/lib/systemd/system/nginx.service
[Unit]
Description=nginx
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/application/nginx/logs/nginx.pid
ExecStart=/application/nginx/sbin/nginx
ExecReload=/application/nginx/sbin/nginx -s reload
ExecStop=/application/nginx/sbin/nginx -s stop
PrivateTmp=true
[Install]
WantedBy=multi-user.target
测试启动
pkill nginx
systemctl daemon-reload
systemctl start nginx
vim /usr/lib/systemd/system/php.service
[Unit]
Description=php
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/application/php/var/run/php-fpm.pid
ExecStart=/application/php/sbin/php-fpm
ExecReload=/bin/kill -USR2 MAINPIDExecStop=/bin/kill−INTMAINPID
ExecStop=/bin/pkill -9 php-fpm
PrivateTmp=true
[Install]
测试启动
pkill php
systemctl daemon-reload
systemctl start php
vim /usr/lib/systemd/system/mysql.service
[Unit]
Description=MySQL Community Server
After=network.target
After=syslog.target
[Install]
WantedBy=multi-user.target
Alias=mysql.service
[Service]
User=mysql
Group=mysql
PIDFile=/application/mysql/tmp/mysql_sunht.pid
# 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=/application/mysql/bin/mysqld_safe
# Don't signal startup success before a ping works
#ExecStartPost=/usr/bin/mysql-systemd-start post
# Disable service start and stop timeout logic of systemd for mysqld service.
TimeoutSec=600
Restart=always
PrivateTmp=false
测试启动
systemctl daemon-reload
systemctl start mysql
vi /usr/lib/systemd/system/redis.service
[Unit]
Description=Redis
After=network.target
[Service]
PIDFile=/var/run/redis_6379.pid
ExecStart=/usr/local/redis/src/redis-server /etc/redis.conf --daemonize no
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
[Install]
WantedBy=multi-user.target
测试启动
systemctl daemon-reload
systemctl start redis
vi /usr/lib/systemd/system/tomcat.service
[Unit]
Description=Tomcat of rapapi
After=syslog.target network.target
[Service]
#指定用户
User=root
Group=root
Type=forking
#因为tomcat使用信号管理进程,所以需要指定pid文件
PIDFile=/usr/share/tomcat/tomcat.pid
ExecStart=/usr/share/tomcat/bin/startup.sh
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
Restart=on-failure
RestartSec=10
[Install]
WantedBy=multi-user.target
测试启动
systemctl daemon-reload
systemctl start tomcat
服务脚本按照上面编写完成后,以754的权限保存在/usr/lib/systemd/system目录下,这时就可以利用systemctl进行配置了
使用systemctl start [服务名(也是文件名)]可以测试服务是否可以成功运行,如果不能运行则可以使用systemctl status [服务名(也是文件名)]查看错误信息和其他服务信息。然后根据报错进行修改,直到可以start,如果不放心还可以测试restart和stop命令。
本博客所有文章如无特别注明均为原创。
复制或转载请以超链接形式注明转自青信-运维,原文地址《systemctl添加服务启动》