首页 > 系统相关 >run_py.sh shell 启动python程序

run_py.sh shell 启动python程序

时间:2023-02-15 10:58:55浏览次数:47  
标签:shell run name python spider echo path dir NAME

 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

相关文章

  • vs code怎么设置python解释器
    首先打开一个python文件,如下图所示 右键单击选择命令面板,如下图所示 接着输入python:select选择第一个 然后点击盘符,如下图所示......
  • Python+Django(1):建立项目
    为项目新建一个目录,将其命名为learning_log,再在终端中切换到这个目录(Python3):运行模块venv来创建一个名为ll_env的虚拟环境:python-mvenvll_env激活虚拟环境:ll_env\S......
  • 【编程基础之Python】2、安装Python环境
    (【编程基础之Python】2、安装Python环境)安装Python环境所谓“工欲善其事,必先利其器”。在学习Python之前需要先搭建Python的运行环境。由于Python是跨平台的,支持在各种......
  • Python3 注释
    单行注释# 多行注释1.单引号''''''2.双引号"""""" 文档注释注释应该放在函数的第一行'''文档注释'''defa():'''这是文档字符......
  • Python文件的操作处理,一看就会
    ​每天进步一点点,关注我们哦,每天分享测试技术文章本文章出自【码同学软件测试】码同学公众号:自动化软件测试,领取资料可加:magetest码同学抖音号:小码哥聊软件测试​在读......
  • Python从PPT文件中提取所有文字到Word
    需求将PPT文件或PPTX文件里面的所有文字提取到一个新的以docx结尾的Word文件中。安装Python库(1)基于Python3(2)运行下方代码安装需要用到的库pipinstallpython-pptx......
  • Python 虚拟环境管理工具 venv
    1.概述Python应用程序通常会使用不在标准库内的软件包和模块。应用程序有时需要特定版本的库,因为应用程序可能需要修复特定的错误,或者可以使用库的过时版本的接口编写应......
  • Python正则表达式
    使用正则表达式正则表达式相关知识在编写处理字符串的程序或网页时,经常会有查找符合某些复杂规则的字符串的需要,正则表达式就是用于描述这些规则的工具,换句话说正则表达......
  • python re.match() / re.search() / re.findall()
    re.match函数re.match尝试从字符串的起始位置匹配一个模式,如果不是起始位置匹配成功的话,match()就返回none。re.search方法re.search扫描整个字符串并返回第一个成......
  • 【Shell】流程控制
    目录流程控制:ifelse语句if条件判断句的退出状态Shell内置命令:test流程控制:ifelse语句if条件判断句的退出状态Shell内置命令:test......