我在 Rails 应用程序中编写了一个 TCP 服务器作为 Rails 任务。 然而,它可以从以下位置开始:
rails socketing:start
如果希望它成为一个在背景中继续运行的任务,该进程将开始执行,他们将其更改为:
nohup rails socketing:start &
并且将继续在backgroup中工作,因为tcp服务器已经实现了多线程,如果出现错误只会停止1个线程。
现在我希望这个过程由服务在 ubuntu 服务器启动时启动,以创建服务并启用它。 但代码中断了 你能建议一个出路吗 我的服务写的是:
[Unit]
Description = TCP at 51234
After = network.target
[Service]
Environment="HOME=/home/vidur"
ExecStart = /home/vidur/rails_app/tukaweb/custom_script.sh
[Install]
WantedBy = multi-user.target
我的custom_script.sh为:
#!/bin/bash
cd /home/vidur/rails_app/project_dir
/home/vidur/.rbenv/shims/bundler exec rails socketing:start
启动服务为:
sudo systemctl start socketing.service
它给出的o/p为:
Jun 10 17:45:31 Vidur-PC systemd[1]: Started TCP at 51234.
Jun 10 17:45:31 Vidur-PC systemd[1]: socketing.service: Succeeded.
但是没有启动rails任务,甚至没有识别rails环境: 我的 Rails 任务为:
task start: :environment do
require 'socket'
puts "Started TCP Server at PORT 53492"
server = TCPServer.new 51324 # Server bound to port 51234
loop do
Thread.start(server.accept) do |client|
client.close
end
end
p "result = #{result}"
p 'Bye'
end
此代码给出以下错误:
Jun 10 19:16:40 Vidur-PC systemd[1]: Started TCP at 53492.
Jun 10 19:16:43 Vidur-PC custom_script.sh[60012]: rake aborted!
Jun 10 19:16:43 Vidur-PC custom_script.sh[60012]: ArgumentError: couldn't find login name -- expanding `~'
Jun 10 19:16:43 Vidur-PC custom_script.sh[60012]: /home/vidur/rails_app/tukaweb/config/application.rb:8:in `<top (required)>'
Jun 10 19:16:43 Vidur-PC custom_script.sh[60012]: /home/vidur/rails_app/tukaweb/Rakefile:4:in `require_relative'
Jun 10 19:16:43 Vidur-PC custom_script.sh[60012]: /home/vidur/rails_app/tukaweb/Rakefile:4:in `<top (required)>'
Jun 10 19:16:43 Vidur-PC custom_script.sh[60012]: /home/vidur/.rbenv/versions/2.7.2/bin/bundler:23:in `load'
Jun 10 19:16:43 Vidur-PC custom_script.sh[60012]: /home/vidur/.rbenv/versions/2.7.2/bin/bundler:23:in `<main>'
Jun 10 19:16:43 Vidur-PC custom_script.sh[60012]: (See full trace by running task with --trace)
Jun 10 19:16:44 Vidur-PC systemd[1]: socketing.service: Main process exited, code=exited, status=1/FAILURE
Jun 10 19:16:44 Vidur-PC systemd[1]: socketing.service: Failed with result 'exit-code'.
更新了application.rb文件:
require_relative 'boot'
require 'rails/all'
# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)
module TukaWeb
class Application < Rails::Application
# Initialize configuration defaults for originally generated Rails version.
config.load_defaults 5.1
if Rails.env.development?
config.active_job.queue_adapter = :async
else
config.active_job.queue_adapter = :sidekiq
end
config.generators.javascript_engine = :js
config.action_dispatch.default_headers = {
'Access-Control-Allow-Origin' => 'https://tukadata.tukatech.com',
'Access-Control-Request-Method' => %w{GET POST OPTIONS}.join(",")
}
# Settings in config/environments/* take precedence over those specified here.
# Application configuration should go into files in config/initializers
# -- all .rb files in that directory are automatically loaded.
end
end
它说明了 tcp 进程 rake 任务,但仅适用于 root 用户。
数据库剩余错误:
`connect': Access denied for user 'root'@'localhost'
但是如果进程正常运行,那么它就会运行。
问题出在 puma 身上,他需要回家,所以更新的服务如下:
[Unit]
Description = TCP at 51234
After = network.target
[Service]
Environment="HOME=/home/vidur"
ExecStart = /home/vidur/rails_app/tukaweb/custom_script.sh
[Install]
WantedBy = multi-user.target