Centos7常用服务脚本(.service)
注意:[Service]中配置路径必须使用绝对路径。
启停:
systemctl { start | stop | restart | reload } xxx.service
自启动:
systemctl { enable | disable } xxx.service
nginx.service:
touch /usr/lib/systemd/system/nginx.service
chmod u+x /usr/lib/systemd/system/nginx.service
vi /usr/lib/systemd/system/nginx.service
内容如下:
[Unit]
Description=nginx daemon
After=network.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
启动服务&设置自启动:
systemctl start nginx
chkconfig nginx on
tomcat.service:
touch /etc/systemd/system/tomcat.service
chmod u+x /etc/systemd/system/tomcat.service
vi /etc/systemd/system/tomcat.service
内容如下:
[Unit]
Description=Apache Tomcat Web Application
After=syslog.target network.target
[Service]
Type=forking
Environment=JAVA_HOME=/usr/java/jdk1.8.0_371
Environment=CATALINA_HOME=/usr/local/tomcat/apache-tomcat-8.5.70
ExecStart=/usr/local/tomcat/apache-tomcat-8.5.70/bin/startup.sh
ExecStop=/usr/local/tomcat/apache-tomcat-8.5.70/bin/shutdown.sh
ExecReload=/bin/kill -s HUP $MAINPID
SuccessExitStatus=143
User=tomcat
Group=tomcat
PrivateTmp=true
[Install]
WantedBy=multi-user.target
这里配置指定的tomcat用户和组,注意目录对应的用户和用户组属性/权限(如果为 root:root 会导致启动失败),需要设置正确:chown -R tomcat:tomcat /usr/local/tomcat
mysql.service:
touch /usr/lib/systemd/system/mysqld.service
chmod u+x /usr/lib/systemd/system/mysqld.service
vi /usr/lib/systemd/system/mysqld.service
内容如下:
[Unit]
Description=MySQL Server
Documentation=man:mysqld(8)
Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html
After=network.target
After=syslog.target
[Install]
WantedBy=multi-user.target
[Service]
User=mysql
Group=mysql
ExecStart=/usr/local/mysql/bin/mysqld --defaults-file=/etc/my.cnf
LimitNOFILE = 5000
#Restart=on-failure
PrivateTmp=true
说明:
Restart=xxx 配置在服务进程退出,终止或超时时是否应重新启动服务
LimitNOFILE = 5000 进程最大打开文件数
PrivateTmp=true 表示给服务分配独立的临时空间