我有一个rake任务,每隔5分钟用crontab运行一次。此rake任务必须启动一个长时间运行的后台进程,该进程根据任务输入创建文件。
由于某些原因,长时间的进程没有运行。我正在使用Backticks来运行命令。
有什么想法吗?
require 'json'
namespace :scrapper do
path = '/home/ubuntu/users'
def lockfile
Rails.root.join('tmp', 'pids', 'leads_task.lock')
end
def running!
`touch #{lockfile}`
end
def done!
`rm #{lockfile}`
end
def running?
File.exists?(lockfile)
end
task :get_profile => :environment do
unless running?
running!
user = User.last
`/usr/local/bin/casperjs #{path}/scrapper.coffee #{user.id}`
done!
end
end
end
cron语法看起来像这样
*/5 * * * * /bin/bash -l -c 'cd /home/ubuntu/UserProfile && RAILS_ENV=staging bundle exec rake scrapper:get_profile --silent >> /home/ubuntu/UserProfile/scrapper_cron.output 2>&1'