Showing posts with label Linux. Show all posts
Showing posts with label Linux. Show all posts

Download activity data from Garmin Forerunner 310XT on Linux

My everyday computer is running CentOS. There is no way install Garmin connect to upload data from my old 310XT. I am sure there are other ways to do so, but the following works for me.
There are two software packages need to install: openant and ant-fs.

1. Steps to install openant
 git clone https://github.com/Tigge/openant
cd openant
sudo python setup.py install

2. steps to install ANT-FS
git clone https://github.com/Tigge/antfs-cli
cd antfs-cli
sudo python setup.py install

3. Pair your ant receiver with your watch
antfs-cli  --pair

4. Read data from your watch
antfs-cli 
The downloaded fit data from your watch is saved at
~/.config/antfs-cli/###/activities/


Dell H310 RAID Controller

My workstation running CentOS 6.7 hangups unexpected recently every since I re-imaged it. A little improvement has been seen by adding "elevator=noop" to the boot parameter. But it does not fix the source of the problem.
I has always been suspecting the raid controller, Dell H310. First I tried updating the firmware without any improvement. To update firmware in Linux, you could use Rufus (http://rufus.akeo.ie) to create a FreeDOS bootable USB driver and put the windows version of the firmware to the USB driver.
All these fixes did not eliminate the problem from source, I wrote an email to Dell support. Finally, I got confirmed from Dell tech support that "....While the controller can do RAID 5, we in tech support really only recommend RAID 0 or RAID 1 with that controller....."
After changing to RAID 0 and running a month now, I have never encountered the hangup again.

Linux: Run process as background and never dies

When you run your process from Putty SSH and the process will die after a certain time's inactivity of PuTTY terminal. Here is a simple solution to avoid lose your work:
nohup python start.py &
Note: Please do not start a web server command from putty terminal with & at the end of the command. THE FOLLOWING MAY CAUSE unexpected website behave PROBLEM:<
command_to_start_a_web_server &

Install Eclipse Kepler On Ubuntu 12.04.3

Here are the simple steps, I used to update eclipse to Kepler on my Ubuntu 12.04.3 box. 1. download the appropriate package of eclipse from http://www.eclipse.org/downloads/ untar the package and copy it to the location you prefer, /opt in my case
# untar
tar -zvxf eclipse-standard-kepler-R-linux-gtk-x86_64.tar.gz
# copy the uncompressed folder into /opt
sudo cp -R eclipse /opt
# change owner of all files to root
sudo chown -R root:root /opt/eclipse
2. link eclipse executable to /usr/bin/
sudo ln -s /opt/eclipse/eclipse /usr/bin/eclipse
3. Create the following .desktop file if you want to be able to pin eclipse to the luncher.
# create eclipse.desktop file at /usr/share/applications/
gedit /usr/share/applications/eclipse.desktop
4. put the following contents in the file
[Desktop Entry]
Name=Eclipse
Type=Application
Exec=/opt/eclipse/eclipse
Terminal=false
Icon=/opt/eclipse/icon.xpm
Comment=Integrated Development Environment
NoDisplay=false
Categories=Development;IDE
Name[en]=eclipse.desktop
Now we are all set.

Install Hadoop 1.1.2 on Ubuntu 12.04

Here is the example of installation of Hadoop on a single machine, UBuntu box in this case. For a simple cluster installation, please reference to Running Hadoop on Clusters of Two Nodes using Ubuntu and CentOS.
1. install hadoop 1.1 download Hadoop-1.1.2 (STABLE VERSION OF THE HADOOP)
http://archive.apache.org/dist/hadoop/core/hadoop-1.1.2/hadoop-1.1.2.tar.gz
1.2 untar the package to /opt 1.3 find the JAVA_HOME and HADOOP_HOME directory and add them to .bashrc file
echo export JAVA_HOME = /usr/lib/jvm/java-6-openjdk >> ~/.bashrc
echo export HADOOP_PREFIX = /opt/Hadoop-1.1.2 >> ~/.bashrc
1.4 add $HADOOP_PREFIX/bin to your $PATH variable
export PATH=$PATH:$HADOOP_PREFIX/bin
export PATH=$PATH:$HADOOP_PREFIX/sbin
2. Decide the running mode of your Hadoop:
local mode
standard/local alone mode
Pseudo-Disributed mode
2.1 If you want to start Hadoop in Pseudo-Distributed mode, edit the following three configuration files at $HADOOP_PREFIX/conf, core-site.xml, hdfs-site.xml and mapred-site.xml
#core-site.xml
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
<configuration>
<property>
<name>hadoop.tmp.dir</name>
<value>/home/username/projects/hdfs_test</value>
</property>
<property>
<name>fs.default.name</name>
<value>hdfs://localhost:54310</value>
</property>
</configuration>
#hdfs-site.xml
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
<configuration>
<property>
<name>dfs.replication</name>
<value>1</value>
</property>
</configuration>
#mapred-site.xml
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
<configuration>
<property>
<name>mapred.job.tracker</name>
<value>localhost:54311</value>
</property>
<property>
<name>mapred.system.dir</name>
<value>/home/username/projects/mapred/system</value>
<final>true</final>
</property>
</configuration>
2.2 create file hadoop-env.sh in $HADOOP_PREFIX/etc/, and write the following to the file:
vim $HADOOP_PREFIX/conf/hadoop-env.sh
export JAVA_HOME = /usr/lib/jvm/java-6-openjdk
3. Format the HDFS system
hadoop namenode -format
4. Start Hadoop
start-all.sh
5. usefule comamnd to check the HDFS system
hadoop fs -ls /: list the root of HDFS
hadoop fs -ls: (without '/'):
# if you could not get anything other than the following error msg:
#"ls: Cannot access .: No such file or directory.".
#You need to do the following steps to make it work.
hadoop fs -mkdir /user
hadoop fs -mkdir /user/username
# Now you should be able to run hadoop fs -ls because by default hadoop is looking for
# "/user/username" structure within HDFS. The error msg is means there is no such structure in HDFS.
#To avoide the error msg we need to create the structure within HDFS for Hadoop.
6. copy files from local "linux" to HDFS I used two text files for testing: file1.txt and file2.txt, they are located at /home/username/projects/hadoop 6.1 copy three files from local to hadoop file system
hadoop dfs -copyFromLocal /home/username/projects/hadoop /user/username
# check copy result
hadoop dfs -ls /user/username/
6.2 download the word-count example from http://repo1.maven.org/maven2/org/apache/hadoop/hadoop-examples/1.0.3/hadoop-examples-1.0.3.jar and put it /home/username/projects/hadoop 6.3 run the mapreduce job
hadoop jar  hadoop-examples-1.0.3.jar wordcount /user/username /user/username-output
# notes for this command:
#(1) if  you see io exception, you might use full path for the jar package
#(2) path "/user/username-output" is the output path for mapreduce, it has not been there before running the job.
6.4 Retrieve the job result from HDFS
# 1. merge mapreduce outputs and copy to local path: /home/username/projects/hadoop/output
hadoop dfs -getmerge /user/username-output /home/username/projects/hadoop/output
7. Hadoop Web Interfaces
NameNode daemon: http://localhost:50070/
JobTracker daemon: http://localhost:50030/
TaskTracker daemon: http://localhost:50060/
7. Questions
Q. $HADOOP_HOME is depreciated always showing up when you run hadoop commands.
A. Replace HADOOP_HOME in your ~/.bashrc file with HADOOP_PREFIX. Check whether $HADOOP_HOME is defined in other places using echo command when you open new terminal.

Use Apache Virtual Host/ Reverse Proxy on Ubuntu

1. install apache2 on Ubuntu
sudo apt-get install apache2
You may notice that http.conf file located at /etc/apach2/ is emplty, which is normal since we are using apache2.conf instead 2. Create symbolic links to enable the proxy modula in Apache2, then restart the server
sudo ln -s /etc/apache2/mods-available/proxy.load /etc/apache2/mods-enabled
sudo ln -s /etc/apache2/mods-available/proxy_http.load /etc/apache2/mods-enabled
sudo /etc/init.d/apache2 restart
3. Create a virtual host file:
sudo gedit /etc/apache2/sites-enabled/proxiedhosts
# and edit the file so that it resembles:
NameVirtualHost *:80  # do not forget this line of code.
<VirtualHost *:80>
ServerName example.com
ProxyRequests off
ProxyPass / http://localhost:8080/
ProxyPassReverse / http://localhost:8080/
</VirtualHost>
It is obviously we are forwarding any request on port 80 to port 8080 4.Activate the virtual host file by making a symbolic link to the Apache2 sites-enabled folder then restarting Apache2:
sudo ln -s /etc/apache2/sites-enabled/proxiedhosts /etc/apache2/sites-enabled
sudo /etc/init.d/apache2 restart

Print/Count Unique Words in a File Using Command Line

Here is a one-line-command used to print/count unique words in a file:
# Print
cat file_name | tr " " "\n"| sort | uniq
# Count
cat file_name | tr " " "\n" | sort | uniq -c
# tr " " "\n" used here to replace space with newline, what it does is to put every word separated by space into a line

sed and awk

Use sed and awk you could print certain lines that meet your query condition.
Here is a command I used to print book titles of my son's reading history from a text file. From the picture, we can tell that book titles are mixed with DVD titles and the author names are listed after titles if there is any.


My task is to pick up titles of books only. Here sed and awk comes handy.
sed -n '/TITLE/p' history.txt | sed -n '/DVD/!p' | awk '{FS="/"; print $1 }' | awk '{FS="        "; print $2}'
By default sed will print anything from the original file.
-n '/TITLE/p' will let sed print only lines contain 'TITLE'.
-n '/DVD/!p' will further suppress lines contains 'DVD'
awk '{FS="/"; print $1 } will remove the author information from title
Further investigate reveal that there are 8 spaces between TITLE and the title of the book,
awk '{FS=" "; print $2}' will print out only the title of the book.

Show Time Stamp on Bash History

Ever wondered when did I execute the command? You could add a time stamp in front of each command you executed.
echo 'export HISTTIMEFORMAT=" %F %T "' >> ~/.bashrc
source ~/.bashrc
You will see time stamps on your commands when you execute history next time.
The result will look like:
1013 2013-05-09 14:47:44 vim scripts_for_Distance_Matrix.txt
1014 2013-05-09 14:48:01 R < scripts_for_Distance_Matrix.txt --no-save

Enable EPEL Repository on CentOS

Extra Packages for Enterprise Linux (or EPEL) provide much more current versions of popular applications like PHP or MYSQL. Before starting you need to check check your current repositories and verify that you have not enabled EPEL
yum repolist
If you do not see EPEL in the output, then you have not enabled it yet. https://fedoraproject.org/wiki/EPEL 1. download the following package from fedoraproject.org
# The following link is for CentOS 5
wget http://dl.fedoraproject.org/pub/epel/5/x86_64/epel-release-5-4.noarch.rpm
# check for and report potential conflicts
rpm -ivh epel-release-5-4.noarch.rpm --test
During test you might see the following NONKEY warning msg: "warning: epel-release-5-4.noarch.rpm: Header V3 DSA signature: NOKEY, key ID 217521f6"
Download the specific GPG key for EPEL repository from fedoraproject.org/keys by matching the key ID.
In my case I need key with ID 217521f6.
wget http://fedoraproject.org/static/217521F6.txt
mv 217521F6.txt /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL
sudo rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL
Verify that the key got installed
rpm -qa gpg*
Now run the test command again you should not see NOKEY msg:
sudo rpm -ivh epel-release-5-4.noarch.rpm --test
2. Enable the repository:
sudo rpm -ivh epel-release-5-4.noarch.rpm

SSH Public Key Authentication

Generating Keys 

To generate the public and private key files, you can use ssh-keygen command which is installed with OpenSSH.
ssh-keygen
By default, your public key file is located at ~/.ssh/id_rsa.pub and it can be shared with anyone. Your private key file is located at ~/.ssh/id_rsa and it must be kept secret.

Key Installation 

You now need to copy your public key file to the remote host(s) that you want to be able to use public key authentication.
1. Copy your public key to remote host1.example.net using one simple command:
ssh-copy-id -i ~/.ssh/id_rsa.pub username@host1.example.net
 
2. Manual Installation
To install the key manually you need to append the key to the ~/.ssh/authorized_keys file on each host you wish to use.
# copy the key to the remote host
scp ~/.ssh/id_rsa.pub user@remotehost:id_rsa.pub
# ssh into the remote host using your password
ssh user@remotehost
# append the public key
cat id_rsa.pub >> ~/.ssh/authorized_keys
rm id_rsa.pub
ON WINDOWS You can download puttygen.exe to generate private and public key pairs. Copy public keys generated to your remote host ~/.ssh/authorized_keys file and make sure they are in ONE LINE.

Obtain IP address of Linux

Two commands used to obtain ip address of linux machine:
#set 1
#displays all cuurentlly active network interfaces
/sbin/ifconfig
# IP address in the output of this command is you IP
/sbin/ifconfig eth0 | grep 'inet addr:'
#set 2
/sbin/ip addr list
/sbin/ip addr list eth0

Copy files between Linux Systems using scp

1. Copy file from a remote server to local server: copy directory pathogenbase from remote server /srv/www/ directory to current working directory

scp -r user_name@remote_ip:/srv/www/pathogenbase/ .
2. Copy file_name from local server to remote server: copy file_name from /srv/www on local server to remote server /srv/www/
scp /srv/www/file_name user_name@remote_ip:/srv/www/
3. If you are copying files between windows and Linux you could use winSCP.

Disbale SELinux on CentOS

Sometimes you want to disbale SELinux on your CentOS machine, here is how:
[sourcecode language="text"]
Edit /etc/sysconfig/selinux  and change SELINUX=disabled
[/sourcecode]

After you disable SELinux, you need reboot your system to make it take effect.

Install R develop version and RPy2 on CentOS 5.5

Prepare the environment

Before configure and compile R source code, I did the following updating of CentOS in order to support R capabilities by installing these packages:

[sourcecode language=""]
libtiff libtiff-devel
libpng libpng-devel
libjpeg libjpeg-devel
cairo cairo-devel
libX11 libX11-devel
libXt ibXt-devel
readline readline-devel
[/sourcecode]

and in order to produce the pdf version of the R-manual, I also installed all search results of

[sourcecode language="bash"]
yum search tetex except tetex-doc
[/sourcecode]

The RPy2 requires that you enabling R-shlib when you install R package. To do so you can build your own R package from source code.

1. donwload R source code: from http://cran.r-project.org/src/base/R-2/

[sourcecode language="bash"]
./configure --enable-R-shlib
[/sourcecode]

Here are my configuration results:

[sourcecode language=""]
R is now configured for x86_64-unknown-linux-gnu
Source directory:          .
Installation directory:    /usr/local
C compiler:                gcc -std=gnu99  -g -O2
Fortran 77 compiler:       gfortran  -g -O2
C++ compiler:              g++  -g -O2
Fortran 90/95 compiler:    gfortran -g -O2
Obj-C compiler:
Interfaces supported:      X11, tcltk
External libraries:        readline
Additional capabilities:   PNG, JPEG, TIFF, NLS, cairo
Options enabled:           shared R library, shared BLAS, R profiling, Java
Recommended packages:      yes
configure: WARNING: inconsolata.sty not found: PDF vignettes and package manuals will not be rendered optimally (as for this warning, specific fonts is not found.)
[/sourcecode]

 

2. Continue the following steps to complete installing R

[sourcecode language="bash"]
make
make check
make install
[/sourcecode]

some other errors you may encounter:

[sourcecode language=""]
configure: error: no acceptable C compiler found in $PATH  --install gcc gcc-c++
configure: error: No F77 compiler found --install compat-gcc-34-g77 gcc-gfortran
configure: error: --with-x=yes (default) and X11 headers/libs are not available  --install libXt-devel
[/sourcecode]

 

3. Install RPy2:

[sourcecode language="bash"]
/opt/python2.7/bin/easy_install rpy2
[/sourcecode]

You could use python2.7 if you have made a symbolic link in /usr/bin/python2.7 by

[sourcecode language="bash"]
ln -s /opt/python2.7/bin/python /usr/bin/python2.7
[/sourcecode]

** I had encountered a problem showing that lpython2.7 cannot be found, to solve this problem I created a symbolic link

[sourcecode language="bash"]
cd /usr/lib64
ln -s /opt/python2.7/lib/libpython2.7.so.1.0 libpython2.7.so.1.0
ln -s libpython2.7.so.1.0 libpython2.7.so
[/sourcecode]

If you have more than one R package installed on your system you may encounter some errors, like "rpy2.rinterface.baseenv.get("show") show not found" when you try to use the rpy2 package.

solution:
configure the  /etc/ld.so.conf.d/R-x86_64.conf file, create the file if the file does not exist Put the path of the R package whic you used to build your rpy2 into this file. Mine is /usr/local/lib64/R/lib.

Run /sbin/ldconfig
You may solve the problem by now.

3. install libraries in R

[sourcecode language="bash"]
sudo  R
install.packages('package_name')
[/sourcecode]

pay attention to the location of the installation of new packages "/usr/local/lib64/R/library/e1071/libs"
Sometimes when you install a package and it requires at least certain version of the R, you could download the source code of the package and do the following installation:

[sourcecode language="bash"]
install.packages("/path/to/the/packages/source/file", repos=NULL, type="source")
[/sourcecode]

Linux System Admin Tool Box

1. See user login history

$ last: listing most recent logins by users.


$ last -a list the IP or name of the system from where the user logged in

2. Vied the currently logged in user

$ who: list logged in users and their process


$ users: only list the logged in users

3. Running procssses of logged in user

$ w : who is logged in and what they are doing now.

4. Others

$ uptime: tell how long the system has been running

$ id: display user and all group ids


$ free

$ du

$ df

$ env

$ ps

$ top

Use Curl to batch download files from a website

In this example, we suppose all the files are in the same folder on the website and the file names have the same pattern:

$ curl -L -O http://www.example site.com/color/pic/fire[1-5].gif

-L : Follows all redirections until the final destination page is found. This switch is almost always required as curl won’t follow redirects by default (read about how to check the server http headers with curl).
-O : By using this switch you instruct curl to save the downloaded data to a file in the current directory. The filename of the remote file is used. This switch is also required or else curl will print the data to stdout, which is something you won’t probably like.


wget all files of the same type

To get all the pdf files:this command does not work on all websites.

$ wget -r -l1 –no-parent -A.pdf  <url>

How to use rsync for transferring files under Linux or UNIX

rsync command common options

  • --delete : delete files that don't exist on sender (system)
  • -v : Verbose (try -vv for more detailed information)
  • -e "ssh options" : specify the ssh as remote shell
  • -a : archive mode
  • -r : recurse into directories
  • -z : compress file data

Task : Copy file from a remote server to a local computer

Copy file /home/tommy/webroot.txt from a remote server openbsd.nixcraft.in to a local computer /tmp directory:
$ rsync -v -e ssh tommy@openbsd.nixcraft.in:~/webroot.txt /tmp
Password


Task: Synchronize a local directory with a remote directory

$ rsync -r -a -v -e "ssh -l jerry" --delete openbsd.nixcraft.in:/webroot/ /local/webroot


Use wget to repeat download the similar items from a site

1.This command will repeat the wget command for each number enumerated in the { }

wget -nd http://www.cracked.com/blog/wp-content/uploads/2009/12/zorklon{1,2,3,4,5}.jpg

2. use -i option. You can put all the links in one txt file and use -i option to download them one by one.

wget -i fedoraisos.txt

3. Some other options

-c to resume an interrupted download session

--user=user_name

--password=password

--user-agent=agent-string: used when a site denies access to non-browser user agents.

Cannot print a pdf file?

Because you forget your password?

Try this command:

$ gs -q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile=output_file.pdf -c .setpdfwrite -f source.pdf

If you want to combine more than one pdf file into one pdf file:

$ gs -q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile=new_3000.pdf -c .setpdfwrite -f /mnt/ncsfile01/3000/*.pdf

Datatable static image not found on the server

When you use ```datatables.min.css``` and ```datatables.min.js``` locally, instead of datatables CDN, you may have encountered that ```sort...