【问题标题】:How does one make sure that the python submission script in slurm is in the location from where the sbatch command was given?如何确保 slurm 中的 python 提交脚本位于发出 sbatch 命令的位置?
【发布时间】:2017-10-12 20:38:34
【问题描述】:

我有一个 python 提交脚本,我使用sbatch 使用slurm 运行:

sbatch batch.py

当我这样做时,事情不能正常工作,因为我假设batch.py 进程没有继承正确的环境变量。因此,不是从完成sbatch 命令的地方运行batch.py,而是从其他地方运行(/ 我相信)。我已经设法通过用 bash 脚本包装 python 脚本来解决这个问题:

#!/usr/bin/env bash
cd path/to/scripts
python script.py

这种临时的破解方式似乎有效,但它似乎完全避免了这个问题,而不是解决它。有人知道如何以更好的方式解决此问题吗?

例如,我知道在 docker 中存在 -w 或 -WORKDIR,以便 docker 容器知道它应该在哪里。我想知道 slurm 是否存在类似的东西。

【问题讨论】:

  • 也许:--workdir=<directory> Set the working directory of the batch script to directory before it is executed. The path can be specified as full path or relative path to the directory where the command is executed.
  • 你看过 sbatch --get-user-env 吗?这可能是你需要的。否则,如果仅在当前终端中添加环境变量,您可能需要使用 sbatch --export 显式传递您需要的变量。
  • 不确定给--get-user-env看是什么意思,你的意思是看标志还是环境变量?

标签: python bash slurm sbatch


【解决方案1】:

Slurm 旨在将用户的环境在提交时推送到作业,但用户或系统管理员明确禁用的变量除外。

但是脚本的运行方式如下:脚本被复制到分配的主节点上的 Slurm 特定目录中并从那里运行,$PWD 设置为 sbatch 命令所在的目录已运行。

你可以通过一个像这样的简单脚本看到这一点:

$ cat t.sh
#!/bin/bash
#
#SBATCH --job-name=test_ms
#SBATCH --output=res_ms.txt

echo $PWD
dirname $(readlink -f "$0")

$ sbatch t.sh
Submitted batch job 1109631
$ cat res_ms.txt
/home/damienfrancois/
/var/spool/slurm/job1109631

一个后果是在当前目录中导入模块的 Python 脚本无法这样做。解决方法是在导入失败之前显式添加sys.path.append(os.getcwd())。

【讨论】:

  • 好的,所以根据你在 bash 提交脚本中回显 $CWD 应该给我一些东西......但它目前给了我一个空的 .out 文件。
  • 抱歉,不是$CWD,而是$PWD。我更正并更新了我的答案
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-11-19
  • 1970-01-01
  • 2022-01-04
  • 2014-08-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多