crontab命令是cron table的简写,它是cron的配置文件,也可以叫它作业列表。
crontab常用的命令有:
crontab -e :编辑工作表
crontab -l :列出工作表里面的任务
crontab -r :删除工作表
可以通过crontab进入工作表编辑页面,是常见的vim 界面。基本上一行就是一条定时任务。
crontab的命令构成为 时间+动作,其时间按照书写顺序有:分、时、日、月、周五种。
比如:0 18 * * * 指的就是每天下午六点执行一次任务。
使用crontab -e 进行crontab定时任务的编写:
5 1 * * * cd /data/scripts/daily_vj_stat && sh daily_vj_stat_MECA.sh > daily_vj_stat_MECA.log 2>&1 &
15 0 * * * cd /data/scripts/daily_vj_stat && sh daily_vj_stat_SEA.sh > daily_vj_stat_SEA.log 2>&1 &
0 8 28-31 * * [[ "$(date --date=tomorrow +\%d)" == "01" ]] && cd /data/scripts/union_anchor_task_copy && sh anchor_task_copy.sh > anchor_task_copy.log 2>&1 &
0 8 28-31 * * [[ "$(date --date=tomorrow +\%d)" == "01" ]] && cd /data/scripts/union_anchor_task_copy && sh union_task_copy.sh > union_task_copy.log 2>&1 &
其中daily_vj_stat_MECA.sh:
tstr=`date -d '31 day ago' +'%Y-%m-%d %H:%M:%S'`
countryList="('中国', '日本')"
mysql -h xxx.xx.xx.xxx -p3306 -u账号 -p密码 -e "select DISTINCT(user_id) from live_record.live_record force index(start_time) where start_time > '$tstr' and is_signed = 1 and country_code in $countryList" |grep -v user_id > ./tmp/vj_user_id_MECA
echo "SELECT user_id from (SELECT user_id, country from (SELECT DISTINCT user_id, country FROM query.currency_record WHERE function_type in (28,103,118) AND country in $countryList AND op_type = 1 AND create_time > '$tstr') as a INNER join (SELECT DISTINCT user_id, country FROM query.currency_record WHERE function_type != 54 AND country in $countryList AND op_type = 1 AND create_time > '$tstr') as b ON a.user_id = b.user_id) as d inner join (SELECT user_id from query.verified_record WHERE country in $countryList AND verified = 1) as c ON d.user_id = c.user_id" | clickhouse-client >> ./tmp/vj_user_id_MECA
sort ./tmp/vj_user_id_MECA | uniq > ./vj_user_id_MECA
cat vj_user_id_MECA|while read line
do
curl -s -X GET 'http://xxx:80/admin/vj/data/daily?userId='$line'&split=true';
done
sstr=`date -d '1 day ago' +'%Y-%m'`
# MECA
curl -s -X GET "http://localhost:8080/admin/export/monthly_vj_salary_v2?region=MECA&statTimeStr=$sstr" -H 'did: 5up3rp0w3r'
echo "xlsx bind"
# MECA salary ranking MECA
curl -s -X GET "http://api-sandbox-internal.micoworld.net:80/admin/income/ranking/count?region=MECA&yearAndMonth=$sstr" -H 'did: 5up3rp0w3r'
echo "python wait"
sleep 10m
python /data/scripts/.junmo/tool/xlsx2csv.py /data/stat/monthly_vj_data_MECA_"$sstr".xlsx ./junmo_test/monthly_vj_data_MECA_"$sstr".csv
rm /data/stat/monthly_vj_data_3_MECA_"$sstr".xls
rm ./junmo_test/monthly_vj_data_MECA_"$sstr".csv
echo "end"