0

I'm new to slurm, and I'm trying to batch a shell script to write to a text file. My shell script (entitled "troublesome.sh") looks like this:

#!/bin/bash
#SBATCH -N 1
#SBATCH -n 1

echo "It worked!"

When I run sh troublesome.sh > doeswork.txt it writes "It worked!" to doeswork.txt as expected. However, when I run sbatch troublesome.sh > doesnotwork.txt, the resulting file contains only "Submitted batch job 3027448." I've successfully used sbatch for more complex commands before, and I'm completely befuddled as to why this simple shell script isn't working.

Any thoughts as to what I'm doing wrong? Thank you very much!

Kate
  • 1
  • 1
  • 2

2 Answers2

0

The sbatch command only outputs the ID assigned to the job submitted. The output of the submission script is written to a file, specified by the --output=<filename pattern> and --error=<filename pattern> parameters (Cf. the sbatch manpage

The file is created once the job starts. By default, it is named

"slurm-%j.out", where the "%j" is replaced by the job ID

So there should be a slurm-3027448.out file in the directory where you ran the sbatch command containing the "It worked!" string.

You can of course name that file the way you want. If the filename does not contain the job ID, it will be truncated (emptied) upon job start, unless you use the --open-mode=append parameter, in which case the new content will be appended to the file.

Another option to append rather the overwrite is to add this line near the beginning of the submission script:

exec >> doeswork.txt

This will redirect all output to the doeswork.txt file as if all commands in the script were followed by >> doeswork.txt.

damienfrancois
  • 52,978
  • 9
  • 96
  • 110
  • Thanks very much! Is there a good way for me to redirect the output into a target file (here, doesnotwork.txt) rather than creating a separate output file? In this example, I was trying to overwrite an exciting file, but I'd eventually like to add with >> notation. – Kate Jun 25 '20 at 22:30
  • I updated my answer with regard to appending rather than overwriting – damienfrancois Jun 27 '20 at 15:11
0

To get the output in a file, add >> output_file to the command (here echo "it worked")inside the sbatch script. Because, the command inside sbatch is executed on all nodes and writes it into stdout. The sbatch command just submit the job to the controller and only returns the output to the login node about the job submission.

#!/bin/bash
#SBATCH -N 1
#SBATCH -n 1

echo "It worked!" >> output_file

Why so?

sbatch exits immediately after the script is successfully transferred to the Slurm controller and assigned a Slurm job ID. So, it is the only output of the command sbatch some_script.sh >> somefile.txt. Thats why you will only get information about submitting jobs to the controller.

The batch script is not necessarily granted resources immediately, it may sit in the queue of pending jobs for some time before its required resources become available.

As in the answer by damienfrancois, the file is created when job is started. By default both standard output and standard error are directed to a file of the name "slurm-%j.out", where the "%j" is replaced with the job allocation number. The file will be generated on the first node of the job allocation. Other than the batch script itself, Slurm does no movement of user files.

When the job allocation is finally granted for the batch script, Slurm runs a single copy of the batch script on the first node in the set of allocated nodes.

j23
  • 3,139
  • 1
  • 6
  • 13