您的位置:首页 > 运维架构 > Shell

指定月份&起始天和终止天串行提交shell命令

2015-10-16 11:55 591 查看
我想需求是这样的:

比如指定月份是201508,8月份起始天是1号,8月份终止天是31号

这样我可以提交hadoop任务补8月份数据

同时我还要求提交的前一个任务完成,才能运行下一个任务,这也是业务要求的,所以自己就用shell和python实现这个需求了,时间很紧就是在晚上睡前写完的

shell

我这个只是简单的调用“touch”命令创建文件,把调用的shell命令改成相应的hadoop或者其他命令就OK

#!/bin/bash

month=$1
start_date=$2
end_date=$3

if [ $start_date -lt 10 ];then
touch /Users/wangyue/Downloads/${month}0${start_date}
returncode=$?
start_date=`expr $start_date + 1`
else
touch /Users/wangyue/Downloads/${month}${start_date}
returncode=$?
start_date=`expr $start_date + 1`
fi

while [ $start_date -le $end_date  ];do
if [ $returncode -eq 0 ];then

if [ $start_date -lt 10 ];then
touch /Users/wangyue/Downloads/${month}0${start_date}
a=$?
start_date=`expr $start_date + 1`
else
touch /Users/wangyue/Downloads/${month}${start_date}
a=$?
start_date=`expr $start_date + 1`
fi
fi
done


调用脚本:

sh scheduleTask.sh 201508 9 11


python

我这个只是简单的调用“mkdir”命令目录,把调用的shell命令改成相应的hadoop或者其他命令就OK

# -*- coding: utf-8 -*-
__author__ = 'wangyue'

import subprocess
import os

def run_cmd(cmd):
print '[CMD] ' + cmd
# return subprocess.call(cmd, shell=True)
return os.system(cmd)

def main():
month = sys.argv[1]  # 201508
start_date = sys.argv[2]  # 8
end_date = sys.argv[3]  # 11

resultcode = run_cmd(
'mkdir  ~/Downloads/' + str(month) + (('0' + str(start_date)) if start_date < 10 else str(start_date)))
start_date = start_date + 1

while (0 == resultcode and start_date <= end_date):
resultcode = run_cmd(
'mkdir  ~/Downloads/' + str(month) + (('0' + str(start_date)) if start_date < 10 else str(start_date)))
start_date = start_date + 1

if __name__ == '__main__':
main()


调用脚本:

python schedueTask.py 201508 9 18


作者:stark_summer

出处:http://write.blog.csdn.net/mdeditor
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: