1.run_py.sh
#!/bin/sh NAME=$1 # $1 运行时输入参数 为文件名称 NAME=${NAME%%.*} if [ -z "$NAME" ]; then echo "STRING is empty" NAME="aa" fi echo $NAME ID=`ps -ef | grep "$NAME" | grep -v "$0" | grep -v "grep" | awk '{print $2}'` echo $ID echo "---------------" for id in $ID do kill -9 $id echo "killed $id" done echo "---------------" sleep 1 current_dir=$(cd $(dirname $0); pwd) echo $current_dir if [ ! -d "$current_dir/logs" ]; then echo "$current_dir/logs does not exist" `mkdir $current_dir/logs` fi echo "---------------" echo "nohup /usr/bin/python3.6 $current_dir/$NAME.py > $current_dir/logs/$NAME.log 2>&1 &" echo "---------------" echo "tail -f $current_dir/logs/$NAME.log" `nohup /usr/bin/python3.6 $current_dir/$NAME.py > $current_dir/logs/$NAME.log 2>&1 &` echo "启动成功"
2.run_py.py
# -*- coding: UTF-8 -*- import concurrent.futures import os import platform import re import subprocess from yscredit_tools import Logger Logger.config(level="info", processname=os.path.splitext(os.path.basename(__file__))[0]) logger = Logger # def run_main(): # system = platform.system() # path = os.path.abspath('.') # for spider_name in os.listdir("."): # if spider_name.endswith(".py") and "ggzyjy" in spider_name: # if 'indo' in system: # spider_path = '{}\{}'.format(path, spider_name) # else: # spider_path = '{}/{}'.format(path, spider_name) # logger.info(spider_path) # res = subprocess.call("python {}".format(spider_path), shell=True) # logger.info(res) def run_spider(spider_path): logger.info(spider_path) try: if os.environ.get("yu_localhosts", "") == "localhost": res = subprocess.call("python {}".format(spider_path), shell=True) else: res = subprocess.call("python3.6 {}".format(spider_path), shell=True) logger.info(res) except Exception as e: logger.error("爬虫启动失败:{]".format(e)) if __name__ == '__main__': # run_main() for i in range(1): try: data_list = [] system = platform.system() path = os.path.abspath('.') for spider_name in os.listdir("."): if spider_name.endswith(".py") and "ggzyjy" in spider_name: index = re.findall(r'\d+', spider_name) if index: index = int(index[0]) else: index = 0 if index != 14: if 'indo' in system: spider_path = '{}\{}'.format(path, spider_name) else: spider_path = '{}/{}'.format(path, spider_name) data_list.append(spider_path) with concurrent.futures.ThreadPoolExecutor(max_workers=59) as executor: future_to_url = {executor.submit(run_spider, data): data for data in data_list} except Exception as e: print("报错")
标签:shell,run,name,python,spider,echo,path,dir,NAME From: https://www.cnblogs.com/yoyo1216/p/17121960.html