Home>

I'm a Rails novice engineer.
We are planning to deploy and operate a web application prototype on AWS, and are building a production environment.

I want to achieve

I want to set the automatic start of the application server (Unicorn) on Amazon Linux2 in preparation for automatic server recovery.

Implementation content
  1. Create a unit file (/etc/systemd/system/unicorn.service) to start unicorn using systemd
  2. Reflect the settingssudo systemctl daemon-reload
  3. Manually start with systemctl for confirmationsudo systemctl start unicorn.service

Contents of the unicorn.service file

[Unit]
Description = The unicorn process
After = network.target remote-fs.target nss-lookup.target
Before = nginx.service
[Service]
User = myname
WorkingDirectory =/var/www/rails/myapp
SyslogIdentifier = unicorn
Type = simple
Restart = on-failure
Environment = RAILS_ENV = production
Environment = UNICORN_CONF =/var/www/rails/myapp/config/unicorn.conf.rb
Environment = BUNDLE_GEMFILE =/var/www/rails/myapp/Gemfile
EnvironmentFile =/etc/environment
ExecStart =/bin/bash -l -c'bundle exec unicorn_rails -c ${UNICORN_CONF} -E ${RAILS_ENV} -D'
ExecStop =/usr/bin/kill -QUIT $MAINPID
ExecReload =/usr/bin/kill -USR2 $MAINPID
[Install]
WantedBy = multi-user.target

Referenced sites:
Launch unicorn with systemd on Amazon Linux 2

Occurrence event

Unicron does not start and the log is as follows.

● unicorn.service --The unicorn process
   Loaded: loaded (/etc/systemd/system/unicorn.service;disabled;vendor preset: disabled)
   Active: failed (Result: start-limit) since Thu 2020-11-19 22:03:24 JST;1h 22min ago
  Process: 29782 ExecStop =/usr/bin/kill -QUIT $MAINPID (code = exited, status = 1/FAILURE)
  Process: 29382 ExecStart =/bin/bash -l -c bundle exec unicorn_rails -c ${UNICORN_CONF} -E ${RAILS_ENV} -D (code = exited, status = 0/SUCCESS)
 Main PID: 29382 (code = exited, status = 0/SUCCESS)
November 19 22:03:24 xxx.compute.internal systemd [1]: Unit unicorn.service entered failed state.
November 19 22:03:24 xxx.compute.internal systemd [1]: unicorn.service failed.
November 19 22:03:24 xxx.compute.internal systemd [1]: unicorn.service holdoff time over, scheduling restart.
November 19 22:03:24 xxx.compute.internal systemd [1]: start request repeated too quickly for unicorn.service
November 19 22:03:24 xxx.compute.internal systemd [1]: Failed to start The unicorn process.
November 19 22:03:24 xxx.compute.internal systemd [1]: Unit unicorn.service entered failed state.
November 19 22:03:24 xxx.compute.internal systemd [1]: unicorn.service failed.
Question

From the log, it seems that ExecStop =/usr/bin/kill -QUIT $MAINPID (code = exited, status = 1/FAILURE) has failed, but I am in trouble because I do not know the cause and how to deal with it.
Thanks for your support.

* As a premise, do not use systemd, directly in the working directorybundle exec unicorn_rails -c /var/www/rails/myapp/config/unicorn.conf.rb -D -E productionI've confirmed that Unicorn starts when I enter the command.

What I tried # 1

Changed the unicorn.service file as follows so that unicorn will not be restarted without using environment variables.

[Unit]
Description = The unicorn process
After = network.target remote-fs.target nss-lookup.target
Before = nginx.service
[Service]
User = myname
WorkingDirectory =/var/www/rails/myapp
SyslogIdentifier = unicorn
Type = simple
ExecStart =/bin/bash -l -c'bundle exec unicorn_rails -c /var/www/rails/myapp/config/unicorn.conf.rb -E production -D'
ExecStop =/usr/bin/kill -QUIT $MAINPID
ExecReload =/usr/bin/kill -USR2 $MAINPID
[Install]
WantedBy = multi-user.target

When I ran it, Unicorn did not start and the log is as follows.

● unicorn.service --The unicorn process
   Loaded: loaded (/etc/systemd/system/unicorn.service;disabled;vendor preset: disabled)
   Active: failed (Result: exit-code) since Fri 2020-11-20 11:23:04 JST;36s ago
  Process: 6590 ExecStop =/usr/bin/kill -QUIT $MAINPID (code = exited, status = 1/FAILURE)
  Process: 6190 ExecStart =/bin/bash -l -c bundle exec unicorn_rails -c /var/www/rails/myapp/config/unicorn.conf.rb -E production -D (code = exited, status = 0/SUCCESS)
 Main PID: 6190 (code = exited, status = 0/SUCCESS)
