# Calling mix task inside a function

**URL:** <https://forum.elixirforum.com/t/calling-mix-task-inside-a-function/17941>\
**Category:** Questions\
**Tags:** task\
**Created:** [November 15, 2018, 12:11pm UTC](https://forum.elixirforum.com/t/calling-mix-task-inside-a-function/17941 "2018-11-15T12:11:48Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![script](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/script/32/8725_2.png) [@script](https://forum.elixirforum.com/u/script)\
**Post date:** [November 15, 2018, 12:11pm UTC](https://forum.elixirforum.com/t/calling-mix-task-inside-a-function/17941/1 "2018-11-15T12:11:48Z")

</div>

Is there a way we can call a mix task inside a function?  
I couldn’t find it in documentation just found the Command Line examples.  
Thanks

---

<div class="post-metadata">

**Author:** ![nsweeting](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/nsweeting/32/8237_2.png) [@nsweeting](https://forum.elixirforum.com/u/nsweeting)\
**Post date:** [November 15, 2018, 12:19pm UTC](https://forum.elixirforum.com/t/calling-mix-task-inside-a-function/17941/2 "2018-11-15T12:19:40Z")

</div>

A mix task is just a module + functions. So you can always run it like any other module + function.

Mix.Tasks.MyTask.run()

---

<div class="post-metadata">

**Author:** ![NobbZ](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/nobbz/32/27235_2.png) [@NobbZ](https://forum.elixirforum.com/u/NobbZ)\
**Post date:** [November 15, 2018, 12:20pm UTC](https://forum.elixirforum.com/t/calling-mix-task-inside-a-function/17941/3 "2018-11-15T12:20:25Z")

</div>

You can, but you shouldn’t.

[`Mix.Task.run/2`](https://hexdocs.pm/mix/Mix.Task.html#run/2), can be used for that, but `mix` is usually not integrated in releases unless you do so manually.

But usually you do not want to do so, for reasons discussed a couple of times in the forum.

---

<div class="post-metadata">

**Author:** ![dimitarvp](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/dimitarvp/32/38664_2.png) [@dimitarvp](https://forum.elixirforum.com/u/dimitarvp)\
**Post date:** [November 15, 2018, 6:07pm UTC](https://forum.elixirforum.com/t/calling-mix-task-inside-a-function/17941/4 "2018-11-15T18:07:32Z")

</div>

You are better off having a shared module and function that your mix task uses. Then you can use the same module and function in your other code instead of trying to run the mix task – this will fail because releases (deployments) don’t include mix.
