shell编程

集合各种linux命令,实现某个功能,常用于自动化运维等。

shell编程

  1. 创建文件
    [root@localhost test1]# cd /opt/
    [root@localhost opt]# vim shell.sh
  2. 编写脚本文件
    • 首行添加#!/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
    
  3. 修改文件权限
    [root@localhost opt]# chmod a+x shell.sh
  4. 检查脚本语法
    bash -n shell.sh
  5. 运行shell脚本
    [root@localhost opt]# ./shell.sh

用户定时任务

可以设定定时任务,比如定时执行脚本。

  1. 查看用户任务,默认当前用户
    crontab -l
  2. 删除用户任务,默认当前用户
    crontab -r
  3. 设定用户,以上命令使用-u选项指定用户
    crontab -l -u root
  4. 添加任务,默认当前用户
    crontab -e打开任务文件,编写任务。格式如下
        #分 时 日 月 周 命令
        #* * * * * /bin/bash /opt/shell2.sh
    
    • 比如每隔15分钟,15 * * * * ls /home > /tmp/lshome.txt
    • 比如每天早晨六点,* 6 * * * ls /home > /tmp/lshome.txt
  5. 在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
posted @ 2018-08-04 22:46  bkycrab  阅读(145)  评论(0编辑  收藏  举报