chenzhao

  • java
  • iOS
  • IT
知识积累
不积跬步无以至千里
  1. 首页
  2. IT
  3. 正文

Centos 7 环境添加开机自启Shell脚本以及部分shell命令_centos7开机启动sh文件

2023年 1月 29日 61点热度 0人点赞 0条评论

一、chkconfig方法添加shell脚本

1.查看当前的开机自启列表
chkconfig --list 
2.进入下列文件夹
cd /etc/rc.d/init.d 
2.新建文件
#touch test.sh
#vim test.sh
#或者直接
vim test.sh 
3.编辑文件如下
#!/bin/bash
#chkconfig:2345 80 90
#decription:autostart

# 固定格式,若缺失在添加进chkconfig的时候可能出现“服务xx.sh不支持 chkconfig”
## 脚本第一行 “#!/bin/sh” 告诉系统使用的shell;
## 脚本第二行 “#chkconfig: 2345 80 90” 表示在2/3/4/5运行级别启动,启动序号(S80),关闭序号(K90);
## 脚本第三行 表示的是服务的描述信息

#脚本具体命令写在下方#
#cd /xxx/xxx/xxx.sh
#./xxx.sh
#实例
/xxx/xxx/可执行文件 
sleep 5s  #可延时执行下一条指令
/xxx/xxx/某服务  start 
4.赋予脚本可执行权限
chmod +x test.sh 
5.添加到开机自启
chkconfig --add test.sh
chkconfig test.sh on
#若不需要可在此关闭
#chkconfig test.sh off 
6.可再次查看启动项
chkconfig --list 

不出意外会看到自己添加的启动,重启机器检查脚本是否执行,相关服务程序是否开启,出现如下2,3,4,5均为开

mysqld      0:关 1:关 2:开 3:开 4:开 5:开 6:关
network     0:关 1:关 2:开 3:开 4:开 5:开 6:关
test.sh     0:关 1:关 2:开 3:开 4:开 5:开 6:关 

二、部分shell脚本命令

1、延时指令
# 可用sleep、usleep
sleep 1 #等待1秒,默认单位为秒
# 在linux中,sleep的默认单位是秒,
# 在windows中,sleep的单位默认是毫秒。
## sleep 可以指定延迟的单位

sleep 1s  ## 延迟一秒

sleep 1m ## 延迟一分钟

sleep 1h ## 延迟一小时

sleep 1d ## 延迟一天 
2.echo命令
##显示字符串
echo string
echo "hello world"      #输出hello world 
echo hello world        #与有引号的输出一致
## 输出转义字符
echo "\"hello world\""  #输出 "hello world"
echo \"It is a test\"   #与有引号的输出一致 
3.test 命令
指令 含义
-eq 等于则为真
-ne 不等于则为真
-gt 大于则为真
-ge 大于等于则为真
-lt 小于则为真
-le 小于等于则为真
## 举例如下
a=10
b=20
if test [a] -eq[b]
then
    echo '两个数相等!'
else
    echo '两个数不相等!'
fi 
4.命令参考学习网站
https://www.runoob.com/linux/linux-shell.html 

本文转自 https://blog.csdn.net/weixin_48817416/article/details/125766605,如有侵权,请联系删除。

标签: 暂无
最后更新:2023年 1月 29日

陈昭

IT 程序员

打赏 点赞
< 上一篇
下一篇 >

文章评论

取消回复

COPYRIGHT © 2022 chenzhao. ALL RIGHTS RESERVED.

Theme Kratos Made By Seaton Jiang