centos 脚本(centos执行sh脚本)
大家好,今天给各位分享centos 脚本的一些知识,其中也会对centos执行sh脚本进行解释,文章篇幅可能偏长,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在就马上开始吧!
centos 7 shell 脚本怎样运行
一、root权限编辑/etc/rc.d/rc.local
Shell代码
su
cd/etc/rc.d/
vi rc.local
二、在这个文件加上你要执行的脚本,全部内容如下:
Shell代码
#!/bin/sh
#
# This script will be executed*after* all the other init scripts.
# You can put your own initialization stuff in here if you don't
# want to do the full Sys V style init stuff.
touch/var/lock/subsys/local
mount//192.168.0.3/data2-1/mnt/data2-1-o username=un,password=123
mount//192.168.0.3/data2-2/mnt/data2-2-o username=un,password=123
mount//192.168.0.3/data2-3/mnt/data2-3-o username=un,password=123
mount//192.168.0.3/data2-4/mnt/data2-4-o username=un,password=123
mount//192.168.0.3/data2-5/mnt/data2-4-o username=un,password=123
提示:这里的做法很不成熟,希望不要这样,最好自己写个脚本文件在这里来调用,结构更清晰,但是要注意到是把要执行的命令作为一个参数传递给su。
另外复习一个VI编辑命令-拷贝
yy
p
CENTOS7开机自启动脚本
1、因为在centos7中/etc/rc.d/rc.local的权限被降低了,所以需要赋予其可执行权
2、赋予脚本可执行权限
假设/usr/local/script/autostart.sh是你的脚本路径,给予执行权限
3、打开/etc/rc.d/rc.local文件,在末尾增加如下内容
1、将脚本移动到/etc/rc.d/init.d目录下
2、增加脚本的可执行权限
3、添加脚本到开机自动启动项目中
chkconfig启动脚本规范在脚本开头加入下面内容:
说明:chkonfig后面是启动级别和优先级,description后面是服务描述。如上面脚本意思是,
服务必须在运行级2,3,4,5下被启动或关闭,启动的优先级是90,停止的优先级是10。
优先级范围是0-100,数字越大,优先级越低。
Linux启动优先级
运行级别就是操作系统当前正在运行的功能级别。这个级别从0到6,具有不同的功能。
这些级别在/etc/inittab文件里指定。这个文件是init程序寻找的主要文件,最先运行的服务是那些放在/etc/rc.d目录下的文件。
不同的运行级定义如下:(可以参考Linux里面的/etc/inittab)
【实用】CentOS 7系统优化脚本
作为一名运维人员,部署操作系统的任务经常重复,操作内容大同小异。为此,可以将相同的操作制作成统一执行的脚本,通过手动输入不同部分实现自动化操作,节省大量时间。最近发现了一款好用的shell源码,稍作修改后分享给大家。
脚本主要实现系统优化功能,包括修改字符集、关闭selinux、关闭防火墙、安装常用工具和加快ssh登录等。
脚本结构如下:
1.主菜单
2.二级菜单
主要实现系统优化,如修改字符集、关闭selinux、关闭防火墙、安装常用工具和加快ssh登录等功能。
脚本代码如下:
sh
#!/bin/sh
./etc/rc.d/init.d/functions
exportLANG=zh_CN.UTF-8
#一级菜单
menu1(){
clear
cat</etc/locale.conf</dev/null
echo'#firewall-cmd--state'
firewall-cmd--state
systemctldisablefirewalld.service&>/dev/null
echo'#systemctllist-unit-files|grepfirewalld'
systemctllist-unit-files|grepfirewalld
action"完成禁用firewalld,生产环境下建议启用!"/bin/true
echo"==========================================================="
sleep5
}
#精简开机启动
chkset(){
echo"=======================精简开机启动========================"
systemctldisableauditd.service
systemctldisablepostfix.service
systemctldisabledbus-org.freedesktop.NetworkManager.service
echo'#systemctllist-unit-files|grep-E"auditd|postfix|dbus-org\.freedesktop\.NetworkManager"'
systemctllist-unit-files|grep-E"auditd|postfix|dbus-org\.freedesktop\.NetworkManager"
action"完成精简开机启动"/bin/true
echo"==========================================================="
sleep2
}
#修改文件描述符
limitset(){
echo"======================修改文件描述符======================="
echo'*-nofile65535'>/etc/security/limits.conf
ulimit-SHn65535
echo"#cat/etc/security/limits.conf"
cat/etc/security/limits.conf
echo"#ulimit-Sn;ulimit-Hn"
ulimit-Sn;ulimit-Hn
action"完成修改文件描述符"/bin/true
echo"==========================================================="
sleep2
}
#安装常用工具及修改yum源
yumset(){
echo"=================安装常用工具及修改yum源==================="
yuminstallwget-y&>/dev/null
if[$?-eq0];then
cd/etc/yum.repos.d/
\cpCentOS-Base.repoCentOS-Base.repo.$(date+%F)
ping-c1mirrors.aliyun.com&>/dev/null
if[$?-eq0];then
wget-O/etc/yum.repos.d/CentOS-Base.repo
yumcleanall&>/dev/null
yummakecache&>/dev/null
else
echo"无法连接网络"
exit$?
fi
else
echo"wget安装失败"
exit$?
fi
yum-yinstallntpdatelsofnet-toolstelnetvimlrzsztreenmapncsysstat&>/dev/null
action"完成安装常用工具及修改yum源"/bin/true
echo"==========================================================="
sleep2
}
#优化系统内核
kernelset(){
echo"======================优化系统内核========================="
chk_nf=`cat/etc/sysctl.conf|grepconntrack|wc-l`
if[$chk_nf-eq0];then
cat>>/etc/sysctl.conf</dev/null
if[$?-eq0];then
/usr/sbin/
echo"*/5****/usr/sbin/ntpdatentp.aliyun.com&>/dev/null">>/var/spool/cron/root
else
echo"ntpdate安装失败"
exit$?
fi
action"完成设置时间同步"/bin/true
echo"==========================================================="
sleep2
}
#history优化
historyset(){
echo"========================history优化========================"
chk_his=`cat/etc/profile|grepHISTTIMEFORMAT|wc-l`
if[$chk_his-eq0];then
cat>>/etc/profile<<'EOF'
#设置history格式
exportHISTTIMEFORMAT="[%Y-%m-%d%H:%M:%S][`whoami`][`whoami|awk'{print$NF}'|sed-r's#[()]##g'`]:"
#记录shell执行的每一条命令
exportPROMPT_COMMAND='\ if[-z"$OLD_PWD"];then exportOLD_PWD=$PWD; fi; if[!-z"$LAST_CMD"&&["$(history1)"!="$LAST_CMD"];then logger-t`whoami`_shell_dir"[$OLD_PWD]$(history1)"; fi; exportLAST_CMD="$(history1)"; exportOLD_PWD=$PWD;' EOF
source/etc/profile
else
echo"优化项已存在。"
fi
action"完成history优化"/bin/true
echo"==========================================================="
sleep2
}
main(){
menu1
case$num1in
1) localeset selinuxset firewalldset chkset limitset yumset kernelset sshset restartset ntpdateset historyset;;
2) menu2
case$num2in
1) localeset;;
2) selinuxset;;
3) firewalldset;;
4) chkset;;
5) limitset;;
6) yumset;;
7) kernelset;;
8) sshset;;
9) restartset;;
10) ntpdateset;;
11) historyset;;
12) main;;
13) exit;;
*) echo'Pleaseselectanumberfrom[1-13].';;
esac
;;
3) exit;;
*) echo'Err:Pleaseselectanumberfrom[1-3].' sleep3 main;;
esac
}
main$*
将脚本保存为init.sh,赋予执行权限并执行即可。
chmod+xinit.sh&&./init.sh
通过一键命令执行,同样能达到脚本的效果:
bash-c"$(curl-Ls.aaa.al/init.sh)"
如需新增功能,可在脚本基础上进行修改实现。
本书从实用角度出发,结合实际应用案例,模拟真实的系统环境,介绍电脑的使用方法与技巧,旨在帮助读者全面、系统地掌握电脑的应用。书中“高手支招”板块提供大量实用技巧,解决日常工作中遇到的常见问题。