Search This Blog

Monday, December 12, 2011

MongoDB on startup for Mac OSX Lion


Hey,

Here's the steps to start your MongoDB every time you switch on your Mac machine.

Step1: Save the below contents into /Library/LaunchDaemons/org.mongo.mongod.plist

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>org.mongo.mongod</string>
<key>RunAtLoad</key>
<true/>
<key>ProgramArguments</key>
<array>
<string>/opt/local/bin/mongod</string>
<string>--dbpath</string>
<string>/opt/local/var/db/mongodb</string>
<string>--logpath</string>
<string>/opt/local/var/log/mongodb.log</string>
</array>
</dict>
</plist>

Step2: Create the log file and DB directory

sudo chown root:wheel /Library/LaunchDaemons/org.mongo.mongod.plist
sudo touch /var/log/mongodb.log
sudo mkdir /var/lib/mongodb
sudo mkdir -p /opt/local/var/db/mongodb 


Step3: Run the following commands in terminal to load the file into launchctl

sudo launchctl load /Library/LaunchDaemons/org.mongo.mongod.plist 
sudo launchctl start org.mongo.mongod


Thats it! Happy coding!

Here's a useful links : MongoDB-OSX-Launchctl

Saturday, November 5, 2011

Ruby on Rails set up on Apple Mac OS X

Hey guys.. 


Recently I setup rails in my new Apple Mac OS X Lion 10.7. Here's the steps I followed..


1. Firstly, install XCode onto your Mac machine. Xcode is a suite of tools, developed by Apple, for developing software for Mac OS X and iOS
  • You can install XCode from the application DVD thats shipped in along with your MacBook. If its not present, then its available on the Mac App Store for free for Mac OS X 10.7, however it requires a registered Apple ID (also requires a credit card to register. damn you!).
  • Well, if you are installing it through Mac App Store, then I recommend you do start it by twilight so thats its complete by the next day morning (Took me hell lot of time man!)
  • Ruby comes preinstalled on your Mac. But its an older version! XCode also contains Git by default. you can check the version of Git by 
git --version  
  •  If Git is not installed by default, you need to install it by using HomeBrew. So first lets install Brew and then Git:
ruby -e "$(curl -fsSL https://raw.github.com/gist/323731)" 
brew install git
 2. After installing the XCode, you need to install RVM. Here's how you can do it..
