Category Archives: Unix

Logging Cron Job

ref – http://stackoverflow.com/questions/4811738/cron-job-log-how-to-log

http://stackoverflow.com/questions/15395479/why-ive-got-no-crontab-entry-on-os-x-when-using-vim

Getting started with Cron
  • In Terminal: crontab -e.
  • Press i to go into vim’s insert mode.
  • Type your cron job, for example:

    30 * * * * /Users/rickytsao/Desktop/run_email_to_admin >> /Users/rickytsao/Desktop/chron-log.txt
  • Press Esc to exit vim’s insert mode.
  • Type ZZ (must be capital letters).
  • You should see the following message: crontab: installing new crontab. You can verify the crontab file by using crontab -l.
Logging Cron jobs


30 * * * * /Users/rickytsao/Desktop/run_email_to_admin >> /Users/rickytsao/Desktop/chron-log.txt

Copying and placing folders

http://askubuntu.com/questions/86822/how-can-i-copy-the-contents-of-a-folder-to-another-folder-in-a-different-directo

For local paths:

For full paths:

Installing mongodb on Ubuntu

ref – http://www.mkyong.com/mongodb/how-to-install-mongodb-on-ubuntu/

This guide shows you how to install MongoDB on Ubuntu.

Ubuntu 12.10
MongoDB 2.2.3

1. Add 10gen package to source.list.d

Ubuntu 12 comes with a “mongo” package, but not the latest version.

$ sudo apt-cache search mongodb
mongodb
mongodb-clients
mongodb-dev
mongodb-server

It’s recommended to add 10gen package to /etc/apt/sources.list.d, as it contains the latest stable MongoDB. Create a /etc/apt/sources.list.d/mongo.list file, and declared the 10gen distro.

$ sudo vim /etc/apt/sources.list.d/mongo.list

/etc/apt/sources.list.d/mongo.list

##10gen package location

deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen

2. Add GPG Key

10gen package required GPG key, imports it :

$ sudo apt-key adv –keyserver keyserver.ubuntu.com –recv 7F0CEB10

If you didn’t imports the GPG key, apt-get update will hits following error message :

GPG error: http://downloads-distro.mongodb.org dist Release:
The following signatures couldn’t be verified because the public key is not available: NO_PUBKEY 9ECBEC467F0CEB10

3. Update package

Update your apt-get list.

$ sudo apt-get update

Search “mongodb” again, a new 10gen package is appearing now. Get the “mongodb-10gen“, it contains the latest stable MongoDB.

$ sudo apt-cache search mongodb
mongodb
mongodb-clients
mongodb-dev
mongodb-server

mongodb-10gen
mongodb18-10gen
mongodb20-10gen

4. Install mongodb-10gen

Everything is ready, now you can Install MongoDB :