November 20 11:23:03 xxx.compute.internal systemd [1]: Started The unicorn process.
November 20 11:23:03 xxx.compute.internal systemd [1]: Starting The unicorn process ...
November 20 11:23:04 xxx.compute.internal unicorn [6590]: kill: not enough arguments
November 20 11:23:04 xxx.compute.internal systemd [1]: unicorn.service: control process exited, code = exited status = 1
November 20 11:23:04 xxx.compute.internal systemd [1]: Unit unicorn.service entered failed state.
November 20 11:23:04 xxx.compute.internal systemd [1]: unicorn.service failed.

Journal of unicorn.service.

(... Omitted ...)
November 20 10:44:15 xxx.compute.internal unicorn [5569]: kill: not enough arguments
November 20 10:44:17 xxx.compute.internal unicorn [5972]: kill: not enough arguments
November 20 11:23:04 xxx.compute.internal unicorn [6590]: kill: not enough arguments
November 25 09:48:42 xxx.compute.internal unicorn [16521]: kill: not enough arguments
What I tried # 2

In the unicorn.service file, delete the dependency, ExecStop, ExecReload, and execute with the simple contents of ExecStart only.

[Unit]
Description = The unicorn process
[Service]
User = myname
WorkingDirectory =/var/www/rails/myapp
SyslogIdentifier = unicornType = simple
ExecStart =/bin/bash -l -c'bundle exec unicorn_rails -c /var/www/rails/myapp/config/unicorn.conf.rb -E production -D'
[Install]
WantedBy = multi-user.target

I got a different log than before, but Unicron still did not start and the cause is unknown.

● unicorn.service --The unicorn process
   Loaded: loaded (/etc/systemd/system/unicorn.service;disabled;vendor preset: disabled)
   Active: inactive (dead)
November 25 09:48:40 xxx.compute.internal systemd [1]: Started The unicorn process.
November 25 09:48:40 xxx.compute.internal systemd [1]: Starting The unicorn process ...
November 25 09:48:42 xxx.compute.internal unicorn [16521]: kill: not enough arguments
November 25 09:48:42 xxx.compute.internal systemd [1]: unicorn.service: control process exited, code = exited status = 1
November 25 09:48:42 xxx.compute.internal systemd [1]: Unit unicorn.service entered failed state.
November 25 09:48:42 xxx.compute.internal systemd [1]: unicorn.service failed.
November 25 10:04:26 xxx.compute.internal systemd [1]: Started The unicorn process.
November 25 10:04:26 xxx.compute.internal systemd [1]: Starting The unicorn process ...
November 25 11:23:00 xxx.compute.internal systemd [1]: Started The unicorn process.
November 25 11:23:00 xxx.compute.internal systemd [1]: Starting The unicorn process ...

The contents of unicorn.log are as follows.

I, [2020-11-25T09: 48: 41.677152 # 16516] INFO-: Refreshing Gem list
I, [2020-11-25T09: 48: 42.436156 # 16516] INFO-: unlinking existing socket =/var/www/rails/myapp/tmp/socks/.unicorn.sock
I, [2020-11-25T09: 48: 42.436349 # 16516] INFO-: listening on addr =/var/www/rails/myapp/tmp/socks/.unicorn.sock fd = 9
I, [2020-11-25T09: 48: 42.469695 # 16516] INFO-: master process ready
I, [2020-11-25T09: 48: 42.477058 # 16520] INFO-: worker = 1 ready
I, [2020-11-25T09: 48: 42.478356 # 16519] INFO-: worker = 0 ready
I, [2020-11-25T09: 48: 42.583104 # 16516] INFO-: reaped #<Process :: Status: pid 16519 exit 0>worker = 0
I, [2020-11-25T09: 48: 42.583221 # 16516] INFO-: reaped #<Process :: Status: pid 16520 exit 0>worker = 1
I, [2020-11-25T09: 48: 42.583295 # 16516] INFO-: master complete
What I tried # 3

Change the ExecStart path from/bin/bash to the working directory (/ var/www/rails/myapp) and execute.

[Unit]
Description = The unicorn process
[Service]
User = myname
WorkingDirectory =/var/www/rails/myapp
SyslogIdentifier = unicorn
Type = simple
ExecStart =/var/www/rails/myapp'bundle exec unicorn_rails -c /var/www/rails/myapp/config/unicorn.conf.rb -E production -D'
[Install]
WantedBy = multi-user.target


A permission error occurs.

systemd [19988]: Failed at step EXEC spawning/var/www/rails/myapp: Permission denied


Although it has been confirmed that Unicorn starts by entering the same directory and the same command from the command line, the above error occurs.
It has also been confirmed that the directory has execute permission, and the cause of the error is unknown.

drwxrwxr-x 15 myname myname 4096 November 3 13:12 myapp
  • Answer # 1

    I solved it by changing from Type = simple to Type = forking in [Service] of the unicorn.service file! !!

    I don't know the detailed reason, but unicorn started for the time being.

    The following is the site that I referred to for this solution.
    stackoverflow
    Introduction to Systemd (4) --Service type Unit configuration file