Showing posts with label linux. Show all posts
Showing posts with label linux. Show all posts

Tuesday, November 13, 2012

Useful terminal commands to quickly get system details

To check if your machine is 32bit or 64bit:
$ uname -m

Depending on the output..
i686 = 32bit
x86_64 = 64bit

To check the version of Linux you're running:
$ cat /etc/issue

To check the free space you have on each of your disks:
$ df -h

To check Memory usage by processes (like Task Manager):
$ top

Friday, October 12, 2012

Where is the apache error log!? [Ubuntu]

Q. Where is the apache error log on ubuntu?
A. Here: /var/log/apache2/error.log

Run tail -f /var/log/apache2/error.log to view the last few lines of the log, which are the most recent errors.

Thursday, October 11, 2012

Create terminal shortcut commands

If you often find yourself having to type long commands in the terminal, this tip could be very useful.

For example, to activate my virtualenv I usually have to run something like
$ source ~/.venvburrito/startup.sh
I've substituted that for just
$ activenv

To do this, all you have to do is edit your bashrc file
$ sudo nano ~/.bashrc

Scroll to the bottom and add your alias by adding a line like so..
alias activenv="source ~/.venvburrito/startup.sh"

In this manner you can add multiple aliases. Just make sure to name them wisely and not cause conflicts.
Save and quit (Cntrl+X > Y > Enter) and you're done!

Restart terminal and you should have your new command available to use!

Ref: http://lifehacker.com/270799/create-terminal-shortcuts

Wednesday, October 10, 2012

How to fix django runserver 'port already in use' error

This usually happens if you  $ python manage.py runserver 8000 and then don't stop the server by hitting Cntrl+C but hit something like Cntrl+Z instead.

To stop the server on port 8000 so that you can run another instance again you can do the following..

Run the below command to list all your processes with 'manage' in their name.
$ ps aux | grep -i manage

Note the process_id (pid)  for our "manage.py runserver" process which should be the second column from the left.

And then simply run
$ kill -9 <pid>

e.g. $ kill -9 30144

Ref: http://stackoverflow.com/a/5788609/1415352

Tuesday, October 9, 2012

How to block IP addresses on your Linux server

This link is a very comprehensive and quick guide on how to block IP addresses on your Linux server using Iptables.

Gist:
View all blocked IPs

$ sudo /sbin/iptables -L INPUT -v -n


Block an IP

$ sudo /sbin/iptables -I INPUT -s 11.22.33.44 -j DROP


Unblock an IP
$ iptables -D INPUT -s 11.22.33.44 -j DROP

Create aliases for hosts to quickly SSH in

The folks at  howtogeek.com have a handy little tip on How to create aliases for your hosts to enable you to quickly ssh into them!

The gist is to turn
$ ssh -i yourkey.pem username@123.456.789.012
into
$ ssh dev-server

To do so, edit ~/.ssh/config to look like..

Host dev-server
        User username
        HostName 123.456.789.012
        IdentityFile /path/to/yourkey.pem
        ServerAliveInterval 120
        ServerAliveCountMax 30


You can save multiple hosts like this.

Wednesday, September 12, 2012

Install pip on Ubuntu

This is an excellent little guide on how to install pip on ubuntu: http://www.saltycrane.com/blog/2010/02/how-install-pip-ubuntu/

$ sudo apt-get install python-pip python-dev build-essential

$ sudo pip install --upgrade pip

Friday, September 7, 2012

SCP from EC2 <-> Local

To SCP from EC2 to local..

scp -i eflkey.pem ubuntu@976.934.942.939:/home/ubuntu/upload/views.py /home/keithxm23/


To SCP from local to EC2..

scp -i mykey.pem somefile.txt root@ec2-184-73-72-150.compute-1.amazonaws.com:/

Tuesday, September 4, 2012

How to Change PYTHONPATH in Windows and Ubuntu

Very good guide..
http://greeennotebook.com/2010/06/how-to-change-pythonpath-in-windows-and-ubuntu/


How to Change PYTHONPATH in Windows and Ubuntu

PYTHONPATHLet’s say you’re in the middle of writing a somewhat large application, and you want to test one of your methods to see if it does what you intended. However, the rest of your application is not yet functional. How do you test the method? You could copy the whole thing into the interpreter and test it that way, or you could copy it into a separate script and run it from the command line. However, both of those methods require also copying various other supporting functions and attributes from elsewhere in your application. What a pain! Instead, you could just import your unfinished application like any other module and run it from the interpreter. Assuming you’ve included a statement like if __name__==’__main__’: main() before calling the main function in your application, this will work great. If not, you might need to do that first or your application may try to start when you initialize it from the interpreter, depending on how it’s designed. For more about __name__, see Using __name__ in Python. Anyway, in order to import our application, we need to add our file(s) to the PYTHONPATH, which is the list of directories Python searches through to find modules. It is different from your OS’sPath Environmental Variable. Before we edit our PYTHONPATH, though, let’s look at what it currently is. I happen to know that your PYTHONPATH is the same thing as your sys.path. Let’s see it.
>>> import sys
>>> for i in sys.path:
...     print i
... 

