问题
I want to execute rake sunspot:reindex RAILS_ENV=production command for every four hours. To achieve this i have written simple bash script using shell programming. I will run successfully in terminal but if i tried to run it in crontab then it is not working. Operating system which am using is centos.
Here is my bash script code
#!/bin/bash
#export PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin
export PATH=/usr/local/rvm/gems/ruby-1.9.2-p290/bin:/usr/local/rvm/gems/ruby-1.9.2-p290@global/bin:/usr/local/rvm/rubies/ruby-1.9.2-p290/bin:/usr/local/rvm/bin:/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/X11R6/bin:/root/bin:
echo "started"
cd /var/www/html/spotchase2
pwd
source /usr/local/rvm/environments/ruby-1.9.2-p290
bundle install
if [ "$?" = 0 ]; then
echo `date`
fi
source /usr/local/rvm/environments/ruby-1.9.2-p290
#/usr/local/rvm/gems/ruby-1.9.2-p290/bin/rake sunspot:reindex RAILS_ENV=production
rake sunspot:reindex RAILS_ENV=production
if [ "$?" = 0 ]; then
echo "complete"
fi
Please help me to fix this issue.
EDIT
I am redirecting the output one text file. In that text file am getting only started, bundle install and date. Am not getting complete message, therefore i came to know that it is not working and am not getting any error on that text file.
Please help me to fix this issue.
回答1:
Usually, I use the whenever gem to handle something like this. I don't have to write a bash script, a rake script is okay enough. Whenever
will generate the correct syntax for crontab.
In my project, I have several script that need to be run in background all the time. Here is the command to generate contab file.
whenever --update-crontab --set environment=production --load-file config/schedule_client.rb --user ubuntu
And it will generate into something like this.
0 2 * * * /bin/bash -l -c 'source /home/ubuntu/.rvm/scripts/rvm && cd /var/www/yoolk_statistics_api && RAILS_ENV=production bundle exec rake statistics:generate_clienst --silent >> log/client.log 2>&1'
To make sure my crontab always work, I redirect the output to the log file so that i can see it's running or not. In my ubuntu server, I usually check the syslog file
for debugging.
来源:https://stackoverflow.com/questions/10816934/how-to-run-sunspot-reindex-in-background-for-every-four-hours