linux自启动的几种方式

RedHat4 自启动方式

一 通过服务的方式自启动
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 手动关闭服务

二 自定义开机程序
1.vi /etc/rc.d/rc.local
2.末尾添加启动命令
3 /usr/src/tomcat/bin/startup.sh /*自动启动tomcat*/

三 定时启动脚本
1. /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

  


2. crontab -e
3. 设置好定时时间
4. 设置为每分钟检查一次 //*/1 * * * * 脚本目录

**********************************************************************
Centos7 自启动方式

一 通过(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 * * * * 脚本目录

posted @ 2018-07-04 16:46  梁小灶  阅读(28940)  评论(2编辑  收藏  举报