Skip to content

Instantly share code, notes, and snippets.

@ultranity
Created August 9, 2022 14:24
Show Gist options
  • Save ultranity/d672ba80b79a4d9b0e2c807c2a106260 to your computer and use it in GitHub Desktop.
Save ultranity/d672ba80b79a4d9b0e2c807c2a106260 to your computer and use it in GitHub Desktop.
Slurm-sbatch-email-with-output
#!/bin/bash
#SBATCH -J MyModel
#SBATCH -n 1 # Number of cores
#SBATCH -t 1-00:00 # Runtime in D-HH:MM
#SBATCH -o JOB%j.out # File to which STDOUT will be written
#SBATCH -e JOB%j.out # File to which STDERR will be written
#SBATCH --mail-type=BEGIN
#SBATCH [email protected]
secs_to_human(){
echo "$(( ${1} / 3600 )):$(( (${1} / 60) % 60 )):$(( ${1} % 60 ))"
}
start=$(date +%s)
echo "$(date -d @${start} "+%Y-%m-%d %H:%M:%S"): ${SLURM_JOB_NAME} start id=${SLURM_JOB_ID}\n"
### exec task here
( << replace with your task here >> ) \
&& (cat JOB$SLURM_JOB_ID.out |mail -s "$SLURM_JOB_NAME Ended after $(secs_to_human $(($(date +%s) - ${start}))) id=$SLURM_JOB_ID" [email protected] && echo mail sended) \
|| (cat JOB$SLURM_JOB_ID.out |mail -s "$SLURM_JOB_NAME Failed after $(secs_to_human $(($(date +%s) - ${start}))) id=$SLURM_JOB_ID" [email protected] && echo mail sended && exit $?)
@hanshanmengqi
Copy link

确实不错,可以成功发送邮件!
手动点赞!!

@avivajpeyi
Copy link

This is great! do you have an example on how logs/plots can be attached?

@ultranity
Copy link
Author

This is great! do you have an example on how logs/plots can be attached?

in this snippet, only default stderr&stdout log will be attached as content by cat JOB$SLURM_JOB_ID.out and pipe to mailx

for more logs/plots, you need to check how the mail client in your env supports adding attachments. as ref:https://stackoverflow.com/questions/902591/how-to-attach-a-file-using-mail-command-on-linux

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment