Install crontab
[root@tingnan-DB3 tandelindata]# yum install crontabs
Create script file
[root@tingnan-DB3 init.d]# touch a.sh
[root@tingnan-DB3 init.d]# chmod 777 111.sh
[root@tingnan-DB3 init.d]# vi 111.sh
/usr/bin/Rscript /tandelindata/ETL_code/R_oracle_mysql.R
Add a scheduled task
[root@tingnan-DB3 tandelindata]# crontab -e
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/
*/6 * * * * ./home/test_shell/test2.sh # 定时间隔6分钟执行一次
Start a scheduled task
[root@tingnan-DB3 tandelindata]# service crond restart # 重启定时任务, 有restart和reload两个命令
[root@tingnan-DB3 tandelindata]# service crond status # 查看执行报错情况
List a list of scheduled tasks
[root@tingnan-DB3 tandelindata]# crontab -l
View running results
[root@tingnan-DB3 tandelindata]# tail -f /var/log/cron # 执行日志查,发现
cron is set to start automatically at boot
[root@tingnan-DB3 tandelindata]# vi /etc/rc.local
[root@tingnan-DB3 tandelindata]# /bin/systemctl start crond.service
Other commonly used commands
service crond start //Start the service
service crond stop //Close the service
service crond restart //Restart the service
service crond reload //Reload the configuration
service crond status //View the service status
crontab -u //Set the user's cron service
crontab -l //View the cron scheduling content of the current user
crontab -r //Delete the user's cron service
crontab -e //Edit the user's cron service
crontab -i //Print prompt , Enter yes and other confirmation information
Script principle: Each segment of the timed task is: minute, hour, day, month, week, user, and command. For example: */5 * * * * ./home/test_shell/test2.sh # Execute once every 5 minutes
Execution result log
[root@tingnan-DB3 tandelindata]# tail -f /var/spool/mail/root # 执行结果日志
Schedule execution log results
The 111.sh script is as follows:
/usr/bin/Rscript /tandelindata/ETL_code/R_oracle_mysql.R
The results of the scheduling log script are as follows:
[root@tingnan-DB3 ~]# crontab -e
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/
*/6 * * * * /bin/sh /home/111.sh
The results of the R language program script are as follows:
library(DBI)
library(RMySQL)
library(RJDBC)
library(rJava)
### 创建Oracle数据库连接
setwd("//tandelindata//data_temp//") ## 在该路径下方架包:ojdbc6.jar,才可以读取Oracle数据库
drv<-JDBC("oracle.jdbc.driver.OracleDriver","ojdbc6.jar", identifier.quote="\"") ##java中JDBC的套路
conn<-dbConnect(drv,"jdbc:oracle:thin:@10.113.113.166:1520:XE","********","**********") ##建立一个连接,**XE**是服务名称
table1<-dbGetQuery(conn,"SELECT * FROM T_SYN_YJXX_ZK01") ##根据sql记录获取Oracle中表的数据
table2<-dbGetQuery(conn,"SELECT * FROM T_SYN_SSZB_ZK01") ##根据sql记录获取Oracle中表的数据
colnames(table1) <- iconv(colnames(table1),from="UTF-8",to="GBK") ## 转换对象数据编码和数据库编码统一,解决乱码
colnames(table2) <- iconv(colnames(table2),from="UTF-8",to="GBK") ## 转换对象数据编码和数据库编码统一,解决乱码
### 创建MySQL数据库连接
con <- dbConnect(MySQL(), host="*******", dbname="******", user="******", password="************")
dbWriteTable(con,"auto_coal_waring",table1,overwrite=T) ## 增量更新
dbWriteTable(con,"auto_coal_analysis",table2,overwrite=T) ## 增量更新
dbWriteTable(con,"auto_coal_waring_history",table1,append=T) ## 历史存储
dbWriteTable(con,"auto_coal_analysis_history",table2,append=T) ## 历史存储
The final data table is executed every 5 minutes to obtain the data collected by the sensor and store it in the database.