如何使用systemd / systemctl启动Unicorn?

时间:2016-03-15 15:05:49

标签: rubygems rvm bundler unicorn systemd

我为systemd撰写了一个systemctl文件,以启动unicorn

[Unit]
Description=Unicorn server

[Service]
SyslogIdentifier=my-app-unicorn
User=deployer
PIDFile=/tmp/unicorn.my-app.pid
WorkingDirectory=/opt/www/my-app.com

ExecStart=/home/deployer/.rvm/gems/ruby-2.2.1@my-app/bin/bundle exec "unicorn_rails -D -c /opt/www/my-app.com/config/unicorn.rb -E production"
#ExecReload=/bin/kill -s HUP $MAINPID
ExecReload=/bin/kill -s USR2 $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID

[Install]
WantedBy=multi-user.target

以下是我用来启动服务的命令

$ sudo systemctl daemon-reload
$ sudo systemctl start my-app.service

我在这里查看状态:

$ sudo systemctl status my-app
● my-app.service - My app unicorn server
   Loaded: loaded (/lib/systemd/system/my-app.service; disabled; vendor preset: enabled)
   Active: failed (Result: exit-code) since Tue 2016-03-15 14:56:31 UTC; 4s ago
  Process: 22165 ExecStop=/bin/kill -s QUIT $MAINPID (code=exited, status=200/CHDIR)
  Process: 22162 ExecStart=/home/deployer/.rvm/gems/ruby-2.2.1@my-app/bin/bundle exec unicorn_rails -D -c /opt/www/my-app.com/config/unicorn.rb -E production (code=exited, status=200/CHDIR)
 Main PID: 22162 (code=exited, status=200/CHDIR)

Mar 15 14:56:31 fat-man systemd[1]: Started My-App unicorn server.
Mar 15 14:56:31 fat-man systemd[22162]: my-app.service: Failed at step CHDIR spawning /home/deployer/.rvm/gems/ruby-2.2.1@my-app/bin/bundle: No such file or directory
Mar 15 14:56:31 fat-man systemd[1]: my-app.service: Main process exited, code=exited, status=200/CHDIR
Mar 15 14:56:31 fat-man systemd[1]: my-app.service: Control process exited, code=exited status=200
Mar 15 14:56:31 fat-man systemd[1]: my-app.service: Unit entered failed state.
Mar 15 14:56:31 fat-man systemd[1]: my-app.service: Failed with result 'exit-code'.

可能是什么问题?

1 个答案:

答案 0 :(得分:3)

错误消息Failed at step CHDIR表示systemd无法cd进入WorkingDirectory中指定的目录。它被指定为/opt/www/my-app.com,但稍后您将其列为/opt/www/my-app

另一个问题是,您使用选项unicorn_rails启动-D,告知unicorn_rails进程要守护进程(启动进程分叉子进程并立即退出),但您的[Service]部分没有& #39; t指定Type,因此默认为simplesystemd期望进程保持活跃状态​​。您需要删除-D选项或指定Type=forking

参见示例unit file for unicorn