bash < <( curl -s https://rvm.beginrescueend.com/install/rvm ) 
echo '[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm" ' >> ~/.bash_profile
  •  close the terminal and reopen a new one and enter this:
type rvm | head -1
  •  This will output "rvm is a function" which means RVM is installed!

3. Now lets install the
Ruby version 1.9.2. Here's the command for that:
rvm install ruby-1.9.2-p290
  •  So now, just do
rvm list
  • And you wil see the newly installed ruby.. When it's done, set Ruby 1.9.2 as the current Ruby version in your Terminal session by typing
rvm use ruby-1.9.2-p290
  • If you restart Terminal, and type "ruby -v" again, you’ll likely find that it has defaulted back to the system version of Ruby: 1.8.7. That’s no good! Let’s be sure to make 1.9.2 the default. Here's the command:
rvm --default use ruby-1.9.2-p290
  • Now, lets update the ruby gems and rake by:
gem update --system
sudo gem update rake

4. Finally, the time has come to install Rails 3.1.0 in your Mac:
gem install rails --version=3.1.0 --include-dependencies
  •  This will install Rails along with the required dependencies.
  • If you need MySQL of PostgreSQL, Make sure you have installed HomeBrew which is covered above.


Installing MySQL:
brew install mysql
  •  Make sure that you follow the instructions at the end to complete the installation and setup. You can review the post install instructions at any time by using the Homebrew info command.
brew info mysql

 Installing PostgreSQL:
brew install postgres
  • Again, make sure that you follow the instructions at the end to complete the installation and setup. As with MySQL, you can review the post install instructions at any time by using the Homebrew info command.
brew info postgres
Thats it folks, you are ready to go!

Sunday, October 9, 2011

Rails 3 : Mailer


Hey fellas, Here's a quick view to convert your rails 2.x mailer to rails 3.x
 
There wont be any change in the path of views files. It wil reside in app/views/user_mailer.

The user_mailer.rb [wch was in models] is now moved to app/mailers
 
The mailer setting are moved to configuration/initializers/mail_setup.rb

Here is the changes that has to be done for sending mails:

In 2.X: UserMailer.deliver_notify_phishing_rsa(phishing, email, address)
In 3.X: UserMailer.notify_phishing_rsa(phishing, email, address).deliver

In user_mailer.rb, there is change in syntax for objects/parameters that needs to be sent to the email body.

In 2.X: @body['phishing'] = phishing
In 3.X: @phishing = phishing

In Views, we can use the following to provide a link in the email body:

<%= link_to "Edit Phishing", edit_phishing_url(@phishing, :host => "www.example.com") %>

OR

<%= edit_phishing_url(@phishing, :host => "localhost:3000") %>


Also, We can attach files with the following syntax inside user_mailer.rb:

attachments["phishingreport.pdf"] = File.read("#{Rails.root}/public/uploads/phishingreport.pdf")

For inline attachments:

attachments.inline['myimage.jpg'] = File.read('/path/to/myimage.jpg')

<%= image_tag attachments['myimage.jpg'].url %> #in views
 
 

Monday, May 9, 2011

CRON on Linux


Cron is a Unix utility that allows tasks to be automatically run in the background at regular intervals by the Cron daemon. These tasks are often termed as Cron Jobs in Unix. You will use it on your Unix or Linux operating systems for doing some tasks at specific intervals with out your intervention every time. You set the clock and forget. The Cron daemon runs the work for you.

What is crontab?


Crontab is a text file that contains a series of cron functions. Linux server uses a Cron Daemon to handle the Cron Jobs which are set in the Crontabs of different users. The Cron Daemon which handles this on Linux servers is called “Crond

Is my Cron running? If not, how to start the Cron?


 Cron is a daemon, which means that it only needs to be started once, and will lay dormant 
until it is required [Similar to a Web server daemon which stays dormant until it gets asked for a
web page]. The Cron daemon, or Crond, stays dormant until a time specified in one of the config
files, or crontabs.

On most Linux distros crond is automatically installed and entered into the start up scripts. 
To find out if it's running do the following:
arjun@vigilance: $ ps aux | grep crond
root       311      0.0  0.7  1284   112  ?          S    Apr24   0:00 crond
arjun    25645  0.0  0.0   4012   756 pts/3    S+   01:45   0:00 grep crond

Note: Its ps aux | grep cron for distros like Ubuntu.

The top line in the output shows that crond is running, the bottom line is the search we just run.

If it's not running then either you killed it since the last time you rebooted, or it wasn't started. 
The crond service can be stopped or started using the scripts below:
arjun@vigilance: $ /etc/init.d/crond stop
arjun@vigilance: $ /etc/init.d/crond start

Note: Use sudo if you are not a root user. For few distros, its /etc/init.d/cron start


Crontab Commands

$ crontab -e => Edit your crontab file, or create one if it doesn’t already exist.
$ crontab -l => Display your crontab file.
$ crontab -r => Remove your crontab file.
$ crontab -v => Display the last time you edited your crontab file. (This option is only available on a few systems.)
$ export EDITOR=vim.tiny => to specify a editor to open crontab file.

Crontab file


The general syntax of a Unix Crontab is :


[minute] [hour] [day of month] [month] [day of week] [command to run]


A Crontab file has six fields. The acceptable values for each of the 6 fields are:



Field
Range of values
minute
0-59
hour
0-23
Day of month
1-31
month
1-12
Day of week
0-7 (where both 0 and 7 mean Sun, 1 = Mon, 2 = Tue, etc)
Command to run
The command to run along with the parameters to that command if any

The fields have to be in that exact order, with no empty or missing fields. An asterisk (*) is used to indicate that every instance (i.e. every hour, every weekday, etc.) of the particular time period will be used.


Examples on how to set up Cron Job


Open the Crontab by:

arjun@vigilance:$ crontab -e


Now, press i [For switching into insert mode] and add your Cron Job like below examples. After adding, press Shift+Z+Z to save and exit from Crontab OR press esc and then type :q! And then hit enter to quit without saving.
 

Examples


# Run Webalizer to update Apache Log file at the 28th min of every hour.

28 * * * * /usr/bin/webalizer


# Run Webalizer to update Apache Log files every 10 minutes.

*/10 * * * * /usr/bin/webalizer



# Run /usr/bin/backup on the 15th of any month at only if its a Saturday

0 12 15 * 6 /usr/bin/backup



# A line in crontab file like below removes the tmp files from /home/someuser/tmp each day at 6:30 PM.

30 18 * * * rm /home/someuser/tmp/*



# At 12.05,12.10 every Monday & on 10th of every month

5,10 0 10 * 1 your_command



# If you want the cron job to run at 1:00 and 2:00 A.M every day, every month and every week.

* 1,2 * * * /home/username/www/members.php




# If you want to run the above task only from Monday to Friday and at 11.30 P.M

30 23 * * 1-5 /home/username/www/members.php



# Will run connection.test every 15 mins between the hours or 9am and 5pm
*/15 9-17 * * * connection.test


# This command is run at 4 am every Sunday
* 4 * * Sun cat test.txt << “Gotcha!!”


# This command is run 4:42 am every 1st of the month
42 4 1 * * cat test.txt << “Gotcha!!”



# Will run cmd every midday between the 1st and the 15th as well as the 20th 
and 25th (inclusive) and also on the 17th of every month.

* 12 1-15,17,20-25 * * cmd


Some of the options which can come handy are given below


Minutes(s)(0-59)
Value

Hour(s)(1-23)
Value
Every Minute
*

Every Hour
*
Every Other Minute
*/2

Every Other Hour
*/2
Every Five Minutes
*/5

Every Five Hours
*/5
1st minute
1

12 at noon
12
2nd minute
2

6 AM
6
58th minute
58

10 at night
22
59th minute
59

12 in morning
0
Day(s)(1-31)
Value

Month(s)(1-12)
Value
Everyday
*

Every Month
*
1st Day
1

January
1
2nd Day
2

February
2
Day 30
30

December
12
Day 31
31






Weekdays( 0-6)
Value



Every Weekday
*



Sunday
0



Monday
1



Friday
5



Saturday
6


Notes


  1. The value column can have a * or a list of elements separated by commas. An element is either a number in the ranges or two numbers in the range separated by a hyphen (meaning an inclusive range).
  2. Repeat pattern like /2 for every 2 minutes or /10 for every 10 minutes is not supported by all operating systems. If you try to use it and crontab complains it is probably not supported.
  3. The specification of days can be made in two fields: month day and weekday. If both are specified in an entry, they are cumulative meaning both of the entries will get executed .
  4. If you inadvertently enter the crontab command with no argument(s), do not attempt to get out with Control-d. This removes all entries in your crontab file. Instead, exit with Control-c.
  5. Script names are case sensitive on Unix. So be careful in entering the script name.
  6. If you don't wish to specify a value for a field, just place a * in the field.
  7. When using the names of weekdays or months, it isn't case sensitive, but only the first three letters should be used, e.g. Mon, sun or Mar, jul.
  8. Comments are allowed in crontabs, but they must be preceded with a '#', and must be on a line by them self.
  9. The cronjob which is set and saved for the user arjun gets saved inside the file /var/spool/cron/arjun.
  10. If you wish to use more than one instance of a particular time periods, then seperate the times by a comma. If you wish for continuous execution, the start and stop items are separated by a dash. For example, if you wanted to run your command at :05 and :35 past the hour, every hour, Monday through Friday, then your time stamp would look like this:

5,35 * * 1-5 your_command

The fifth position indicates which task will be run at the given time(s).