cronelixirmacos-venturaasdf

Cronjob does not work for the local asdf elixir path


My crontab file looks like:

*/1 * * * * /Users/dmitriy/.asdf/shims/elixir ~/work/scripts/my_script.exs

And that script doesn't work. As I understand it, cron cannot detect the location of elixir. I run it on macOs Ventura.

How can I solve this problem?


Solution

  • I'm on macOS 12.5.1, and I have elixir installed with asdf as well. My path to elixir is the same as yours but with my user name instead:

    */1 * * * * /Users/7stud/.asdf/shims/elixir
    

    I used a full path to my elixir script:

    */1 * * * * /Users/7stud/.asdf/shims/elixir /Users/7stud/elixir_programs/my_prog.ex
    

    my_prog.ex contains:

    IO.puts "Cron job just finished"
    

    I redirected the output of stdout and stderr from the elixir script to a log file (>> is append):

    */1 * * * * /Users/7stud/.asdf/shims/elixir /Users/7stud/elixir_programs/my_prog.ex >> /Users/7stud/cron_output.log 2>&1
    

    Here is what I see in cron_output.log:

    /Users/7stud/.asdf/installs/elixir/1.16.0-otp-26/bin/elixir: line 247: exec: erl: not found
    /Users/7stud/.asdf/installs/elixir/1.16.0-otp-26/bin/elixir: line 247: exec: erl: not found
    /Users/7stud/.asdf/installs/elixir/1.16.0-otp-26/bin/elixir: line 247: exec: erl: not found
    /Users/7stud/.asdf/installs/elixir/1.16.0-otp-26/bin/elixir: line 247: exec: erl: not found
    /Users/7stud/.asdf/installs/elixir/1.16.0-otp-26/bin/elixir: line 247: exec: erl: not found
    /Users/7stud/.asdf/installs/elixir/1.16.0-otp-26/bin/elixir: line 247: exec: erl: not found
    
    ...
    ...
    

    It looks like erl, i.e. the erlang command, can't be found.

    I also have python installed with asdf, and it has the same path as elixir but with python instead of the word elixir. Here's a cron job for python:

    */1 * * * * /Users/7stud/.asdf/shims/python /Users/7stud/python_programs/1.py >> /Users/7stud/cron_output.log 2>&1
    

    Here's my python program 1.py:

    print("Cron job just finished")
    

    Output in cron_output.log:

    Cron job just finished
    Cron job just finished
    Cron job just finished
    Cron job just finished
    ...
    ...
    

    The interplay between elixir and erlang seems to be the problem with scheduling elixir scripts.

    This worked for me:

    */1 * * * * PATH=~/.asdf/shims:$PATH; /Users/7stud/elixir_programs/my_prog.ex >> /Users/7stud/cron_output.log 2>&1
    

    The last part:

    >> /Users/7stud/cron_output.log 2>&1
    

    just redirects stdout and stderr to a log file. You don't have to do that. I get system mail if there are errors when executing the cronjob or if there is unhandled output:

    Last login: Mon Feb 26 08:57:10 on ttys009
    You have new mail.
    
    ~%  mail
    Mail version 8.1 6/6/93.  Type ? for help.
    "/var/mail/7stud": 8 messages 8 new
    >N  1 7stud@7studs-MacBook  Mon Feb 26 09:13  18/749   "Cron <7stud@7studs-MacBook-Pro> /bin/bash "
     N  2 7stud@7studs-MacBook  Mon Feb 26 09:14  18/749   "Cron <7stud@7studs-MacBook-Pro> /bin/bash "
     N  3 7stud@7studs-MacBook  Mon Feb 26 09:15  18/749   "Cron <7stud@7studs-MacBook-Pro> /bin/bash "
     N  4 7stud@7studs-MacBook  Mon Feb 26 09:16  18/749   "Cron <7stud@7studs-MacBook-Pro> /bin/bash "
     N  5 7stud@7studs-MacBook  Mon Feb 26 09:16  18/749   "Cron <7stud@7studs-MacBook-Pro> /bin/bash "
     N  6 7stud@7studs-MacBook  Mon Feb 26 09:18  18/745   "Cron <7stud@7studs-MacBook-Pro> /bin/bash "
     N  7 7stud@7studs-MacBook  Mon Feb 26 09:19  18/745   "Cron <7stud@7studs-MacBook-Pro> /bin/bash "
     N  8 7stud@7studs-MacBook  Mon Feb 26 09:19  18/745   "Cron <7stud@7studs-MacBook-Pro> /bin/bash "
    ? 1
    Message 1:
    From 7stud@7studs-MacBook-Pro.local  Mon Feb 26 09:13:01 2024
    X-Original-To: 7stud
    Delivered-To: 7stud@7studs-MacBook-Pro.local
    From: 7stud@7studs-MacBook-Pro.local (Cron Daemon)
    To: 7stud@7studs-MacBook-Pro.local
    Subject: Cron <7stud@7studs-MacBook-Pro> /bin/bash -lc "elixir /Users/7stud/elixir_programs/my_prog.ex"
    X-Cron-Env: <SHELL=/bin/sh>
    X-Cron-Env: <PATH=/usr/bin:/bin>
    X-Cron-Env: <LOGNAME=7stud>
    X-Cron-Env: <USER=7stud>
    Date: Mon, 26 Feb 2024 09:13:00 -0700 (MST)
    
    /bin/bash: elixir: command not found