centos7建议使用systemctl来管理服务的自启动,它可以知足以前service和chkconfig的功能
[Unit] Description=nginx - high performance web server Documentation=http://nginx.org/en/docs/ After=network.target remote-fs.target nss-lookup.target [Service] Type=forking PIDFile=/run/nginx.pid ExecStartPre=/usr/sbin/nginx -t -c /etc/nginx/nginx.conf ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf ExecReload=/bin/kill -s HUP $MAINPID ExecStop=/bin/kill -s QUIT $MAINPID PrivateTmp=true [Install] WantedBy=multi-user.target
Description : 服务的简单描述nginx
Documentation : 服务文档web
Before、After:定义启动顺序。Before=xxx.service,表明本服务在xxx.service启动以前启动。After=xxx.service,表明本服务在xxx.service以后启动。centos
Requires:这个单元启动了,它须要的单元也会被启动;它须要的单元被中止了,这个单元也中止了。tomcat
Wants:推荐使用。这个单元启动了,它须要的单元也会被启动;它须要的单元被中止了,对本单元没有影响。jvm
[Service]socket
Type=simple(默认值):systemd认为该服务将当即启动。服务进程不会fork。若是该服务要启动其余服务,不要使用此类型启动,除非该服务是socket激活型。post
Type=forking:systemd认为当该服务进程fork,且父进程退出后服务启动成功。对于常规的守护进程(daemon),除非你肯定此启动方式没法知足需求,使用此类型启动便可。使用此启动类型应同时指定 PIDFile=,以便systemd可以跟踪服务的主进程。测试
Type=oneshot:这一选项适用于只执行一项任务、随后当即退出的服务。可能须要同时设置 RemainAfterExit=yes 使得 systemd 在服务进程退出以后仍然认为服务处于激活状态。ui
Type=notify:与 Type=simple 相同,但约定服务会在就绪后向 systemd 发送一个信号。这一通知的实现由 libsystemd-daemon.so 提供。centos7
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 daemon-reload 修改配置后重载是配置生效 systemctl enable nginx.service 开机启用服务 systemctl disable nginx.service 禁用开机启动 systemctl is-enabled nginx.service 判断服务是否开机状态 systemctl list-unit-files | grep enabled 查看开机启动状态服务列表
systemctl start nginx.service 启动一个服务 systemctl stop postfix.service 关闭一个服务 systemctl restart nginx.service 重启一个服务 systemctl status postfix.service 显示一个服务的状态
操做系统:CentOS 7(64位)
已安装JDK,并配置好环境变量
已安装tomcat,可手动启动,安装路径:/opt/tomcat/(可自定义)
catalina.sh在执行的时候会调用同级路径下的setenv.sh来设置额外的环境变量,所以须要在/opt/tomcat/bin路径下建立setenv.sh文件。
export JAVA_HOME=/usr/lib/jvm/jdk1.8.0_121 export CATALINA_HOME=/opt/tomcat export CATALINA_BASE=/opt/tomcat #设置Tomcat的PID文件 CATALINA_PID="$CATALINA_BASE/bin/tomcat.pid" #添加JVM选项 JAVA_OPTS="-server -XX:PermSize=256M -XX:MaxPermSize=1024m -Xms512M -Xmx1024M -XX:MaxNewSize=256m"
==注意:路径都使用绝对路径。==
[Unit] Description=Tomcat8 After=syslog.target network.target remote-fs.target nss-lookup.target [Service] Type=forking PIDFile=/opt/tomcat/bin/tomcat.pid ExecStart=/opt/tomcat/bin/startup.sh ExecReload=/bin/kill -s HUP $MAINPID ExecStop=/bin/kill -s QUIT $MAINPID PrivateTmp=true [Install] WantedBy=multi-user.target
systemctl daemon-reload
systemctl enable tomcat.service
[Unit] Description=nginx After=network.target [Service] Type=forking ExecStart=/usr/local/nginx/sbin/nginx ExecReload=/usr/local/nginx/sbin/nginx -s reload ExecStop=/usr/local/nginx/sbin/nginx -s quit PrivateTmp=true [Install] WantedBy=multi-user.target
systemctl daemon-reload
systemctl enable nginx.service