Showing posts with label cron. Show all posts
Showing posts with label cron. Show all posts

5.24.2016

Cron Like Program in Ruby with Clockwork

I like this gem I discovered called clockwork. A good replacement for cron plus more because you can do extra programming when scheduling jobs.
require 'clockwork'
module Clockwork
  handler do |job|
    puts "Running #{job}"
  end 

  # handler receives the time when job is prepared to run in the 2nd argument
  # handler do |job, time|
  #   puts "Running #{job}, at #{time}"
  # end

  every(1.hour, 'hourly.ms_status') do
    `cd $HOME/Documents/projects/monweb_management && $HOME/.rvm/wrappers/ruby-2.3.0@RailsDev/rake file:get_ms_status`
  end 

  every(1.day, 'daily.pbba_query', :at => '07:00') do
    `cd $HOME/Documents/projects/monweb_management && $HOME/.rvm/wrappers/ruby-2.3.0@RailsDev/rake file:get_pbba_sizing`
  end 
end
Then run it like this...
$ clockwork job_scheduler.rb
I, [2016-05-24T09:18:21.215569 #771]  INFO -- : Starting clock for 2 events: [ hourly.ms_status daily.pbba_query ]
I, [2016-05-24T09:18:21.215729 #771]  INFO -- : Triggering 'hourly.ms_status'

6.05.2014

Schedule rake using cron on rvm environment

Little tricky to get this going. The key is knowing rvm environment.
$ rvm env --path
/auto/home3/delacs/.rvm/environments/ruby-2.0.0-p353@RailsDev
For rvm, basic command line tools like gem, rake and ruby are in the wrappers directory.
$ crontab -l
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
HOME=/auto/home3/delacs
RAILS_ENV=development

0 7 * * * /bin/bash -l -c 'source ~/.bash_profile' && cd $HOME/Documents/projects/monweb_management && $HOME/.rvm/wrappers/ruby-2.0.0-p353@RailsDev/rake perfdb:get_baselines >> $HOME/logs/db.import.log 2>&1

5 7 * * * /bin/bash -l -c 'source ~/.bash_profile' && cd $HOME/Documents/projects/monweb_management && $HOME/.rvm/wrappers/ruby-2.0.0-p353@RailsDev/rake perfdb:get_published_runs  >> $HOME/logs/db.import.log 2>&1

5.08.2014

Backup Dir and Files in Linux Using Tar and Bash

1. Create a bash script
$ vi backup.sh
#!/bin/bash
# This is a script the creates a backup of
# monitor instances: .../monitor/inst => .../monitor/backup

DATE=`date +"%Y%m%d"`
TIMESTAMP=`date +"%Y/%m/%d %T"`
FILENAME="inst_backup_$DATE.tar.gz"
SRCDIR="inst"
DESDIR="backup"

tar -cpzf $DESDIR/$FILENAME $SRCDIR

if [ $? -eq 0 ]; then
    echo "$TIMESTAMP: Backup success: $FILENAME"
else
    echo "$TIMESTAMP: Backup error: $?"
fi

# Any backup older than 30 days are deleted
find ./$DESDIR -type f -mtime +30 -delete -printf "$TIMESTAMP: Deleted %16f\n"

for tar:
c - create
p - preserve file and dir permission
z - compress file
f - target filename

2. Schedule with cron
$ crontab -e
0 5 * * * cd $HOME/scheduler/monitor && ./backup.sh >> $HOME/scheduler/monitor/backup/logs/monitor.backup.log 2>&1

This would schedule a backup at 5am everyday