linux中crontab定时任务执行

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}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值