linux中crontab定时任务执行
- 0.crontab命令
- 开启与关闭 crontab 服务:
- 新增调度任务的两种方式:
- 查看调度任务:
- 1.shell命令封装为sh文件
- 2.sh文件赋权
- 3.crontab -e命令编辑
- 4.查看执行结果
- 5.优化借鉴
0.crontab命令
开启与关闭 crontab 服务:
service crond status #查看crontab服务状态
service crond start #启动crontab服务
service crond status #查看crontab服务状态
service crond stop #关闭crontab服务
service crond restart #重启crontab服务
service crond reload #重新载入配置
新增调度任务的两种方式:
1). 在命令行输入: crontab -e 然后添加相应的任务,最后 wq存盘退出。
2). 直接编辑 /etc/crontab 文件,即 vim /etc/crontab,添加相应的任务。
查看调度任务:
- crontab -l ,列出当前的所有调度任务
1.shell命令封装为sh文件
vim curl_fin_moderl_api.sh,内容如下:
#!/bin/bash
curl -H "Content-Type: application/json" -X POST "http://127.0.0.1:50000/label/regularApi/model_calc" -d'{"date_year":["2018","2019","2020","2021","2022"]}'
echo "$(date) run done!" > /data/project/szcsyth/modeling/five_forces/fin_model_curl.log
shell脚本保存:按Esc后,按:键,然后wq保存且退出
因为sh文件中有log文件,所以记得创建log文件
touch fin_model_curl.log
2.sh文件赋权
对创建的curl_fin_moderl_api.sh文件,进行755权限的赋权后
chmod 755 curl_fin_moderl_api.sh
ls命令后,文件为变绿色
3.crontab -e命令编辑
这里一定要把cron定时时间正确
如:0 1 * * * 表示:每天1点执行一次
*/5 * * * * 表示:每5分钟执行一次
编辑crontab -e,可以先每5分钟执行一次,查看命令正确性
crontab -e
*/5 * * * * /data/project/szcsyth/modeling/five_forces/curl_fin_moderl_api.sh
4.查看执行结果
cat fin_model_curl.log
tail -1000f fin_model_curl.log
查看日志,以及查看接口调用数据库,后者
tail -1000f fin-model-api.log
tail -1000f web.log
tail -1000f fin_model_curl.log
5.优化借鉴
#!/bin/bash
. /etc/profile
. ~/.bash_profile
datetime=$(date +%Y%m%d)
log_dir="/tlyth/python_script/full_link_gc/log/${datetime}.log"
if [ -f "${log_dir}" ];then
echo '有文件存在' >> ${log_dir}
else
touch ${log_dir}
fi
#full_link_sys_catalog_dept表更新
sh /tlyth/zhouze/kettle8.2/data-integration/pan.sh -file=/tlyth/python_script/full_link_gc/full_link_sys_catalog_dept.ktr >> ${log_dir}2>&1
echo '====tlx_full_link_data_update.sh=====Successsed:------------------------' >> ${log_dir}
优化1:
#!/bin/bash
datetime=$(date +%Y%m%d)
log_dir="/data/project/szcsyth/modeling/five_forces/fin_model_api_curl_log/${datetime}.log"
if [ -f "${log_dir}" ];then
echo '有文件存在' >> ${log_dir}
else
touch ${log_dir}
fi
#五力模型接口调用
curl -H "Content-Type: application/json" -X POST "http://127.0.0.1:50000/label/regularApi/model_calc" -d'{"date_year":["2018","2019","2020","2021","2022"]}' >> ${log_dir}2>&1
echo '====curl_fin_moderl_api.sh=====Successsed:------------------------' >> ${log_dir}
优化2:
#!/bin/bash
datetime=$(date +%Y%m%d)
log_dir="/data/project/szcsyth/modeling/five_forces/seed_pool_api_curl_log/${datetime}.log"
if [ -f "${log_dir}" ];then
echo '有文件存在' >> ${log_dir}
else
touch ${log_dir}
fi
#种子池模型接口调用
curl -H "Content-Type: application/json" -X POST "http://127.0.0.1:50000/label/regularApi/model_calc_seed_pool" -d'{"date_year":["2022"]}' >> ${log_dir}2>&1
echo '====curl_seed_pool_api.sh=====Successsed:------------------------' >> ${log_dir}