为什么我的 Cron 工作不能正常工作? [英] Why Doesn't My Cron Job Work Properly?

查看:35
本文介绍了为什么我的 Cron 工作不能正常工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Ubuntu Hardy VPS 上有一个 cron 工作,它只能工作一半,我不知道为什么.该作业是一个 Ruby 脚本,它使用 mysqldump 来备份 Rails 应用程序使用的 MySQL 数据库,然后将其压缩并使用 SFTP 上传到远程服务器.

I have a cron job on an Ubuntu Hardy VPS that only half works and I can't work out why. The job is a Ruby script that uses mysqldump to back up a MySQL database used by a Rails application, which is then gzipped and uploaded to a remote server using SFTP.

gzip 文件已成功创建并复制,但它始终为零字节.然而,如果我直接从命令行运行 cron 命令,它就可以完美运行.

The gzip file is created and copied successfully but it's always zero bytes. Yet if I run the cron command directly from the command line it works perfectly.

这是定时任务:

PATH=/usr/bin
10 3 * * * ruby /home/deploy/bin/datadump.rb

这是 datadump.rb:

This is datadump.rb:

#!/usr/bin/ruby
require 'yaml'
require 'logger'
require 'rubygems'
require 'net/ssh'
require 'net/sftp'

APP        = '/home/deploy/apps/myapp/current'
LOGFILE    = '/home/deploy/log/data.log'
TIMESTAMP  = '%Y%m%d-%H%M'
TABLES     = 'table1 table2'

log        = Logger.new(LOGFILE, 5, 10 * 1024)
dump       = "myapp-#{Time.now.strftime(TIMESTAMP)}.sql.gz"
ftpconfig  = YAML::load(open('/home/deploy/apps/myapp/shared/config/sftp.yml'))
config     = YAML::load(open(APP + '/config/database.yml'))['production']
cmd        = "mysqldump -u #{config['username']} -p#{config['password']} -h #{config['host']} --add-drop-table --add-locks --extended-insert --lock-tables #{config['database']} #{TABLES} | gzip -cf9 > #{dump}"

log.info 'Getting ready to create a backup'
`#{cmd}`    

# Strongspace
log.info 'Backup created, starting the transfer to Strongspace'
Net::SSH.start(ftpconfig['strongspace']['host'], ftpconfig['strongspace']['username'], ftpconfig['strongspace']['password']) do |ssh|
  ssh.sftp.connect do |sftp|
    sftp.open_handle("#{ftpconfig['strongspace']['dir']}/#{dump}", 'w') do |handle|
      sftp.write(handle, open("#{dump}").read)
    end
  end
end
log.info 'Finished transferring backup to Strongspace'

log.info 'Removing local file'
cmd       = "rm -f #{dump}" 
log.debug "Executing: #{cmd}"
`#{cmd}`
log.info 'Local file removed'

我已经检查并仔细检查了所有路径,它们是正确的.sftp.yml(SFTP 凭据)和 database.yml(MySQL 凭据)都由执行用户(deploy)拥有,对该用户具有只读权限(chmod 400).我使用的是 1.1.x 版本的 net-ssh 和 net-sftp.我知道它们不是最新的,但它们是我目前所熟悉的.

I've checked and double-checked all the paths and they're correct. Both sftp.yml (SFTP credentials) and database.yml (MySQL credentials) are owned by the executing user (deploy) with read-only permissions for that user (chmod 400). I'm using the 1.1.x versions of net-ssh and net-sftp. I know they're not the latest, but they're what I'm familiar with at the moment.

什么可能导致 cron 作业失败?

What could be causing the cron job to fail?

推荐答案

您确定在作为 cron 作业运行时正确创建了临时文件吗?脚本的工作目录将在 HOME 环境变量中指定,或者在安装 cron 作业的用户的/etc/passwd 条目中指定.如果 deploy 对其执行的目录没有写权限,那么您可以为转储文件指定一个绝对路径来解决问题.

Are you sure the temporary file is being created correctly when running as a cron job? The working directory for your script will either be specified in the HOME environment variable, or the /etc/passwd entry for the user that installed the cron job. If deploy does not have write permissions for the directory in which it is executing, then you could specify an absolute path for the dump file to fix the problem.

这篇关于为什么我的 Cron 工作不能正常工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