# Is there a way to do startup and cleanup tasks with an SGE task array?

**URL:** <https://ask.cyberinfrastructure.org/t/is-there-a-way-to-do-startup-and-cleanup-tasks-with-an-sge-task-array/271>\
**Category:** Q&A\
**Tags:** sge, hpc-getting-started, parameter-sweep, scripting, scheduler, researcher\
**Created:** [July 10, 2018, 12:44am UTC](https://ask.cyberinfrastructure.org/t/is-there-a-way-to-do-startup-and-cleanup-tasks-with-an-sge-task-array/271 "2018-07-10T00:44:13Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jpessin1](https://ask.cyberinfrastructure.org/user_avatar/ask.cyberinfrastructure.org/jpessin1/32/339_2.png) [@jpessin1](https://ask.cyberinfrastructure.org/u/jpessin1)\
**Post date:** [July 10, 2018, 12:44am UTC](https://ask.cyberinfrastructure.org/t/is-there-a-way-to-do-startup-and-cleanup-tasks-with-an-sge-task-array/271/1 "2018-07-10T00:44:13Z")

</div>

Is there a way to run one time commands at the beginning before the first task in the array or after the last task?

Doing things like creating a new directory before hand to put all the output files in, or being able to tar and move them to a long term storage location would be very helpful.

---

<div class="post-metadata">

**Author:** ![jpessin1](https://ask.cyberinfrastructure.org/user_avatar/ask.cyberinfrastructure.org/jpessin1/32/339_2.png) [@jpessin1](https://ask.cyberinfrastructure.org/u/jpessin1)\
**Post date:** [July 10, 2018, 1:30am UTC](https://ask.cyberinfrastructure.org/t/is-there-a-way-to-do-startup-and-cleanup-tasks-with-an-sge-task-array/271/2 "2018-07-10T01:30:23Z")

</div>

There are different, but combinable options for doing so by either when the first or last task starts  
and for when all tasks finish:

**When only concerned about when tasks start** :  
The key is that SGE array jobs set some additional variables  
`$SGE_TASK_FIRST`, `$SGE_TASK_LAST`, `$SGE_STEP_SIZE.`

> `#!/bin/bash`  
> `#$ -t 1-100`  
> `if [$SGE_TASK_ID -eq $SGE_TASK_FIRST] ; then`  
> ` # do first-task stuff here`  
> `fi`
> 
> ` # do normal processing here`
> 
> `if [$SGE_TASK_ID -q $SGE_TASK_LAST]; then`  
> ` # do last-task stuff here`  
> `fi`

**When concerned about when the last job finishes** :  
you can use the `--sync` flag in a script

> `qsub --sync y jobscript.sh >> exitstatus.log`  
> `# do things after`

Will wait on the submitted job(s) to complete and report its exit code before closing.  
With an array job it will wait on, and report on all of them.

---

<div class="post-metadata">

**Author:** ![jpessin1](https://ask.cyberinfrastructure.org/user_avatar/ask.cyberinfrastructure.org/jpessin1/32/339_2.png) [@jpessin1](https://ask.cyberinfrastructure.org/u/jpessin1)\
**Post date:** [March 15, 2019, 3:23pm UTC](https://ask.cyberinfrastructure.org/t/is-there-a-way-to-do-startup-and-cleanup-tasks-with-an-sge-task-array/271/3 "2019-03-15T15:23:10Z")

</div>

This seems rather clunky are there better ways?