note see (http://stackoverflow.com/questions/12480935/install-mongodb-10gen-failed-with-apt-get) if you get an overwrite error.

$ sudo apt-get install mongodb-10gen

5. Where is MongoDB?

MongoDB is installed and started.

$ ps -ef | grep mongo
mongodb 5262 1 0 15:27 ? 00:00:14 /usr/bin/mongod –config /etc/mongodb.conf
mkyong 5578 3994 0 16:29 pts/0 00:00:00 grep –color=auto mongo

$ mongo -version
MongoDB shell version: 2.2.3

All MongoDB executable files are stored at /usr/bin/

$ ls -ls /usr/bin | grep mongo
4220 -rwxr-xr-x 1 root root 4317928 Feb 2 08:11 mongo
10316 -rwxr-xr-x 1 root root 10563336 Feb 2 08:11 mongod
10320 -rwxr-xr-x 1 root root 10563664 Feb 2 08:11 mongodump
10284 -rwxr-xr-x 1 root root 10526736 Feb 2 08:11 mongoexport
10324 -rwxr-xr-x 1 root root 10567768 Feb 2 08:11 mongofiles
10296 -rwxr-xr-x 1 root root 10539056 Feb 2 08:11 mongoimport
10272 -rwxr-xr-x 1 root root 10514544 Feb 2 08:11 mongooplog
10272 -rwxr-xr-x 1 root root 10518512 Feb 2 08:11 mongoperf
10320 -rwxr-xr-x 1 root root 10563632 Feb 2 08:11 mongorestore
6644 -rwxr-xr-x 1 root root 6802848 Feb 2 08:11 mongos
10312 -rwxr-xr-x 1 root root 10556560 Feb 2 08:11 mongostat
10272 -rwxr-xr-x 1 root root 10515856 Feb 2 08:11 mongotop

The “mongodb control script” is generated at /etc/init.d/mongodb

$ ls -ls /etc/init.d | grep mongo
0 lrwxrwxrwx 1 root root 21 Feb 2 08:11 mongodb -> /lib/init/upstart-job

The MongoDB config file is at /etc/mongodb.conf
/etc/mongodb.conf

# mongodb.conf

# Where to store the data.

# Note: if you run mongodb as a non-root user (recommended) you may
# need to create and set permissions for this directory manually,
# e.g., if the parent directory isn’t mutable by the mongodb user.
dbpath=/var/lib/mongodb

#where to log
logpath=/var/log/mongodb/mongodb.log

logappend=true

#port = 27017

#……

6. Controlling MongoDB

Some commands to control the MongoDB.

Starting MongoDB

$ sudo service mongodb start

Stoping MongoDB

$ sudo service mongodb stop

Restarting MongoDB

$ sudo service mongodb restart

Using mongo via terminal for DB manipulation


$ mongo

MongoDB shell version: 3.0.4
connecting to: test

> show dbs

bookAPI 0.078GB
local 0.078GB
my_database_name 0.078GB

CREATE A DATABASE

Don’t worry about “connecting to” … that’s just the default database Mongo decides to use if you don’t specify one on the command line, which we didn’t because it’s not important right now. It doesn’t actually even create the “test” database unless you add a record. It’d be totally fine to just work in that database for right now, but let’s make one of our own. In your Mongo console, type the following:

Now we’re using the database “test1.” Nothing actually exists yet. To make the database exist, we have to add some data. We’re going to start off by doing that right inside of the Mongo client.

ADD SOME DATA

MongoDB uses JSON for its structure.

Let’s add a record to our collection. Note that we are currently ‘on’ database test1. So whatever we insert will be into database ‘test1’. Our data format will thus look like this:

Something important to note here: that “db” stands for our database, which as mentioned above we’ve defined as “test1”. The “usercollection” is the name of our collection in this database. Its kind of like ‘table’ in a SQL database. Every JSON we insert is like a row. Note that there wasn’t a step where we created the “usercollection” collection. That’s because the first time we add to it, it’s going to be auto-created. OK, Hit enter. Assuming everything went right, you should see … nothing. That’s not very exciting, so type this:

Showing all data from table

You access the table name by saying ‘db’ for current database, then db.tableName, which in our case is users, so it would be db.users. Finally, we use find() to display all data so in the end it would be:

db.users.find()

output:

{ “_id” : ObjectId(“559261a6250879f91747a6a5”), “name” : “MODULUS ADMIN”, “age” : 42, “roles” : [ “admin”, “moderator”, “user” ], “__v” : 0 }
>

Find where age > “—“


> db.users.find( { age: { $gt: 25 } } )

{ “_id” : ObjectId(“559261a6250879f91747a6a5”), “name” : “MODULUS ADMIN”, “age” : 42, “roles” : [ “admin”, “moderator”, “user” ], “__v” : 0 }

> db.users.find( { age: { $gt: 43 } } )

>

Clear out a Collection

Removing objects where name is “—“


> db.users.remove({name:”MODULUS ADMIN”})

WriteResult({ “nRemoved” : 1 })

> db.users.find()

>

counting how many are in a table


> db.users.count()

0

Insert

If the collection does not exist, the insert() method creates the collection.

> db.users.insert( { name: “ricky”, age: 35, roles:”admin” } )

Dropping Collections


db.{collection name}.drop()

Dropping Database


use my_database_name

switched to db my_database_name

> db.dropDatabase()

{ “dropped” : “my_database_name”, “ok” : 1 }

Viewing contents of a database

first show all database

> show dbs;

PersonDatabase 0.078GB
local 0.078GB

Then pick a database to use

> use PersonDatabase;

switched to db PersonDatabase

Then show all collections inside that database

> show collections

people
system.indexes

Then do db.{collection name}.find()

> db.people.find()

{ “_id” : ObjectId(“55939a0800f3e7343974a41f”), “name” : “Ricky-dude”, “username” : “rtsao”, “password” : “compaq”, “admin” : true, “__v” : 0 }
{ “_id” : ObjectId(“55939adc2b0f71473b38b3d6”), “name” : “Ricky-dude”, “username” : “hadoken”, “password” : “compaq”, “admin” : true, “__v” : 0 }
{ “_id” : ObjectId(“55939b81648f93fd3bb1f4f1”), “name” : “Ricky-dude”, “username” : “shoryuken”, “password” : “compaq”, “admin” : true, “__v” : 0 }

inserting dates via terminal

db.badmintonEvents.insert({ “attendee_name” : “badminton_2-18-2016”, “date” : ISODate(“2016-02-18T00:00:00Z”), “description” : “8:00pm at baishizhou” })

Install MongoDB on Mac

ref – http://www.mkyong.com/mongodb/how-to-install-mongodb-on-mac-os-x/

Moving files

First download the mongo database file from its website. Be default, your mac should put it into your ~/Download folder. Then open up a terminal.

You cd into the download directory. Then you unzip the tgz file. Then you move the folder into directory /usr/local/mongdb


cd ~/Download
$ tar xzf mongodb-osx-x86_64-2.2.3.tgz
$ sudo mv mongodb-osx-x86_64-2.2.3 /usr/local/mongodb

You should now see the mongodb folder with bin and data in the directory /usr/local/mongdb.

data/db folder

By default, MongoDB write/store data into the /data/db folder, you need to create this folder manually and assign proper permission. We make the directory, then check who the current User is. Then give permission to the current user for directory /data/db


$ sudo mkdir -p /data/db
$ whoami

rickytsao

$ sudo chown rickytsao /data/db

Now, the directory /data/db is ready for mongodb data writes.

.bash_profile file

Now we need to open up our .bash_profile and add a PATH entry. This is so that we can run mongod, and mongo from the /usr/local/mongodb/bin directory without having to type out the whole path.


$ cd ~
$ pwd
/Users/rickytsao
$ touch .bash_profile
$ vim .bash_profile

Then type in the following into the .bash_profile file:

export MONGO_PATH=/usr/local/mongodb
export PATH=$PATH:$MONGO_PATH/bin

restart terminal, then:


$ mongo -version

MongoDB shell version: 2.2.3

In terminal 1:

$ mongod

MongoDB starting : pid=34022 port=27017 dbpath=/data/db/ 64-bit host=mkyong.local
//…
waiting for connections on port 27017

In terminal 2:

$ mongo

MongoDB shell version: 2.2.3
connecting to: test

> show dbs

local (empty)

If you happen to get a “rlimits warning”, just type just type “ulimit -n 2048” or use something higher.

Using Mongoose


npm install mongoose –save

Installing NodeJS on Mac

ref – http://stackoverflow.com/questions/11177954/how-do-i-completely-uninstall-node-js-and-reinstall-from-beginning-mac-os-x

Install Homebrew

Homebrew is a package manager for the Mac — it makes installing most open source sofware (like Node) as simple as writing brew install node. You can learn more about Homebrew at the Homebrew website. To install Homebrew just open Terminal and type:

You’ll see messages in the Terminal explaining what you need to do to complete the installation process.

result:

Last login: Sat Jun 6 05:15:33 on console
Rickys-MacBook-Pro:~$ ruby -e “$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)”
==> This script will install:
/usr/local/bin/brew
/usr/local/Library/…
/usr/local/share/man/man1/brew.1
==> The following directories will be made group writable:
/usr/local/lib/pkgconfig
/usr/local/share/man/man3
/usr/local/share/man/man5
/usr/local/share/man/man7

