Search code examples
slurmhpc

Write commands run by slurm to the log


I am running several computations on an HPC server and I use slurm to send commands to the queue. To submit jobs I use a sbatch script in which I set all the parameters for slurms and then have a series of commands to run. I'd like to have all the commands that have been run written in the log, in such a way that even if I change the sbatch script I always have a log of everything that happened.

For example for such a script:

SLURM Frontmatter

Command1
Command2
Command3

I'd like the log to show something like the following:

SLURM: Running Command1
<Command1 output>
SLURM: Running Command2
<Command2 output>
SLURM: Running Command3
<Command3 output>

I have tried using echo for this, but it's not very maintainable. Every time I change the script then I also have to change the echo blocks, which is annoying and very prone to errors. I'd like to know if there's any automatic way to generate this kind of messages.


Solution

  • You can start Bash in verbose mode, by changing the first line (shebang) from something like #!/bin/bash or #!/usr/bin/env bash to #!/bin/bash -v or #!/usr/bin/env bash -v

    This will print each command in the script just before executing it.

    Alternatively, you can use the

    set -v
    

    command in your script to enable logging from that point on, and

    set +v
    

    to later disable it.