Linux 添加开机自启动

44 篇文章 0 订阅
11 篇文章 0 订阅

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

三 、定时启动脚本

  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
  1. crontab -e
  2. 设置好定时时间
  3. 设置为每分钟检查一次 //*/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 onsystemctl enable httpd.service
使服务不自动启动chkconfig httpd offsystemctl disable httpd.service
检查服务状态service httpd statussystemctl status httpd.service (服务详细信息)
systemctl is-active httpd.service (仅显示是否 Active)
已启动的服务chkconfig --listsystemctl list-units --type=service
启动某服务service httpd startsystemctl start httpd.service
停止某服务service httpd stopsystemctl stop httpd.service
重启某服务service httpd restartsystemctl 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 * * * * 脚本目录

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值