Press RETURN to continue or any other key to abort
==> /usr/bin/sudo /bin/chmod g+rwx /usr/local/lib/pkgconfig /usr/local/share/man/man3 /usr/local/share/man/man5 /usr/local/share/man/man7

WARNING: Improper use of the sudo command could lead to data loss
or the deletion of important system files. Please double-check your
typing when using sudo. Type “man sudo” for more information.

To proceed, enter your password, or type Ctrl-C to abort.

Password:
==> /usr/bin/sudo /bin/mkdir /Library/Caches/Homebrew
==> /usr/bin/sudo /bin/chmod g+rwx /Library/Caches/Homebrew
==> Downloading and installing Homebrew…
remote: Counting objects: 3590, done.
remote: Compressing objects: 100% (3433/3433), done.
remote: Total 3590 (delta 28), reused 667 (delta 19), pack-reused 0
Receiving objects: 100% (3590/3590), 2.86 MiB | 322.00 KiB/s, done.
Resolving deltas: 100% (28/28), done.
From https://github.com/Homebrew/homebrew
* [new branch] master -> origin/master
Checking out files: 100% (3594/3594), done.
HEAD is now at 231c429 unac: fix audit warnings
==> Installation successful!
==> Next steps
Run brew help to get started
Rickys-MacBook-Pro:~ $

