如何使用带有python脚本的分离屏幕进程来启动多个python脚本

时间:2019-05-22 22:26:40

标签: bash python-2.7

我正在尝试在分离的屏幕上运行多个python脚本。我正在创建一个python脚本来启动每个屏幕会话,但是它们没有运行。我认为这与subprocess.Popen有关。直接放入命令行时,我在Popen中放入的命令起作用。当前,它会循环执行,并打印应运行的命令,但脚本不会运行。

我添加了完整的文件路径,以确保所有命令都可以运行。我已经复制了应该直接从控制台运行的命令(在尝试运行该命令之前,我先将其打印出了该命令),然后直接粘贴到控制台中并且可以正常工作。我浏览了许多帖子,但找不到涵盖我确切问题的帖子。


count = 0
generic_command = 'screen -dm bash -c "source /home/<user>/py2/bin/activate; python /home/<user>/folder/{f} {arg1} {arg2} > {log_file}"'

generic_log_file = '/home/<user>/folder/logs/log_{file_type}_{date}_{time}.txt'



to_run = {
    'file':[
        'run_brands.py',
        'run_brands.py',
    ],
    'arg1':[
        'category',
        'subcategory',
    ],
    'arg2':[
        '3',
        '8',
    ],
    'file_type':[
        'string_to_add',
        'string_to_add_2',
    ]
}


while count < len(to_run['file']):

    now = datetime.datetime.now()
    date = now.date()
    time = now.time()
    addition = ''
    if date.month < 10:
        addition = '0'

    curr_date = "{m}{d}{y}".format(m = addition + str(date.month), 
         d = date.day, y = str(date.year)[2:])
    curr_time = "{h}{m}".format(h = time.hour, m = time.minute)

    log_file = generic_log_file.format(file_type = to_run['file_type'][count], 
        date = curr_date, time = curr_time)

    curr_command = generic_command.format(count = count, 
        f = to_run['file'][count], 
        arg1 = to_run['arg1'][count], 
        arg2 = to_run['arg2'][count], 
        log_file = log_file)

    print('Running command: ' + curr_command)

    process = subprocess.Popen(curr_command.split(), stdout=subprocess.PIPE)

    output, error = process.communicate()

    count += 1
print 'Finished starting loop'

我希望在此脚本完成后,屏幕可以分离并运行。屏幕中的脚本具有输出,该输出将被重定向到同样未创建的文件。

0 个答案:

没有答案