简体   繁体   English

使用python自动创建bash脚本

[英]Use python to automate creation of bash scripts

I'm trying to get some help for a problem which I've laid out here , and from some further research I think that a Python script might be the answer.我正在尝试为我在这里提出的问题寻求帮助,并且从一些进一步的研究中,我认为 Python 脚本可能是答案。 That said, I'm new to Python and not sure how to implement what I have in mind, or if it would be the right thing.也就是说,我是 Python 的新手,不确定如何实现我的想法,或者这是否正确。

Essentially I think I need a python script which can take variables which I pass it and then write those variables into .sh files.基本上我认为我需要一个 python 脚本,它可以接受我传递的变量,然后将这些变量写入 .sh 文件。 Is there a simple way to do this?有没有一种简单的方法可以做到这一点?

EDIT: In response to a couple of comments, I think I ought to spell out my problem a little more.编辑:为了回应一些评论,我认为我应该更详细地说明我的问题。

I'm running a matlab function via a SLURM script.我正在通过 SLURM 脚本运行 matlab 函数。 The SLURM script is (I think) a kind of bash script but specifically for scheduling jobs on an HPC. SLURM 脚本(我认为)是一种 bash 脚本,但专门用于在 HPC 上调度作业。 My problem is that I want to, for example, submit ten jobs at once, but all with a particular variable changed to some value.我的问题是,例如,我想一次提交 10 个作业,但所有作业都将特定变量更改为某个值。 Now, the problem is I can't just do this in a simple way because as far as I can tell there's no good way of passing variables to SLURM scripts.现在,问题是我不能简单地做到这一点,因为据我所知,没有将变量传递给 SLURM 脚本的好方法。 So what I'm doing currently is literally having ten versions of the submission script, each with their own fixed variable - and then when I want to submit all the jobs I open each of these ten scripts and update the shared variable, manually, and then run them one by one.所以我目前正在做的是实际上有十个版本的提交脚本,每个版本都有自己的固定变量 - 然后当我想提交所有作业时,我打开这十个脚本中的每一个并手动更新共享变量,然后然后一一运行。 I think what I'm after is a python script which will go into each of these SLURM scripts and edit them.我想我所追求的是一个 python 脚本,它将进入每个 SLURM 脚本并对其进行编辑。

There are two options that could solve your problem:有两个选项可以解决您的问题:

  1. You could use slurm arrays你可以使用slurm 数组

An example below cycles through the numbers 1-16:下面的示例循环遍历数字 1-16:

#!/bin/bash

#SBATCH --job-name=array
#SBATCH --output=array_%A_%a.out
#SBATCH --error=array_%A_%a.err
#SBATCH --array=1-16
#SBATCH --time=01:00:00
#SBATCH -p partition-name
#SBATCH --ntasks=1
#SBATCH --mem=4G

# Print the task id.
echo "My SLURM_ARRAY_TASK_ID: " $SLURM_ARRAY_TASK_ID

# run code
./exec ${SLURM_ARRAY_TASK_ID}-inputfile.i
  1. You could use python Template strings你可以使用 python 模板字符串

Here is an example这是一个例子

in generate-scripts.py below it cycles through 0-10, but you could make it substitute in strings or anything--it's powerful:在下面的 generate-scripts.py 中,它在 0-10 之间循环,但您可以将其替换为字符串或任何东西——它很强大:

template = None
# Create input files from boiler plate
with open(templatefname, "r") as template_f:
    template_string = template_f.read()
    template = Template(template_string)

for i in range(10):
    newfilestrin = template.safe_substitute(var=str(i))

then in your template file you could have:然后在您的模板文件中,您可以拥有:

# blah blah
./run ${var}

I've used both solutions.我已经使用了这两种解决方案。 SLURM arrays are convenient if your changing inputs are just increasing integers.如果您更改的输入只是增加整数,则 SLURM 数组很方便。 Python template strings or files are more powerful, but a bit more work. Python 模板字符串或文件更强大,但需要做更多的工作。

To answer your actual SLURM-related question, it looks like you should just use --export when creating the job to define variables, which are then available as environment variables for your job:要回答您与 SLURM 相关的实际问题,您似乎应该在创建作业时使用--export来定义变量,然后这些变量可用作您的作业的环境变量:

sbatch --export=A=5,b='test' jobscript.sbatch

Quoting the manual for completeness:引用手册的完整性:

--export=<environment variables [ALL] | NONE>

Identify which environment variables from the submission environment are propagated to the launched application.确定将提交环境中的哪些环境变量传播到启动的应用程序。 By default, all are propagated.默认情况下,所有都被传播。 Multiple environment variable names should be comma separated.多个环境变量名称应以逗号分隔。 Environment variable names may be specified to propagate the current value (eg "--export=EDITOR") or specific values may be exported (eg "--export=EDITOR=/bin/emacs").可以指定环境变量名称来传播当前值(例如“--export=EDITOR”)或可以导出特定值(例如“--export=EDITOR=/bin/emacs”)。 [...] [...]

This creates files with the same content but different numbers这将创建内容相同但编号不同的文件

    def writeFile(number):
        f = open(filename+str(number)+".slurm","w")
        f.write(content+str(number)+content)
        f.close()

I would suggest simple-slurm , a Python wrapper for Slurm I developed.我建议使用simple-slurm ,这是我开发的 Slurm的 Python 包装器。 Then, you can use simple Python logic and variables to create the jobs.然后,您可以使用简单的 Python 逻辑和变量来创建作业。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM