saggit

saggit

Run Mix task in crontab

Hi, everybody
I was try to use crontab to schedule my simple mix task that send emails, the task works perfect by run
mix hello.greeting but does not work (scheduled, but no email sent ) if i put something like this in crontab */5 * * * * cd path/to/myapp; /usr/local/bin/mix hello.greeting

my simple task code

defmodule Mix.Tasks.Hello.Greeting do
  use Mix.Task

  alias Hwapp.Email
  alias Hwapp.Mailer

  @shortdoc "Sends a greeting to us from Hello Phoenix"

  @moduledoc """
    This is where we would put any long form documentation or doctests.
  """

  def run(_args) do
    Mix.shell.info("Greetings from the Hello Phoenix Application!")
    Mix.Task.run "app.start"
    # get all users
    "hello@localhost"
    		Email.welcome_html_email()
    		|> Mailer.deliver_now
    	end)
   end

end

Thanks for your time.

Most Liked

speeddragon

speeddragon

Finally got it working. Here is the steps done in my computer (running OSX), it might change a little bit if you use Linux.

I’ve created a script with the following code:

#!/bin/bash
cd /path/project
mix phx.server&

And run with the following line on crontab:

*/1 * * * * /tmp/test.sh >> /var/log/crontab.log 2>&1

The output of crontab.log was showing this:

/tmp/test.sh: line 2: mix: command not found

Using the full path of mix didn’t solve it. (/usr/local/bin/mix phx.server)

env: elixir: No such file or directory

Searching that on google, I found an comment from José Valim, Ability to use elixir commands that shipped as an application dependency · Issue #789 · elixir-lang/elixir · GitHub

But even using /usr/local/bin/elixir /usr/local/bin/mix phx.server & didn’t solve it, because it gave the following error:

/usr/local/bin/elixir: line 123: exec: erl: not found

So, if you output the $PATH, you will see that only have 2 directories (or less than you expected): /usr/bin:/bin

You can check where is your binaries, with the command which, like which elixir. You will need in your path at least for mix, elixir and erl.

In the final script, you should export PATH concatenating the missing path, in my case was /usr/local/bin/.

export PATH=$PATH:/usr/local/bin/

After that you should be able to run any command with mix, you don’t need the full path.

saggit

saggit

It works perfect, thanks for your help,
Really wonderful community.

speeddragon

speeddragon

You can put that code cd path/to/myapp; /usr/local/bin/mix hello.greeting inside a shell script (something.sh) give +x permission (chmod +x something.sh) and then call it on the crontab. Don’t forget to use full path instead of relative paths.

Last Post!

saggit

saggit

Thanks Nobbz,

Where Next?

Popular in Questions Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a > b) do {:ok, "a"} end if (a < b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New

Other popular topics Top

minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New

We're in Beta

About us Mission Statement