C:\Python25\Lib\idlelib
C:\Python25\Lib\idlelib
C:\Python25\lib\site-packages\mp3play-0.1.15-py2.5.egg
C:\Windows\system32\python25.zip
C:\Python25\DLLs
C:\Python25\lib
C:\Python25\lib\plat-win
C:\Python25\lib\lib-tk
C:\Python25
C:\Python25\lib\site-packages
C:\Python25\lib\site-packages\PIL
C:\Python25\lib\site-packages\win32
C:\Python25\lib\site-packages\win32\lib
C:\Python25\lib\site-packages\Pythonwin
C:\Python25\lib\site-packages\wx-2.8-msw-unicode

Altering PYTHONPATH in Windows: Method 1 (append to sys.path)

The first method is intuitive. Look at our sys.path above. in the example above. Why not just add to it from the interpreter? Let’s say you want to add the directoryC:\Users\Public\Programs.
>>> sys.path.append('C:\\Users\\Public\\Programs')
>>> for i in sys.path:
...     print i
... 

C:\Python25\Lib\idlelib
C:\Python25\Lib\idlelib
C:\Python25\lib\site-packages\mp3play-0.1.15-py2.5.egg
C:\Windows\system32\python25.zip
C:\Python25\DLLs
C:\Python25\lib
C:\Python25\lib\plat-win
C:\Python25\lib\lib-tk
C:\Python25
C:\Python25\lib\site-packages
C:\Python25\lib\site-packages\PIL
C:\Python25\lib\site-packages\win32
C:\Python25\lib\site-packages\win32\lib
C:\Python25\lib\site-packages\Pythonwin
C:\Python25\lib\site-packages\wx-2.8-msw-unicode
C:\Users\Public\Programs
It worked! However, there is one shortfall to this method of editing the PYTHONPATH: it is short-lived. As soon as you close the interpreter, your PYTHONPATH returns to its previous state. This could be a good thing, but if you plan to import from your modules often, you should try one of the other methods.

Altering PYTHONPATH in Windows: Method 2 (with a .pth file)

The second method is also fairly simple. You just create a text file, write the directory path you want to add (e.g. C:\\Users\\Public\\Programs), save it as <AnyFileName>.pth, and place it in your site-packages directory (you can see that mine is at C:\Python25\lib\site-packages). When you install modules, this is how they add their directories to yourPYTHONPATH, and you can see that a few modules have done exactly that in my installation (mp3play, PIL, Pythonwin, win32, etc.).

Altering PYTHONPATH in Windows: Method 3 (With an Environmental Variable)

The third method takes the most work and might seem more permanent, but in reality it’s not that hard to do, and it’s easy to change. In Windows, go to your Environmental Variables editor (Control Panel -> System -> Advanced System Settings -> Environmental Variables). First, look for PYTHONPATH. It’s probably not in there unless you’ve already done this. So click on New, and type PYTHONPATH. Now enter the directory(s) you want to add. Make sure to use the forward-slash format Windows likes (C:\Users\Public\Programs), place a semicolon between directories if you add more than one, do not leave spaces between entries, and do not place a semicolon after the last directory on the list! When you’re finished, your directory will be ADDED to your PYTHONPATH. There is no need to specify those directories that are already in your PYTHONPATH. Similarly, if you’ve added directories using the second method, they will all still be available after you create the PYTHONPATH Environmental Variable even if you don’t include them in it. NOTE: If you’ve edited your PYTHONPATH using the second or third methods, you will not see the changes reflected in your sys.path until you’ve restarted the interpreter!

Altering PYTHONPATH in Ubuntu: Method 1 (append to sys.path)

There are four methods to alter the PYTHONPATH in Ubuntu. The first one is identical to Method 1 in Windows: just append to sys.path. See the description for Windows above for details.

Altering PYTHONPATH in Ubuntu: Method 2 (with a .pth file or symbolic link)

The second method is similar to Method 2 for Windows: place the new path in a .pth file. However, in Ubuntu after Python2.5, the site-packages directory is no longer in the PYTHONPATH by default. Instead, you need to put your .pth file in the dist-packages directory (/usr/local/lib/python2.6/dist-packages). Instead of using a .pth file in that directory, you could also simply create a symbolic link to the dist-packages directory like this:
$ sudo ln -s /home/greeenguru/Programs/example /usr/local/lib/python2.6/dist-packages/example

Altering PYTHONPATH in Ubuntu: Method 3 (temporarily alter PYTHONPATH)

