shell编程
集合各种linux命令,实现某个功能,常用于自动化运维等。
shell编程
- 创建文件
[root@localhost test1]# cd /opt/
[root@localhost opt]# vim shell.sh
- 编写脚本文件
- 首行添加
#!/bin/bash
- 接着编写内容
echo "please input your name:" read uname echo "input your password:" read upwd echo "name:$uname, pwd:$upwd" #readonly name="py" #defined variale name="ppy" unset name
- 首行添加
- 修改文件权限
[root@localhost opt]# chmod a+x shell.sh
- 检查脚本语法
bash -n shell.sh
- 运行shell脚本
[root@localhost opt]# ./shell.sh
用户定时任务
可以设定定时任务,比如定时执行脚本。
- 查看用户任务,默认当前用户
crontab -l
- 删除用户任务,默认当前用户
crontab -r
- 设定用户,以上命令使用-u选项指定用户
crontab -l -u root
- 添加任务,默认当前用户
crontab -e
打开任务文件,编写任务。格式如下#分 时 日 月 周 命令 #* * * * * /bin/bash /opt/shell2.sh
- 比如每隔15分钟,
15 * * * * ls /home > /tmp/lshome.txt
- 比如每天早晨六点,
* 6 * * * ls /home > /tmp/lshome.txt
- 比如每隔15分钟,
- 在root用户下执行以下命令
- 停止服务,
service cron stop
或者/etc/init.d/cron stop
- 启动服务,
service cron start
或者/etc/init.d/cron start
- 重启服务,
servce cron restart
或者/etc/init.d/cron restart
- 以上cron取决于目录
/etc/init.d
中的cron
- 停止服务,
不为当下而战,何以颠覆未来