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, https://github.com/elixir-lang/elixir/issues/789#issuecomment-12657214

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.

NobbZ

NobbZ

Does cron send you any logs? If not please activate the feature to send you the logs of each run to your local address for the debugging period at least(*).

Also, how are you editing your crontab? If you do not use crontab -e(*) Cron won’t know about the changes and nothing will be run of course.

Have you made sure that the project folder and all folders above and below and the files are actually readable/accessible by the user that owns the crontab in question?

(*): Can’t look up details or exact command as I’m on mobile right now.

NobbZ

NobbZ

As far as I remember Cron log will only contain that a job has started, it will not contain it’s results. Please try writing into a file, use the wrapper script from before and change it to be like this:

cd /path/to/project
mix hello.greeting >> /tmp/greeting.log

Where Next?

Popular in Questions Top

vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
logicmason
Hi there, I'm working through my first release with elixir/phoenix. I've built a release with distillery and found that it crashes when I...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
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
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
mathew4509
I have a list say x = ["23gh", "56kh", "97mh"] I would like to pass each element to Val in each iteration. Say, in iteration 1 -------...
New
sabri
Can someone explain the settings of pool_size of Ecto in config file? and what is the recommend size? Thanks
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New

Other popular topics Top

srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 35421 110
New
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New

We're in Beta

About us Mission Statement