<p><a href="https://craftcms.com/classreference/services/TasksService#getRunningTask-detail" rel="nofollow"><code>TasksService-&gt;getRunningTask()</code></a> will tell you if a task <em>is currently running</em>. However, it doesn't tell you whether a particular chunk of code is being executed by that Task or by some other function.</p>

<p>The most straightforward solution I can think of would be to write yourself a little helper method to check whether the call stack includes a Task class:</p>

<pre><code>static function inTask()
{
   foreach(debug_backtrace() as $caller)
   {
      if (substr($caller['file'], -8) === 'Task.php') return true;
   }
   return false;
}
</code></pre>

<p>Add that to your plugin class, and then you can call it anywhere you need:</p>

<pre><code>if (MyPlugin::inTask()) ...
</code></pre>

CC BY-SA 3.0
2016-09-16T05:10:31.613Z
2016-09-16T05:10:31.613Z
Michael Rog
<p>I have a chunk of PHP code which <em>should not</em> be run if the script is being executed from a task.</p>

<p>We already have these methods, which are similar to what I'm looking for:</p>

<ul>
<li><code>craft()-&gt;isConsole()</code> - Whether the script is running in the console.</li>
<li><code>craft()-&gt;request-&gt;isCpRequest()</code> - Whether the script is running in the CP.</li>
</ul>

<p><strong>Is there a way to tell if the script is running inside of a task?</strong></p>

<p>(Since my research has turned up nothing so far, I opened a <a href="https://craftcms.uservoice.com/forums/285221-feature-requests/suggestions/16112530-an-istask-method-to-determine-if-script-is-occ" rel="nofollow">related feature request...</a>)</p>

<plugin-development><tasks>
Is it possible to determine if a script is running within a task?
# Is it possible to determine if a script is running within a task?

(Asked by: username_0 on 2016-09-16)

I have a chunk of PHP code which *should not* be run if the script is being executed from a task.

We already have these methods, which are similar to what I'm looking for:

* `craft()->isConsole()` - Whether the script is running in the console.
* `craft()->request->isCpRequest()` - Whether the script is running in the CP.

**Is there a way to tell if the script is running inside of a task?**

(Since my research has turned up nothing so far, I opened a [related feature request...](https://craftcms.uservoice.com/forums/285221-feature-requests/suggestions/16112530-an-istask-method-to-determine-if-script-is-occ))

**Tags:** plugin-development, tasks

---

## 1 answers

### Answer by username_1 on 2016-09-16. Score: 4 | Accepted answer

TasksService->getRunningTask() will tell you if a task is currently running. However, it doesn't tell you whether a particular chunk of code is being executed by that Task or by some other function.
The most straightforward solution I can think of would be to write yourself a little helper method to check whether the call stack includes a Task class:
static function inTask()
{
   foreach(debug_backtrace() as $caller)
   {
      if (substr($caller['file'], -8) === 'Task.php') return true;
   }
   return false;
}

Add that to your plugin class, and then you can call it anywhere you need:
if (MyPlugin::inTask()) ...


---

