Enable alt for command line editing

When I first switched from CentOS to Ubuntu I noticed that Alt+f and Alt+b do not work in command line editing. Instead it opens terminal menu...
Here comes how to enable them
Open a terminal >> Go to Edit (on terminal menu) >> Keyboard Shortcuts >> Uncheck "Enable menu access keys (such as ....)"

Install Flash Player in Ubuntu

Can not watch youtube video in Ubuntu? Here is a solution:

sudo apt-get install flashplugin-installer

Enable print to pdf in Ubuntu

apt-get install cups-pdf

The printed pdf file will be located at ~/PDF/

Let Cron Send Email Only When Something Goes Wrong

Tired of getting too many emails from cron daemon? Here is a simple solution of letting cron send you an email when something goes wrong.
sudo crontab -e
Set "MAILTO=" on top of yout first command to be executed. And add ">/dev/null" to the end of each command. Here is my example:
#run emen2 local backup everyday at 1am and sync with production server everyday at 3am
MAILTO=your_email_address
0 1 * * * /home/user/cronjobs/emen2_backup.sh > /dev/null
0 3 * * * /home/user/cronjobs/syncserver.sh > /dev/null
Restart cront using command
sudo /etc/init.d/cron restart
Here is a very good example of explaing what /dev/null mean?

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.

Autostart application after CentOS reboots

If you only want to run a script after CentOS boots, you could put the command to rc.local file.
vim /etc/rc.d/rc.local
# type in your script at the bottom of this file
If you want to start and shutdown the program along with the system, you might need to create a service using chkconfig. To do so,

1.create a script file at /etc/init.d/
Here is an example of the content of the file
# create file( you need root privilege to do)
cd /etc/init.d/
vim emen2
and add the followings to the file
#!/bin/sh
#chkconfig: 235 80 05
#description: EMEN2 database server
#source function library
EMEN2_HOME=/srv/EMAN2
if [ ! -f $EMEN2_HOME/eman2.bashrc ]
then
  echo "EMEN2 startup: cannot start, missing eman2.bashrc"
  exit
fi
case "$1" in
  "start")
    echo -n "Start emen2 service"
    source $EMEN2_HOME/eman2.bashrc
    # To run it as root
    $EMEN2_HOME/Python/bin/emen2ctl start -h /srv/db_env -e default,site,em --port 8080
    # Or to run it as dedicated user
    /bin/su - username -c $EMEN2_HOME/Python/bin/emen2start
    echo "."
    ;;
  "stop")
    echo - n "Stop emen2 service"
    source $EMEN2_HOME/eman2.bashrc
    # To run it as root
    $EMEN2_HOME/Python/bin/emen2ctl stop -h /srv/db_env -e default,site,em --port 8080
    # Or to run it as dedicated user
    /bin/su - username -c $EMEN2_HOME/Python/bin/emen2stop
    echo "."
    ;;
  *)
    echo "Usage: /sbin/service emen2 {start|stop}"
    exit 1
esac
exit 0

**About the three values after #chkconfig
(1). The first value ’235′ states the default runlevels at which the service should be started. In this case, the service is configured to start at runlevel 2,3 & 5.
(2). The second value ’80′ states the start priority of the service. In this case the service shall be started after starting all those services having start priority less than 80.
(3). The third value ’05′ states the stop priority of the service. In this case the service shall be stopped after stopping all those services having stop priority less than 05.

**/bin/su -c can only accept simple command, which means there should be no "-" or "--" in the command.

2. make the file runnable:
chmod a+x emen2
 
3. add the newly created service to the start up list by
/sbin/chkconfig/emen2 on

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...