I am trying to set up a cron job to, every 15 min, run a ruby script that lives inside the database folder of my ror website. The line I have in my crontab looks like this:
0,15,30,45 * * * * /bin/bash -l -c 'ruby /Users/arpitadey/rails_projects/sample_app/db/poller.rb'
poller.rb is supposed to collect some data remotely and then update a database file called sites.db with the new data. poller.rb and sites.db are in the same folder--so when i run poller.rb from the terminal, it finds sites.db and updates it just fine. But when poller.rb is run as a cron job, i get mail saying that there is no such table, sites. This error issues from the following line in poller.rb:
currentEnergy = db.query("SELECT energydata FROM sites")
The previous line in poller.rb,
db=SQLite3::Database.new("sites.db")
issues no error. I just learned about cron jobs today (since being kindly pointed toward them by some of you yesterday) and I think maybe since I have no background knowledge about general unix things (or computer science in general I should say), I can't understand a lot of posts on the subject. I am a lowly mechanical engineer and will not be able to und开发者_如何学编程erstand answers involving unix background knowledge. I simply need to know how to alter my crontab so that cron will know where to find my database file, so that poller.rb will be able to update it. Thanks so much in advance!
Cron doesn't execute with your shell - so any enviornment variables would need to be set. You can write a shell script that does the setting and then runs the ruby script.
Have the script look something like:
. $HOME/.profile
ruby /Users/arpitadey/rails_projects/sample_app/db/poller.r
And just have the cron job execute that script.
Ok, there may not be a .profile script - so create a file in your home directory with all your envirornment variables.
Call it poller.sh
type "env > poller.sh"
edit poller.sh
Make the first line #!/bin/sh
for each following line put export at the start of the line so it will look something like
#!/bin/sh
export GRAILS_HOME=/home/SillyUser/grails/
export PATH=/blah/blah/blah
...
At the bottom of the file:
ruby /Users/arpitadey/rails_projects/sample_app/db/poller.r
Do this in your script:
Dir.chdir(File.expand_path(File.dirname(__FILE__)))
精彩评论