The third method is to temporarily alter the PYTHONPATH Environmental Variable:
$ export PYTHONPATH=/home/greeenguru/Programs/example:$PYTHONPATH
Note that in Linux, paths are separated with a colon whereas in Windows they’re separated with a semicolon. That last part will ensure that the PYTHONPATH Variable is appended to rather than overwritten. This modification only lasts as long as the current shell session.

Altering PYTHONPATH in Ubuntu: Method 4 (permanently alter PYTHONPATH)

In order to permanently alter the PYTHONPATH in Ubuntu, you have to add the export command from the previous method to two of your config files that reside in your home directory: .bashrc and .profile. .bashrc will ensure the PYTHONPATH is updated for any python application running through BASH, and .profile should cover the rest. Just to be sure, though, let’s take a look at the PYTHONPATH before and after:
>>> sys.path
['', '/usr/lib/python2.6', '/usr/lib/python2.6/plat-linux2', '/usr/lib/python2.6/lib-tk', '/usr/lib/python2.6/lib-old', '/usr/lib/python2.6/lib-dynload', '/usr/lib/python2.6/dist-packages', '/usr/lib/python2.6/dist-packages/PIL', '/usr/lib/python2.6/dist-packages/gst-0.10', '/usr/lib/pymodules/python2.6', '/usr/lib/python2.6/dist-packages/gtk-2.0', '/usr/lib/pymodules/python2.6/gtk-2.0', '/usr/lib/python2.6/dist-packages/wx-2.8-gtk2-unicode', '/usr/local/lib/python2.6/dist-packages']
Ok, so that’s the PYTHONPATH before alteration. Now let’s update our .bashrc:
$ echo -e '# Add a directory to PYTHONPATH\nexport PYTHONPATH=/home/greeenguru/Programs/example:$PYTHONPATH' | tee -a ~/.bashrc
After this, you need to close your terminal and open a new one to ensure your new .bashrc is read. Now let’s take a look after updating the .bashrc:
['', '/home/greeenguru/Programs/example', '/home/greeenguru', '/usr/lib/python2.6', '/usr/lib/python2.6/plat-linux2', '/usr/lib/python2.6/lib-tk', '/usr/lib/python2.6/lib-old', '/usr/lib/python2.6/lib-dynload', '/usr/lib/python2.6/dist-packages', '/usr/lib/python2.6/dist-packages/PIL', '/usr/lib/python2.6/dist-packages/gst-0.10', '/usr/lib/pymodules/python2.6', '/usr/lib/python2.6/dist-packages/gtk-2.0', '/usr/lib/pymodules/python2.6/gtk-2.0', '/usr/lib/python2.6/dist-packages/wx-2.8-gtk2-unicode', '/usr/local/lib/python2.6/dist-packages']
Notice that now my home directory and my example directory are both in the PYTHONPATH. But what happens if I try to run the Python Interpreter through IDLE (a Python Shell that doesn’t start through BASH)?:
['/home/greeenguru', '/usr/bin', '/usr/lib/python2.6', '/usr/lib/python2.6/plat-linux2', '/usr/lib/python2.6/lib-tk', '/usr/lib/python2.6/lib-old', '/usr/lib/python2.6/lib-dynload', '/usr/lib/python2.6/dist-packages', '/usr/lib/python2.6/dist-packages/PIL', '/usr/lib/python2.6/dist-packages/gst-0.10', '/usr/lib/pymodules/python2.6', '/usr/lib/python2.6/dist-packages/gtk-2.0', '/usr/lib/pymodules/python2.6/gtk-2.0', '/usr/lib/python2.6/dist-packages/wx-2.8-gtk2-unicode', '/usr/local/lib/python2.6/dist-packages']
My home directory is there for some reason, but my example directory definitely didn’t make it. Now let’s try updating the .profile config file:
$ echo -e '# Add a directory to PYTHONPATH\nexport PYTHONPATH=/home/greeenguru/Programs/example:$PYTHONPATH' | tee -a ~/.profile
Your .profile config file is only read when you log in, as far as I know, so you have to log out and log back in again for the change to take effect. Here’s what the PYTHONPATH looks like in IDLE after logging back in:
['/usr/bin', '/home/greeenguru/Programs/example', '/home/greeenguru', '/usr/lib/python2.6', '/usr/lib/python2.6/plat-linux2', '/usr/lib/python2.6/lib-tk', '/usr/lib/python2.6/lib-old', '/usr/lib/python2.6/lib-dynload', '/usr/lib/python2.6/dist-packages', '/usr/lib/python2.6/dist-packages/PIL', '/usr/lib/python2.6/dist-packages/gst-0.10', '/usr/lib/pymodules/python2.6', '/usr/lib/python2.6/dist-packages/gtk-2.0', '/usr/lib/pymodules/python2.6/gtk-2.0', '/usr/lib/python2.6/dist-packages/wx-2.8-gtk2-unicode', '/usr/local/lib/python2.6/dist-packages']
It worked!

Popular Posts