如何使用 systemd/systemctl 启动 Unicorn?

如何使用 systemd/systemctl 启动 Unicorn?

我写了一个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'.

可能是什么问题呢?

LE:用户部署者对 /opt/www/my-app 目录的访问权限:

$ ls -alh
total 84K
drwxr-xr-x 12 deployer admin 4.0K Mar 12 20:10 .
drwxr-xr-x  3 root     root  4.0K Mar 11 20:43 ..
drwxr-xr-x  9 deployer admin 4.0K Mar 11 20:52 app
drwxr-xr-x  2 deployer admin 4.0K Mar 11 20:52 bin
drwxr-xr-x  5 deployer admin 4.0K Mar 13 14:02 config
-rw-r--r--  1 deployer admin  153 Mar 11 20:52 config.ru
drwxr-xr-x  3 deployer admin 4.0K Mar 11 20:52 db
-rw-r--r--  1 deployer admin 1.9K Mar 11 20:52 Gemfile
-rw-r--r--  1 deployer admin 5.1K Mar 11 20:52 Gemfile.lock
-rw-r--r--  1 deployer admin  616 Mar 11 20:52 .gitignore
drwxr-xr-x  5 deployer admin 4.0K Mar 11 20:52 lib
drwxr-xr-x  2 deployer admin 4.0K Mar 12 20:53 log
drwxr-xr-x  3 deployer admin 4.0K Mar 12 19:12 public
-rw-r--r--  1 deployer admin  249 Mar 11 20:52 Rakefile
-rw-r--r--  1 deployer admin  478 Mar 11 20:52 README.rdoc
-rw-r--r--  1 deployer admin    9 Mar 11 20:52 .ruby-gemset
-rw-r--r--  1 deployer admin   11 Mar 11 20:52 .ruby-version
drwxr-xr-x  8 deployer admin 4.0K Mar 11 20:52 test
drwxr-xr-x  6 deployer admin 4.0K Mar 12 20:53 tmp
drwxr-xr-x  3 deployer admin 4.0K Mar 11 20:52 vendor

答案1

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

另一个问题是,您从告诉进程进行守护进程的unicorn_rails选项开始(启动的进程分叉子进程并立即退出),但您的部分没有指定,因此它默认为并期望进程保持活动状态。您需要删除选项或指定.-Dunicorn_rails[Service]Typesimplesystemd-DType=forking

参见示例独角兽的单元文件

答案2

这可能是因为字母大小写而不是例如/opt/www/my-app.com尝试/opt/www/My-app.com

相关内容