0

I am trying to get Monit / init.d scripts running on a few different servers, and for a few different processes on Ubuntu servers.

  1. Trying to get Sphinx to start on reboot, and to work with Monit
  2. Trying to get Elixir/Phoenix mix phx.server to start on reboot and work with Monit
  • When I run monit -t I see no errors

For elixir:

  • When logged in as deploy user role, I manually run ./etc/init.d elixir start – the process starts just fine
  • When logged in as deploy user role I manually run sudo monit start elxir but nothing happens...
  • When I reboot the machine the application is not started
  • I am attempting to log result of the init.d script, but I see no output

For searchd:

  • When logged in as deploy user role, I manually run ./etc/init.d sphinx start – the process starts just fine

  • When logged in as deploy user role I manually run sudo monit start sphinx but nothing happens...

  • When I reboot the machine the application is not started

  • I am attempting to log result of the init.d script, but I see no output

  • The init.d scripts are executable

  • I’ve tried this command which I read may ensure that the script is registered to run on reboot: sudo update-rc.d start_sphinx defaults

Example Elixir monit script:

example monit script:
check process api matching "mix phx.server"
start program = "/etc/init.d/elixir start" as uid deploy and gid deploy
stop program = "/etc/init.d/elixir stop" as uid deploy and gid deploy

Example Elixir init.d script:

#!/bin/bash
# chkconfig: 2345 20 80
# description: Elixir API

# Source function library.
#. /etc/init.d/functions

start() {
    cd /home/deploy/apps/api && PORT=8888 MIX_ENV=prod elixir --erl "-detached" -S mix phx.server > /home/deploy/elixir_init_d_start.log 2>&1
}

stop() {
  pkill -f 'mix phx.server'
}

case "$1" in
    start)
       start
       ;;
    stop)
       stop
       ;;
    restart)
       stop
       start
       ;;
    status)
       # code to check status of app comes here
       # example: status program_name
       ;;
    *)
       echo "Usage: $0 {start|stop|status|restart}"
esac

exit 0
Gordon Isnor
  • 2,065
  • 1
  • 19
  • 32
  • Does the leading dot in “I manually run `./etc/init.d elixir start`” means you run a custom `init.d` located somewhere in a custom folder? Also, normally you’d run `start service` to start services. – Aleksei Matiushkin Oct 07 '22 at 06:46
  • Sorry that leading dot was just a typo – Gordon Isnor Oct 07 '22 at 16:14
  • To add more context, one of the servers in question is Ubuntu 14 – Gordon Isnor Oct 07 '22 at 16:15
  • Do you try to start "elixir", then use "./elixir" to start the program. Remember, the monitor use an reduced environment, the used profile will not honored and the folder, the script switch to /home/deploy/apps/api, is not in the path. – lutzmad Oct 11 '22 at 14:08

1 Answers1

0

I never did get the above script working with the existing setup, but what I did find that worked was this: I upgrade Phoenix from 1.3 to 1.6 and am now using Phoenix/Elixir releases.

My deploy process now looks like this:

git push server
{login to server}
cd apps
rm -rf api
mkdir api
cd api
git clone --shared ../new_deploy_method . 
cd config
vi prod.secret.exs
{copy local config/prod.secret.exs}
cd ../
mix deps.get --only prod
MIX_ENV=prod mix compile
mix phx.gen.release
MIX_ENV=prod mix release
_build/prod/rel/my_app/bin/my_app daemon

Now my init.d script looks like this:

#!/bin/bash
# chkconfig: 2345 20 80
# description: Elixir API

# Source function library.
#. /etc/init.d/functions

start() {
  /home/deploy/apps/api/_build/prod/rel/ckdu_site_api/bin/ckdu_site_api daemon > /home/deploy/elixir_init_d_start.log 2>&1
}

stop() {
  /home/deploy/apps/api/_build/prod/rel/ckdu_site_api/bin/ckdu_site_api stop > /home/deploy/elixir_init_d_start.log 2>&1
}

case "$1" in
    start)
       start
       ;;
    stop)
       stop
       ;;
    restart)
       stop
       start
       ;;
    status)
       # code to check status of app comes here
       # example: status program_name
       ;;
    *)
       echo "Usage: $0 {start|stop|status|restart}"
esac

exit 0

My monit script now looks like this:

check process api matching "beam"
start program = "/etc/init.d/elixir start" as uid deploy and gid deploy
stop program = "/etc/init.d/elixir stop" as uid deploy and gid deploy

Thanks to the folks who commented trying to help me out!

Gordon Isnor
  • 2,065
  • 1
  • 19
  • 32