几种设置开机启动的方法

1. rc.local

不被推荐使用,但是还挺好用的。
在鸟哥私房菜中说到ll /etc/rc3.d/ 可以查看到rc.local的运行级别是s99,我在自己环境上查看并未查看到,推测新版的linux中rc.local不被推荐使用后就未加入到级别3的启动中(其他级别也没有),但这并不代表rc.local没有被执行,在rc.local中加入打印并重启,rc.local确实有被执行。
rc.local是在/etc/rc3.d/下的脚本之后运行的。在rc3.d中添加任务的方法见第二节。

2. chkconfig

1.编写chkconfig格式的文件
其中 chkconfig: 345 85 15
345表示添加到运行级别3 4 5中,85表示启动顺序,15表示停止顺序

#!/bin/bash
# chkconfig: 345 85 15
# description: This is a haoCoding Test Service.
usage() {
  echo " usage:$0 {start|stop|restart} "
}

start() {
  echo "haoCoding Service Started!"
}

stop() {
  echo "haoCoding Service Stopped!"
}

restart() {
  stop
  start
}

#main function
case $1 in
  start)
     start
     ;;
  stop)
     stop
     ;;
  restart)
     restart
     ;;
  *)
     usage
     ;;
esac

将以上内容存储在hello中
2.将文件放在/etc/init.d/下面
然后执行chkconfig --add hello
(查询chkconfig --list hello
删除chkconfig --del hello)
通过ll /etc/rc4.d/ 可以查看到hello的软链接了。
(rc4.d表示运行级别4的启动目录,通过runlevel可以查看当前的运行级别)

reboot之后发现hello中的start部分的内容会被执行。

3. 制作成service

touch /lib/systemd/system/hello.service
vi /lib/systemd/system/hello.service
[Unit]
Description=Hello Service
 
[Service]
Type=simple
ExecStart=/usr/bin/hello.sh
SuccessExitStatus=2
 
[Install]
WantedBy=multi-user.target

当系统服务的进程是在ExecStart的程序中完成时,选用Type=simple配置,如果ExecStart的程序有子进程在后台运行时,应该使用Type=forking配置,父进程退出时,子进程也能够在后台运行。

新建启动脚本文件hello.sh并加可执行权限。

touch /usr/bin/hello.sh
vi /usr/bin/hello.sh

hello.sh的内容如下

 
#!/bin/bash
                  
echo "Hello World!" > /home/root/hello

给hello.sh赋权限

chmod +x /usr/bin/hello.sh
systemctl daemon-reload

重启系统,观察效果

reboot

查找hello.service的状态

systemctl status hello.service

可以看到这个服务启动了哪些进程,已经这些进程的一些启动信息(应该是从syslog或者message中拷出来的)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值