目录
Centos6.x 或 RedHat6.x 自启动方式
一、通过 chkconfig 服务自启动
你好! 这是你第一次使用 Markdown编辑器 所展示的欢迎页。如果你想学习如何1.在/etc/init.d
下建立相关程序的启动脚本autostart.sh
#!/bin/sh
#chkconfig: 2345 80 90
#description:开机自动启动的脚本程序
# 开启redis服务 端口为6379
/usr/local/service/redis-2.8.3/src/redis-server --port 6379 &
PS:
脚本第一行 “#!/bin/sh” 告诉系统使用的shell;
脚本第二行 “#chkconfig: 2345 80 90” 表示在2/3/4/5运行级别启动,启动序号(S80),关闭序号(K90);
脚本第三行 表示的是服务的描述信息注意: 第二行和第三行必写,负责会出现如“服务 autostart.sh 不支持 chkconfig”这样的错误。
2.chmod +x autostart.sh
#给脚本赋予可执行权限
3.开机自启动
chkconfig --add autostart.sh
chkconfig autostart.sh on # 开机自启动
#chkconfig 服务名 off # 关闭自启动
service 服务名 start 手动启动服务
service 服务名 stop 手动关闭服务
二、自定义开机程序
1.vi /etc/rc.d/rc.local
2.末尾添加启动命令,例如:
/usr/local/service/redis-2.8.3/src/redis-server --port 6379 & #自动启动redis
#或者:
/usr/src/tomcat/bin/startup.sh # 自动启动tomcat
三 、定时启动脚本
- /root下写好启动的shell文件
#! /bin/sh
A=`netstat -anp | grep ":8080" | grep "LISTEN" | wc -l`
echo $A
if [ $A -eq 0 ]
then
/etc/init.d/tomcat start
echo "tomcat重启中"
else
echo "tomcat正在运行中"
fi
- crontab -e
- 设置好定时时间
- 设置为每分钟检查一次 //*/1 * * * * 脚本目录
Centos7 自启动方式
一、通过(init.d)服务的方式自启动
一 通过(init.d)服务的方式自启动(同上
)
1.在/etc/init.d 下建立相关程序的启动脚本
ln -s /etc/init.d/服务名 /etc/rc.d/rc3.d/S100服务名 //S:开机自启动 100:启动顺序
2.chkconfig --add 服务名
3.chkconfig 服务名 on 开机自启动
chkconfig 服务名 off 关闭自启动
4.service 服务名 start 手动启动服务
5.service 服务名 stop 手动关闭服务
二、通过systemctl 服务的方式自启动
1.cd /usr/lib/systemd/system/
2.vi 服务名.service
# Systemd unit file for default tomcat
#
# To create clones of this service:
# DO NOTHING, use tomcat@.service instead.
[Unit]
Description=Apache Tomcat Web Application Container
After=syslog.target network.target
[Service]
Type=simple
EnvironmentFile=/etc/tomcat/tomcat.conf
Environment="NAME="
EnvironmentFile=-/etc/sysconfig/tomcat
ExecStart=/usr/libexec/tomcat/server start
SuccessExitStatus=143
User=tomcat
[Install]
WantedBy=multi-user.target
3.systemctl enable 服务名.service
//设置自启动服务
4.systemctl start 服务名.service
//启动服务
5.systemctl stop 服务名.service
//停止服务
6.service 服务名 start
//启动服务
7.service 服务名 stop
//停止服务
任务 | 旧指令 | 新指令 |
---|---|---|
使某服务自动启动 | chkconfig httpd on | systemctl enable httpd.service |
使服务不自动启动 | chkconfig httpd off | systemctl disable httpd.service |
检查服务状态 | service httpd status | systemctl status httpd.service (服务详细信息) systemctl is-active httpd.service (仅显示是否 Active) |
已启动的服务 | chkconfig --list | systemctl list-units --type=service |
启动某服务 | service httpd start | systemctl start httpd.service |
停止某服务 | service httpd stop | systemctl stop httpd.service |
重启某服务 | service httpd restart | systemctl restart httpd.service |
三、自定义开机程序
1.vi /etc/rc.d/rc.local
2.末尾添加启动命令
3 /usr/src/tomcat/bin/startup.sh /自动启动tomcat/
四、定时启动脚本
1./root下写好启动的shell文件
2. crontab -e
3. 设置好定时时间
4. 设置为每分钟检查一次 // */1 * * * * 脚本目录