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 <br><br>

三 自定义开机程序

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 * * * * 脚本目录



作者:凌烟阁主5221
链接:https://www.jianshu.com/p/1dd924ae2e05
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值