暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

Centos7开机自动运行脚本

云自由 2022-10-05
174

一、新建脚本文件

[guo@localhost ~]$ vim test.sh
  #!/bin/bash
  #chkconfig:2345 80 90     # 本脚本优先级
#decription:autostart     # 本脚本描述文字
  echo "HelloWorld"

chkonfig:启动级别和优先级。2345:表示在2、3、4、5运行级别上启动;80:表示启动优先级;90:表示关闭优先级。优先级范围是0-100,数字越大,优先级越低。

这两行必须存在,否则报错“服务 test.sh 不支持 chkconfig”。

二、增加脚本的可执行权限

[guo@localhost ~]$ chmod +x ./test.sh
[guo@localhost ~]$ ll
-rwxrwxr-x. 1 guo guo 18 10月 4 16:05 test.sh

三、将脚本移动到/etc/rc.d/init.d目录下(可以在/etc/init.d 下建立启动脚本,省去复制 )

[guo@localhost ~]$ sudo mv test.sh etc/rc.d/init.d/

四、添加脚本到开机自动启动项目中

[root@localhost init.d]# chkconfig --add test.sh

五、设置该脚本为开机自运行,已成为系统服务

[root@localhost init.d]# chkconfig test.sh on

六、查看服务状态

[root@localhost ~]# systemctl status test
● test.service - (null)
  Loaded: loaded (/etc/rc.d/init.d/test.sh; bad; vendor preset: disabled)
  Active: active (exited) since 二 2022-10-04 16:46:56 CST; 3min 51s ago
    Docs: man:systemd-sysv-generator(8)
Process: 1084 ExecStart=/etc/rc.d/init.d/test.sh start (code=exited, status=0/SUCCESS)

10月 04 16:46:56 localhost.localdomain systemd[1]: Starting (null)...
10月 04 16:46:56 localhost.localdomain test.sh[1084]: HelloWorld
10月 04 16:46:56 localhost.localdomain systemd[1]: Started (null).



文章转载自云自由,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论