在这里,我们将给大家分享关于Centos的Shell脚本定时执行SQL语句的知识,让您更了解shell脚本执行定时任务的本质,同时也会涉及到如何更有效地bash–每次创建新用户时执行shell脚本、C
在这里,我们将给大家分享关于Centos 的 Shell 脚本定时执行SQL语句的知识,让您更了解shell脚本执行定时任务的本质,同时也会涉及到如何更有效地bash – 每次创建新用户时执行shell脚本、CentOS 7 Shell脚本编程第二讲 Shell 脚本创建和执行、centos crontab定时运行shell脚本、CentOS 中定时执行脚本的内容。
本文目录一览:- Centos 的 Shell 脚本定时执行SQL语句(shell脚本执行定时任务)
- bash – 每次创建新用户时执行shell脚本
- CentOS 7 Shell脚本编程第二讲 Shell 脚本创建和执行
- centos crontab定时运行shell脚本
- CentOS 中定时执行脚本
Centos 的 Shell 脚本定时执行SQL语句(shell脚本执行定时任务)
目的:解决每天重复刷新数据的问题;
1. 编写Shell 脚本excute_MysqL.sh
#!/bin/bash # Define log TIMESTAMP=`date +%Y%m%d%H%M%s` LOG=call_sql_${TIMESTAMP}.log echo "Start execute sql statement at `date`." >>${LOG} #o execute sql stat MysqL -e ' use onem2bak; UPDATE `onem2bak`.`system_function` SET `security_leven` = "low" where 1=1; quit'
因为我是使用MysqL本地免密登录,如果你设置了密码就将MysqL -e 改成 MysqL -uroot -p123456 -e;
2. 设置执行权限
chmod 777 excute_MysqL.sh
3. 设置定时任务执行
#修改定时任务 crontab -e 新增一行 59 23 * * * /home/shell/excute_MysqL.sh #查看定时任务 crontab -l好了,到此结束。
bash – 每次创建新用户时执行shell脚本
我检查了/etc/adduser.conf但是没有什么东西在那里看起来会这样做?
我想将它添加到用户.bashrc,以便jsut在登录时执行,但我宁愿在登录之前进行设置.
任何帮助,将不胜感激.
If the file /usr/local/sbin/adduser.local exists,it will be exe‐ cuted after the user account has been set up in order to do any local setup. The arguments passed to adduser.local are: username uid gid home-directory
您似乎可以在此处添加用户创建操作.
CentOS 7 Shell脚本编程第二讲 Shell 脚本创建和执行
首先说明一点,shell和python在语法上有很多相同点,比如都不强制以不以;结尾,都是弱类型脚本语言。
[root@promote ~]# yum install vim -y
Shell 脚本通常用编辑器vim创建和修改。也可以使用其他编辑器,例如nano等。
创建脚本users.sh。用于显示当前用户。
[root@promote ~]# vim users.sh
#按i进入编辑模式输入who
#按Esc 退出编辑模式
#输入:wq 保存文件
#授予脚本可执行权限
[root@promote ~]# chmod +x users.sh
#查看当前目录文件
[root@promote ~]# ls
#执行脚本
[root@promote ~]# ./users.sh
root tty1 2019-03-31 11:17
root pts/0 2019-03-31 14:08 (192.168.216.1)
[root@promote ~]#
可以看出当前用户是root,分别在本机和192.168.216.1远程登录。vim快捷键还可以使用w保存修改,q退出编辑,q!不保存强制退出编辑。
下文正式开始Shell脚本编写。该脚本执行安装epel-release和htop两个软件。
[root@promote ~]# vim install_htop.sh
#!/bin/bash
#Date: 2019-03-31 14:55:55
#Author: WeiWei
#Version: v1.0
#install epel-release
yum install epel-release -y
#install htop
yum install htop -y
#!/bin/bash用于指定脚本解释器,本例指定解释器为/bin/bash,通常脚本还加上编写时间、作者和版本等信息,用于满足Shell编程规范推荐。从该脚本开始需要增加以上注释内容。
#开头为注释,通常位于脚本前后行或脚本行尾,解释器不会加以解释执行。脚本增加注释可以帮助自己和他人理解脚本用途,通常脚本不建议使用中文字符。
脚本文件如何执行呢?除了第一讲介绍方法外,下文介绍第二和第三种方法。
#和第一种方法缺少chmod赋予执行权限过程
[root@promote ~]# bash install_htop.sh
#第三种方法
[root@promote ~]# source install_htop.sh
脚本执行过程中,会查找系统环境变量。env命令可以详细显示环境变量。
[root@promote ~]# env
思考内容:是否有其他命令显示系统变量?
centos crontab定时运行shell脚本
1、安装yum install crontabs
2、创建脚本touch test.sh
3、编辑脚本vi test.sh
编辑后的test.sh还不是可执行文件,需要执行chmod 700 test.sh
4、编辑crontab -e (类似于vi操作)
20 23 * * * /home/pe/test.sh
表示每天晚上23:20运行脚本test.sh
5、手动启动crontab服务:service crond start
深入学习:
http://www.ha97.com/910.html
https://www.cnblogs.com/chen-lhx/p/5996781.html
https://www.cnblogs.com/kerrycode/p/3238346.html
https://blog.csdn.net/baidu_35757025/article/details/64437792
CentOS 中定时执行脚本
一:安装定时工具
默认安装的 centos 竟然没有 crontabs 模块,于是我们手动安装:(如果已安装,不需要再安装啦)
yum install vixie-cron crontabs
把 crond 服务添加到系统启动项
chkconfig crond on
启动 crond 服务
service crond start
说明:
service crond start // 启动服务
service crond stop // 关闭服务
service crond restart // 重启服务
service crond reload // 重新载入配置
查看 crontab 服务状态:service crond status
查看 crontab 服务是否已设置为开机启动,执行命令:ntsysv
加入开机自动启动:
chkconfig –level 35 crond on
二:添加定时任务
#vim /etc/crontab
进入这个文件,并在其后添加
17 01 0 * * * root /data/backup/168/backup.sh
18 01 0 * * * root /data/backup/ty/backup.sh
三:使用说明
1,crontab 命令
功能说明:设置计时器。
语 法:crontab [-u < 用户名称>][配置文件] 或 crontab [-u < 用户名称 >][-elr]
补充说明:cron 是一个常驻服务,它提供计时器的功能,让用户在特定的时间得以执行预设的指令或程序。只要用户会编辑计时器的配置文件,就可以使 用计时器的功能。其配置文件格式如下:
Minute Hour Day Month DayOFWeek Command
参 数:
-e 编辑该用户的计时器设置。
-l 列出该用户的计时器设置。
-r 删除该用户的计时器设置。
-u <用户名称> 指定要设定计时器的用户名称。
2,crontab 格式
基本格式 :
* * * * * command
分 时 日 月 周 命令
第 1 列表示分钟 1~59 每分钟用 * 或者 */1 表示
第 2 列表示小时 1~23(0 表示 0 点)
第 3 列表示日期 1~31
第 4 列 表示月份 1~12
第 5 列标识号星期 0~6(0 表示星期天)
第 6 列要运行的命令
# Use the hash sign to prefix a comment
# +—————- minute (0 – 59)
# | +————- hour (0 – 23)
# | | +———- day of month (1 – 31)
# | | | +——- month (1 – 12)
# | | | | +—- day of week (0 – 7) (Sunday=0 or 7)
# | | | | |
# * * * * * command to be executed
crontab 文件的一些例子:
30 21 * * * /etc/init.d/nginx restart
每晚的 21:30 重启 nginx。
45 4 1,10,22 * * /etc/init.d/nginx restart
每月 1、 10、22 日的 4 : 45 重启 nginx。
10 1 * * 6,0 /etc/init.d/nginx restart
每周六、周日的 1 : 10 重启 nginx。
0,30 18-23 * * * /etc/init.d/nginx restart
每天 18 : 00 至 23 : 00 之间每隔 30 分钟重启 nginx。
0 23 * * 6 /etc/init.d/nginx restart
每星期六的 11 : 00 pm 重启 nginx。
* */1 * * * /etc/init.d/nginx restart
每一小时重启 nginx
* 23-7/1 * * * /etc/init.d/nginx restart
晚上 11 点到早上 7 点之间,每 隔一小时重启 nginx
0 11 4 * mon-wed /etc/init.d/nginx restart
每月的 4 号与每周一到周三 的 11 点重启 nginx
0 4 1 jan * /etc/init.d/nginx restart
一月一号的 4 点重启 nginx
*/30 * * * * /usr/sbin/ntpdate 210.72.145.20
每半小时同步一下时间
关于Centos 的 Shell 脚本定时执行SQL语句和shell脚本执行定时任务的问题就给大家分享到这里,感谢你花时间阅读本站内容,更多关于bash – 每次创建新用户时执行shell脚本、CentOS 7 Shell脚本编程第二讲 Shell 脚本创建和执行、centos crontab定时运行shell脚本、CentOS 中定时执行脚本等相关知识的信息别忘了在本站进行查找喔。
本文标签: