# Turn on printing of all executed bash commands and disable deleting temp folder in work folder

**URL:** <https://community.seqera.io/t/turn-on-printing-of-all-executed-bash-commands-and-disable-deleting-temp-folder-in-work-folder/549>\
**Category:** Tips & Tricks\
**Tags:** nextflow\
**Created:** [March 5, 2024, 3:20pm UTC](https://community.seqera.io/t/turn-on-printing-of-all-executed-bash-commands-and-disable-deleting-temp-folder-in-work-folder/549 "2024-03-05T15:20:51Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![shl](https://dub1.discourse-cdn.com/flex013/user_avatar/community.seqera.io/shl/32/255_2.png) [@shl](https://community.seqera.io/u/shl)\
**Post date:** [March 5, 2024, 3:20pm UTC](https://community.seqera.io/t/turn-on-printing-of-all-executed-bash-commands-and-disable-deleting-temp-folder-in-work-folder/549/1 "2024-03-05T15:20:51Z")

</div>

Sometimes when a Nextflow task fails in mysterious ways, it can be useful to debug the execution of the bash scripts (`.command.run` and `.command.sh`) in the task’s work folder extra carefully.

I have found two things that help a lot:

1. Commenting out the deletion of the temporary run folder, so that its contents can be explored
2. Turning on printing of all executed bash commands, by adding `set -x` in the top of both `.command.sh` and `.command.run`

This can be done with the following bash function, which is supposed to be executed inside a task’s work folder:

```bash
function debugnf() {
    # Inserts `set -x` on the second line of .command.run and .command.sh
    # so that all executed commands are printed during execution.
    sed -i '2s/^/set -x\n/' .command.{run,sh};
    # Comments out all rm commands in .command.run so temp folder is kept
    sed -i 's/rm /#rm /g' .command.run;
}

```

To use it:

1. Check for a failing task’s work folder (similar to `/tmp/nf/work/a3/0e2ea68c421ced4797e00de9e73155`), and change directory into it, e.g. with:

```bash
cd /tmp/nf/work/a3/0e2ea68c421ced4797e00de9e73155

```

2. Run `debugnf`
3. Now execute either `.command.run` or `.command.sh` using bash, e.g:

```bash
bash .command.run

```

Some more context is provided in [this blog post](https://bionics.it/posts/troubleshooting-nextflow-pipelines).

---

<div class="post-metadata">

**Author:** ![mahesh.binzerpanchal](https://dub1.discourse-cdn.com/flex013/user_avatar/community.seqera.io/mahesh.binzerpanchal/32/253_2.png) [@mahesh.binzerpanchal](https://community.seqera.io/u/mahesh.binzerpanchal)\
**Post date:** [March 5, 2024, 3:57pm UTC](https://community.seqera.io/t/turn-on-printing-of-all-executed-bash-commands-and-disable-deleting-temp-folder-in-work-folder/549/2 "2024-03-05T15:57:00Z")

</div>

You can also add

```auto
trap read DEBUG

```

to the script to step through the script as it’s executed.

Helpful tip from: [bash debugging](https://wizardzines.com/comics/bash-debugging/)

You can also set `-x` on the script directly by doing:

```auto
bash -x .command.run

```

However, putting `set -x` in the script is useful for example if you need to run it in the workflow execution environment, for example if you submit the task to slurm.

```auto
sbatch .command.run

```

---

<div class="post-metadata">

**Author:** ![shl](https://dub1.discourse-cdn.com/flex013/user_avatar/community.seqera.io/shl/32/255_2.png) [@shl](https://community.seqera.io/u/shl)\
**Post date:** [March 6, 2024, 6:00pm UTC](https://community.seqera.io/t/turn-on-printing-of-all-executed-bash-commands-and-disable-deleting-temp-folder-in-work-folder/549/3 "2024-03-06T18:00:28Z")

</div>

Ah, thank you @mahesh.binzerpanchal ! That’s extremely helpful tips, and will immediately go into my toolbox 😊
