saggit

saggit

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.

Showing Posts 1 to 8

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.

saggit

saggit OP

Thanks for your reply, I tried and got the same result, Just wonder if I got something wrong.

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.

saggit

saggit OP

Hi Nobbz, Thanks for your reply, tail -f /var/log/cron.log give me correct execute log say every 5 minutes without error message so that mean the path are correct and command run without error. I have to do more check. But thanks.

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
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 OP

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

saggit

saggit OP

Thanks Nobbz,

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
apz
I’m new to elixir and just tried to install the elixirLS extension for VScode(ium) and it is throwing some errors that I would like help ...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews