I lost a man day recently to this. The context was trying to run Unicorn on a user named apps and the Errno::EPERM error just kept biting us. Here's the stack trace:
E, [2018-04-12T20:45:07.277588 #2048] ERROR -- : Operation not permitted (Errno::EPERM)
/apps/cas-seas3/shared/bundle/ruby/2.2.0/gems/unicorn-5.4.0/lib/unicorn/worker.rb:143:in `initgroups'
/apps/cas-seas3/shared/bundle/ruby/2.2.0/gems/unicorn-5.4.0/lib/unicorn/worker.rb:143:in `user'
/apps/cas-seas3/shared/bundle/ruby/2.2.0/gems/unicorn-5.4.0/lib/unicorn/http_server.rb:657:in `init_worker_process'
/apps/cas-seas3/shared/bundle/ruby/2.2.0/gems/unicorn-5.4.0/lib/unicorn/http_server.rb:682:in `worker_loop'
/apps/cas-seas3/shared/bundle/ruby/2.2.0/gems/unicorn-5.4.0/lib/unicorn/http_server.rb:549:in `spawn_missing_workers'
/apps/cas-seas3/shared/bundle/ruby/2.2.0/gems/unicorn-5.4.0/lib/unicorn/http_server.rb:563:in `maintain_worker_count'
/apps/cas-seas3/shared/bundle/ruby/2.2.0/gems/unicorn-5.4.0/lib/unicorn/http_server.rb:293:in `join'
/apps/cas-seas3/shared/bundle/ruby/2.2.0/gems/unicorn-5.4.0/bin/unicorn:126:in `<top (required)>'
/apps/cas-seas3/shared/bundle/ruby/2.2.0/bin/unicorn:23:in `load'
/apps/cas-seas3/shared/bundle/ruby/2.2.0/bin/unicorn:23:in `<top (required)>'
Instrumenting the code base with a print statement around line 143 gave us the following debug info:
user = apps
group=apps
uid=1040
gid=110
whoami=apps
Process.egid = 1001
That discrepancy between the gid and Process.egid tripped this code block:
if gid && Process.egid != gid
Process.initgroups(user, gid)
Process::GID.change_privilege(gid)
end
and that's what caused the failure.
I finally traced it down to a difference between the GID / EGID and the issue was that the apps user belonged to a different primary group (admin). When I shifted the apps user to the apps group as primary then it worked. This was done by editing the /etc/passwd file which defined the box's user accounts.
Another way to test for this is to use sg to manually run unicorn with the correct group using the sg command like this:
bundle exec sg apps -c unicorn -c /apps/cas-seas3/current/config/unicorn.rb -E deployment -D
If you find that works then this is a very good sign to look into your user / group settings.
Unicorn seems to be very sensitive to user / group settings to check /etc/passwd and /etc/group and also use this ps line to check the difference on the executing Unicorn process for differences in the UID and GID:
ps -eo uid,gid,egid,args | grep unicorn
Note: The unicorn after hook above did not work for me. Nothing worked except for this.