<p>You could add an exit trap to the beginning of the shell scripts inside your Snakefile, which is executed after each script is done and sends a message when there was an error:</p>

<pre><code> shell:
    """
    trap 'if [ $? -neq 0 ]; then ping_slack -m "Error in rule xxx with input {input}"; exit 1' EXIT
    ...
</code></pre>

CC BY-SA 4.0
2019-08-21T15:30:25.830Z
I believe this needs to be added to all of workflow rules. I am hoping to set a trap similar to existing `onerror` handler of [snakemake](https://snakemake.readthedocs.io/en/stable/snakefiles/rules.html#onstart-onsuccess-and-onerror-handlers) but prefer to execute it at completion of each rule rather an entire workflow.
Samir
CC BY-SA 4.0
2019-08-22T05:36:09.557Z
yes, that would be needed in each rule.
Peter Menzel
CC BY-SA 4.0
2019-08-21T15:00:44.933Z
2019-08-21T15:00:44.933Z
Peter Menzel
<p>When a rule fails in snakemake workflow, it returns a descriptive message in log file like following:</p>

<pre><code>Error in rule step1:
    jobid: 99
    output: results/foo/step1/sample5/outdir/output1.tsv, results/foo/step1/sample5/outdir/output2.tsv
    log: logs/step1/sample5_out.log (check log file(s) for error message)
    shell:
    ...
        (exited with non-zero exit code)
</code></pre>

<p>Since I run workflow over many samples, I have defined a <em>postrun.sh</em> bash script in <em>jobscript.sh</em> directive that runs after snakemake <code>{exec_job}</code> section and notifies me via userbot in slack channel when a step failed for one or more samples.</p>

<p><strong>jobscript.sh</strong></p>

<pre class="lang-sh prettyprint-override"><code>        ########################### START SNAKEMAKE CMD ############################
    {exec_job}
        ############################ END SNAKEMAKE CMD #############################
    exitstat=$?
    export exitstat

    echo "END at <span class="math-container">$(date)"
. "$</span>{{HOME}}"/.smk_confs/postrun.sh
</code></pre>

<p><strong>postrun.sh</strong></p>

<pre class="lang-sh prettyprint-override"><code>## execute after running each of snakemake job
echo 'Running postrun.sh'
ERRMSG="MYJOB ID: <span class="math-container">$PBS_JOBID failed at $</span>(pwd) on <span class="math-container">$(hostname) for $</span>{USER} with exit status: ${exitstat}."

## ping to slack
ping_slack -m "$ERRMSG"
sleep 5
</code></pre>

<p>I am looking for a snakemake exported environment variable that I can use in <em>postrun.sh</em> to know which rule ID failed for one or more samples. For now, I can think of using job scheduler variable matching job name, e.g, PBS_JOBNAME, but better if I have a variable from snakemake API for rule ID or preferably, path to log file.</p>

<p>Thanks</p>

<snakemake>
snakemake get environment variable for rule name and log path
# snakemake get environment variable for rule name and log path

(Asked by: username_0 on 2019-08-21)

When a rule fails in snakemake workflow, it returns a descriptive message in log file like following:
Error in rule step1:
    jobid: 99
    output: results/foo/step1/sample5/outdir/output1.tsv, results/foo/step1/sample5/outdir/output2.tsv
    log: logs/step1/sample5_out.log (check log file(s) for error message)
    shell:
    ...
        (exited with non-zero exit code)

Since I run workflow over many samples, I have defined a postrun.sh bash script in jobscript.sh directive that runs after snakemake {exec_job} section and notifies me via userbot in slack channel when a step failed for one or more samples.
jobscript.sh
        ########################### START SNAKEMAKE CMD ############################
    {exec_job}
        ############################ END SNAKEMAKE CMD #############################
    exitstat=$?
    export exitstat

    echo "END at $(date)"
. "${{HOME}}"/.smk_confs/postrun.sh

postrun.sh
## execute after running each of snakemake job
echo 'Running postrun.sh'
ERRMSG="MYJOB ID: $PBS_JOBID failed at $(pwd) on $(hostname) for ${USER} with exit status: ${exitstat}."

## ping to slack
ping_slack -m "$ERRMSG"
sleep 5

I am looking for a snakemake exported environment variable that I can use in postrun.sh to know which rule ID failed for one or more samples. For now, I can think of using job scheduler variable matching job name, e.g, PBS_JOBNAME, but better if I have a variable from snakemake API for rule ID or preferably, path to log file.
Thanks

**Tags:** snakemake

---

## 1 answers

### Answer by username_1 on 2019-08-21. Score: 1

You could add an exit trap to the beginning of the shell scripts inside your Snakefile, which is executed after each script is done and sends a message when there was an error:
 shell:
    """
    trap 'if [ $? -neq 0 ]; then ping_slack -m "Error in rule xxx with input {input}"; exit 1' EXIT
    ...

> **Comments:**
> **username_0**: I believe this needs to be added to all of workflow rules. I am hoping to set a trap similar to existing `onerror` handler of [snakemake](https://snakemake.readthedocs.io/en/stable/snakefiles/rules.html#onstart-onsuccess-and-onerror-handlers) but prefer to execute it at completion of each rule rather an entire workflow.  
> **username_1**: yes, that would be needed in each rule.  

---

