# Elixir standalone script in a Linux cron

**URL:** <https://forum.elixirforum.com/t/elixir-standalone-script-in-a-linux-cron/23253>\
**Category:** Questions\
**Tags:** elixir-project\
**Created:** [June 19, 2019, 12:08pm UTC](https://forum.elixirforum.com/t/elixir-standalone-script-in-a-linux-cron/23253 "2019-06-19T12:08:32Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![jerry](https://forum.elixirforum.com/letter_avatar_proxy/v4/letter/j/5daacb/32.png) [@jerry](https://forum.elixirforum.com/u/jerry)\
**Post date:** [June 19, 2019, 12:08pm UTC](https://forum.elixirforum.com/t/elixir-standalone-script-in-a-linux-cron/23253/1 "2019-06-19T12:08:32Z")

</div>

Hello,

Is there a way I can write an elixir script and schedule it in a Linux cron job to perform some repetitive tasks?

I would like the script to invoke some Ecto functions from modules defined inside an Elixir mix project.

Any help would be most appreciated.

Thanks.

Jerry

---

<div class="post-metadata">

**Author:** ![sweeneydavidj](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/sweeneydavidj/32/5892_2.png) [@sweeneydavidj](https://forum.elixirforum.com/u/sweeneydavidj)\
**Post date:** [June 19, 2019, 12:26pm UTC](https://forum.elixirforum.com/t/elixir-standalone-script-in-a-linux-cron/23253/2 "2019-06-19T12:26:00Z")

</div>

Hi,  
here is a nice article on using Elixir as a scripting language…  
[https://thoughtbot.com/blog/is-elixir-a-scripting-language](https://thoughtbot.com/blog/is-elixir-a-scripting-language)  
Enjoy 🙂

---

<div class="post-metadata">

**Author:** ![jerry](https://forum.elixirforum.com/letter_avatar_proxy/v4/letter/j/5daacb/32.png) [@jerry](https://forum.elixirforum.com/u/jerry)\
**Post date:** [June 19, 2019, 1:34pm UTC](https://forum.elixirforum.com/t/elixir-standalone-script-in-a-linux-cron/23253/3 "2019-06-19T13:34:35Z")

</div>

Hi @sweeneydavidj,

Thanks for your response.

I had read and tried the steps outlined in the guide ealier before my post here.  
Below is the error I get when I attempt to run the standalone script with mix:

13:06:27.160 [error] Failed to start Ranch listener Test.Router.HTTP in :ranch\_tcp:listen([cacerts: :…, key: :…, cert: :…, port: 80, ip: {0, 0, 0, 0}]) for reason :eaddrinuse (address already in use)

13:06:27.180 [info] Application test exited: Test.Application.start(:normal, ) returned an error: shutdown: failed to start child: {:ranch\_listener\_sup, Test.Router.HTTP}  
\*\* (EXIT) shutdown: failed to start child: :ranch\_acceptors\_sup  
\*\* (EXIT) {:listen\_error, Test.Router.HTTP, :eaddrinuse}  
\*\* (Mix) Could not start application test: Test.Application.start(:normal, ) returned an error: shutdown: failed to start child: {:ranch\_listener\_sup, Test.Router.HTTP}  
\*\* (EXIT) shutdown: failed to start child: :ranch\_acceptors\_sup  
\*\* (EXIT) {:listen\_error, Test.Router.HTTP, :eaddrinuse}

---

<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:** [June 19, 2019, 2:28pm UTC](https://forum.elixirforum.com/t/elixir-standalone-script-in-a-linux-cron/23253/4 "2019-06-19T14:28:38Z")

</div>

Your cron seems to want to start the application a second time, rather than to communicate with it, therefore `ranch` can’t boot, as the port it wants to listen on, is already in use by your main instance of the application.

If you use `distillery` just use a custom task, if you are using 1.9-releases, use their equivalent, if you use `mix` (as a command via cron) then make sure to use `--no-start` (IIRC), if you use an `escript` make sure it does not start the application and its dependencies, but just loads them.

---

<div class="post-metadata">

**Author:** ![jerry](https://forum.elixirforum.com/letter_avatar_proxy/v4/letter/j/5daacb/32.png) [@jerry](https://forum.elixirforum.com/u/jerry)\
**Post date:** [June 19, 2019, 4:39pm UTC](https://forum.elixirforum.com/t/elixir-standalone-script-in-a-linux-cron/23253/5 "2019-06-19T16:39:35Z")

</div>

Hello @NobbZ,

Thanks for the response.  
It worked for me.

I, however, have another issue still pertaining to this same activity.  
When the function that has been invoked by the standalone script from a module in the main project has some Ecto processes to run,  
I get the error below:

\*\* (ArgumentError) repo Test.Repo is not started, please ensure it is part of your supervision tree  
lib/ecto/query/planner.ex:148: Ecto.Query.Planner.query\_lookup/6  
lib/ecto/query/planner.ex:131: Ecto.Query.Planner.query\_with\_cache/7  
lib/ecto/repo/queryable.ex:124: Ecto.Repo.Queryable.execute/5  
lib/ecto/repo/queryable.ex:37: Ecto.Repo.Queryable.all/4  
lib/test/transactions.ex:387: Test.Transaction.process\_staged\_requests/0  
scripts/staged\_requests.exs:5: (module)

I would be grateful once again for your assistance.

Thanks.

Jerry

---

<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:** [June 19, 2019, 5:34pm UTC](https://forum.elixirforum.com/t/elixir-standalone-script-in-a-linux-cron/23253/6 "2019-06-19T17:34:02Z")

</div>

Well, you need to start your ecto repository without the rest of the application.

Be aware that it might then crash as well, as dependencies for ecto might be missing.

To be honest, the easiest thing is probably to use `quantum` and trigger the task from within your application.

---

<div class="post-metadata">

**Author:** ![jerry](https://forum.elixirforum.com/letter_avatar_proxy/v4/letter/j/5daacb/32.png) [@jerry](https://forum.elixirforum.com/u/jerry)\
**Post date:** [June 19, 2019, 6:16pm UTC](https://forum.elixirforum.com/t/elixir-standalone-script-in-a-linux-cron/23253/7 "2019-06-19T18:16:17Z")

</div>

Thanks very much, @NobbZ.

I’ll go ahead and use Quantum instead.

Regards,

Jerry
