'Auto boot MailHog on Ubuntu 20.04

I installed MailHog in my staging environment by following these steps:

  1. sudo apt-get -y install golang-go
  2. go get github.com/mailhog/MailHog

In order to manually start the service I do:

  1. cd ~/go/bin
  2. ./MailHog

Since I'm using Laravel I already have supervisor running for workers. I'm wondering if there is a way to add a new .conf file in order to start MailHog.

I tried to follow how Laravel workers are started, but so far no luck

[program:mailhog]
process_name=%(program_name)s_%(process_num)02d
command=~/go/bin/MailHog
user=ubuntu
stdout_logfile=/var/www/api/storage/logs/mailhog.log

I get mailhog:mailhog_00: ERROR (no such file) when I try to start supervisor.

I need a way to auto boot MailHog, no matter if I need supervisor or via services.

I'll really appreciate it if you can provide the "recipe" for starting MailHog from the supervisor or by using a service.



Solution 1:[1]

I figure out how the complete installation/setup should be:

  1. Downloading & installation
sudo apt-get -y install golang-go
go get github.com/mailhog/MailHog
  1. Copy Mailhog to bin directory
sudo cp ~/go/bin/MailHog /usr/local/bin/MailHog
  1. Create MailHog service
sudo tee /etc/systemd/system/mailhog.service <<EOL
[Unit]
Description=MailHog
After=network.target
[Service]
User=ubuntu 
ExecStart=/usr/bin/env /usr/local/bin/MailHog > /dev/null 2>&1 &
[Install]
WantedBy=multi-user.target
EOL
  1. Check status service is loaded successfully.
sudo systemctl status mailhog

Output

mailhog.service - MailHog
     Loaded: loaded (/etc/systemd/system/mailhog.service; disabled; vendor preset: enabled)
     Active: inactive (dead)
  1. Start service
sudo systemctl enable mailhog
  1. Reboot system and visit http://yourdomain.com:8025/

Solution 2:[2]

You don't need a supervisor, you can use Linux systemd to create a startup application.

systemd is the standard system and service manager in modern Linux. It is responsible for executing and managing programs during Linux startup

before that add mailhog to your system path variable to call it only by name

export PATH=$PATH:/home/YOUR-USERNAME/go/bin/MailHog

sudo systemctl enable mailhog

or if you are using any desktop environment , you can follow this https://askubuntu.com/questions/48321/how-do-i-start-applications-automatically-on-login

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Tien Do
Solution 2 Tipu Sultan Eiko