Installation

Removing previous installations of NodeJS

If its your first time installing nodeJS, then skip this part go on to the standard installation instructions below

However, if you have previous versions of nodeJS installed, you need to remove previous node folders and files.

  • go to /usr/local/lib and delete any node and node_modules
  • go to /usr/local/include and delete any node and node_modules directory
  • if you installed with brew install node, then run brew uninstall node in your terminal
  • check your Home directory for any local or lib or include folders, and delete any node or node_modules from there
  • go to /usr/local/bin and delete any node executable
  • remove /usr/local/lib/dtrace/node.d
  • remove /usr/local/share/man/man1/node.1

Usually every time you do an install, and if there are any issues, it will print out for you…something like this:

Error: The brew link step did not complete successfully
The formula built, but is not symlinked into /usr/local
Could not symlink share/man/man1/node.1
Target /usr/local/share/man/man1/node.1
already exists. You may want to remove it:
rm ‘/usr/local/share/man/man1/node.1’

Just solve the error, do a “brew uninstall node”, and start the installation all over again.

Actual installation

Installing Node.js and NPM is pretty straightforward using Homebrew. Homebrew handles downloading, unpacking and installing Node and NPM on your system. The whole process (after you have XCode and Homebrew installed) should only take you a few minutes.

Open the Terminal app and type brew install node

Sit back and wait. Homebrew downloads some files and installs them. And that’s it.

After you entered the command you should see the installation go under way:

Rickys-MacBook-Pro:~ rickytsao$ brew install node
==> Downloading https://homebrew.bintray.com/bottles/node-0.12.4.yosemite.bottle.tar.
######################################### 75%

==> Downloading https://homebrew.bintray.com/bottles/node-0.12.4.yosemite.bottle.tar.
Already downloaded: /Library/Caches/Homebrew/node-0.12.4.yosemite.bottle.tar.gz
==> Pouring node-0.12.4.yosemite.bottle.tar.gz
==> Caveats
Bash completion has been installed to:
/usr/local/etc/bash_completion.d
==> Summary
🍺 /usr/local/Cellar/node/0.12.4: 2590 files, 28M
Rickys-MacBook-Pro:~ rickytsao$

After installation, check what version of Node and NPM you’ve just installed:

To see if Node is installed, type node -v in Terminal. This should print the version number so you’ll see something like this v0.10.31.
To see if NPM is installed, type npm -v in Terminal. This should print the version number so you’ll see something like this 1.4.27

How to Update Node and NPM

New versions of Node and NPM come out frequently. You can use Homebrew to update the software it installs.
Make sure Homebrew has the latest version of the Node package. In Terminal type brew update
Upgrade Node: brew upgrade node

How to Uninstall Node and NPM

You can use Homebrew to uninstall packages that it installed: brew uninstall node