欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

shell脚本实现定时开启Apache服务

程序员文章站 2022-07-10 09:22:08
...
  1. 写shell脚本
#! /bin/bash
case $1  in
        start)
                systemctl start httpd
                if [ $? -eq 0 ]
                then
                        echo "已成功开启httpd服务"
                else
                        echo "开启失败"
                fi
                ;;
        stop)
                systemctl stop httpd
                if [ $? -eq 0 ]
                then
                        echo "已成功关闭httpd服务"
                else
                        echo "关闭失败"
                fi
                ;;
        restart)
                systemctl restart httpd
                if [ $? -eq 0 ]
                then
		                echo "已成功重启httpd服务"
                else
                        echo "重启失败"
                fi
                ;;
          *)
                echo "没有此项功能,敬请期待"
                ;;
esac

  1. 实现定时任务

yum install -y crontabs
systemctl start crond
crontab -e 设置定时任务
46 14 * * * . /root/mariadb.sh stop
crontab -l 查看已经设置的定时任务
[[email protected] ~]# crontab -l
46 14 * * * . /root/mariadb.sh stop

#! /bin/bash
read -p "请输入您要管理的服务名称:" name
echo "1、开启${name};"
echo "2、重启${name};"
echo "3、关闭${name};"
echo "4、查看${name}状态;"
echo "5、开启定时服务;"
echo "6、退出"
read -p "请选择您要进行的操作:" num
case $num in
        1) systemctl start $name
           if [ $? -eq 0 ]
           then
           echo "${name}开启成功!"
           else
           echo "${name}开启失败!"
           fi
        ;;
        2) systemctl restart $name
           if [ $? -eq 0 ]
           then
           echo "${name}重启成功!"
           else
           echo "${name}重启失败!"
           fi
        ;;
        3) systemctl stop $name
           if [ $? -eq 0 ]
           then
           echo "${name}关闭成功!"
           else
           echo "${name}关闭失败!"
           fi
        ;;
        4)
          systemctl status $name
        ;;
        5)
          read -p "请输入每天自动开启的时间(24小时制):" h_up
          read -p "请输入每天自动关闭的时间(24小时制):" h_down
          echo "0 ${h_down} * * * systemctl stop $name" > crontab.cron
          echo "0 ${h_up} * * * systemctl start $name" >> crontab.cron
          crontab crontab.cron
          if [ $? -eq 0 ]
          then
          echo "${name}定时功能开启成功!"
          echo "您的设定为每天${h_up}点开启,${h_down}点关闭!"
          else
          echo "${name}定时功能开启失败!"
          fi
        ;;
        6)echo "欢迎下次使用,谢谢!"
        ;;
        *) echo "暂无此功能,正在开发中..."
        ;;
esac

相关标签: shell脚本