System V init script(start-stop-daemon)

I’ve been looking for efficient ways to start at boot my NodeJS dependent applications, with inspiration from https://gist.github.com/alobato/1968852, I modified it to my own needs.

Link might interest you as well:
http://big-elephants.com/2013-01/writing-your-own-init-scripts/
https://www.cyberciti.biz/tips/linux-write-sys-v-init-script-to-start-stop-service.html

Copy template to /etc/init.d and rename it to something meaningful. Then edit the script and enter that name after Provides:(between ### BEGIN INIT INFO and ### END INIT INFO).

#!/bin/bash
# Inspired by https://gist.github.com/alobato/1968852
# Needs Provides, Descriptions

### BEGIN INIT INFO
# Provides:
# Required-Start: $all
# Required-Stop: $all
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description:
# Description:
### END INIT INFO

set -e
NAME=""
PIDFILE="/run/$NAME/$NAME.pid"
# Application one wants to upstart
DAEMON=""
DAEMON_OPTS=""
# Run as user
RUN_USER=""
RUN_GROUP=""
function daemon_run {
	mkdir -p /run/$NAME
	chown $RUN_USER:$RUN_GROUP /run/$NAME
	start-stop-daemon --start --background --quiet --chuid $RUN_USER:$RUN_GROUP --chdir /run/$NAME --pidfile $PIDFILE --make-pidfile --exec $DAEMON $DAEMON_OPTS
}
exec > /var/log/$NAME.log 2>&1

case "$1" in
start)
	echo -n "Starting $NAME ... "
	daemon_run
	echo "done."
;;

silent)
	echo -n "Starting $NAME in silent mode ... "
	daemon_run
	echo "done."
;;

stop)
	echo -n "Stopping $NAME ... "
	start-stop-daemon --stop --quiet --oknodo --pidfile $PIDFILE --remove-pidfile
	echo "done."
;;
restart|force-reload)
	echo -n "Restarting $NAME ... "
	start-stop-daemon --stop --quiet --oknodo --retry 30 --pidfile $PIDFILE --remove-pidfile
	alexa_run
	echo "done."
;;
*)
	echo "Usage: $0 {start|stop|restart}"
exit 1
esac
exit 0

When done,

sudo systemctl enable 'name_of_filename'
reboot

Mpho

Ubuntu media server WakeOnLan

I have always been conservative when it comes to saving electricity in my household not because I want to but because 1 kWh costs me R1.87, hence I had to think of a better solution rather than running my server 24 hours per day with an average load wattage of 532W, if one does the maths it costs me roughly R23.87 per day when always on.

So not cool, hence why I had to think of a better way to conserve energy:- WakeOnLan

In Ubuntu 16.04, make changes:

# Install ethtgool (Dependency)

sudo apt-get update && sudo apt-get -y upgrade && sudo apt-get install -y ethtool

# To avoid getting WOL disabled by TLP power management make changes to:-

sudo nano /etc/default/tlp

WOL_DISABLE=N

# To prevent powering off the network card during shutdown make changes to:-

sudo nano /etc/default/halt


NETDOWN=no

# Enable Wake on LAN when static network configuration is used.

sudo nano /etc/network/interfaces</code>

# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).
# The loopback network interface

auto lo
iface lo inet loopback
# The primary network interface

auto eth0
iface eth0 inet static
address 192.168.0.1
netmask 255.255.255.0
gateway 192.168.0.1
dns-nameservers 192.168.0.1
up ethtool -s eth0 wol g

Enable wake on lan in BIOS, enter the BIOS setup and look for something called “Wake up on PCI event”, “Wake up on LAN” or similar. Change it so that it is enabled. Save your settings and reboot.

From there you should be good to go.

Move files by date into different directory using CLI

A while back I was unfortunate in such a way that my HDD that I primarily use as photo archive started having mechanical issues, and before It died on my arms I had the opportunity to backup most of my pictures, however most of them were truncated and had to be discarded and the ones I could recover their filenames where overwritten with random filenames and ascii codes.

For over a while I have neglected my pictures and they just piled up in a single and nameless directory, until I took it against myself to clean them up.

And one of the benefits of using Linux OS is the scripting features.

for i in *; 
do d=$(exiftool $i | grep 'Date/Time Original' | cut -f 17 -d ' '); 
newd=`echo ${d//:/-}`; 
echo $i ${newd} ;
mkdir -p ${newd}; 
mv -- ${i} ${newd}; 
done
  1. write this script to a file named mvByDate.sh in your old directory.
  2. replace the /your/new/directory with your new directory’s path
  3. make this file executable with chmod +x mvByDate.sh
  4. then execute this file by ./mvByDate.sh
  5. voila

Install and configure NFS server on Ubuntu for serving files

When it comes to serving media content within your local network, a NFS share is way better than Samba/CIFS share in terms of speed and resource usage. If you have a XBMC box to watch TV shows and movies from a central server then a Network File System (NFS) share should be the choice for a media source. It only takes a few minutes to install and configure NFS server in Ubuntu, and it is a must have item on my server.

Setup NFS Server on Ubuntu

If you do not have NFS yet, you will have to install NFS server first using the following commands:

sudo apt-get install nfs-kernel-server

That’s the easiest part. It requires just bit more effort to configure NFS server to share files with right permissions.

Configure NFS Server

After installation you can configure NFS server by editing the /etc/exports file. First make a backup copy of the default NFS configuration file:

sudo cp -a /etc/exports /etc/exports.backup

Then open the /etc/exports file using the following command:

sudo nano /etc/exports

Edit and comment out all existing lines by adding a “#” in front the line. Next, setup NFS share for each folder you want to make available to the client devices as shown below.

Setup NFS Share for Media

One of the primary purposes I configure NFS server on Ubuntu server is to share my media to all my Xbian Raspberry Pi HTPC. As I said before, for sharing within a network the performance of NFS is better than SAMBA. If you a media folder on your central Ubuntu server, then add the following line to line to /etc/exports to setup NFS share for it.

#Export media to all IP address under 192.168.1.X
/home/user/media   192.168.1.0/24(rw,async,insecure,no_subtree_check,nohide)

This will make your media folder located under /home/user available to every device in your local network. The options within parenthesis are specifically chosen to improve performance. We are sharing “non-critical” media files and we are making them available only to devices in your home network (192.168.1.X). So we can afford to go lower on security to improve speed and performance.

rw – allows read/write if you want to be able to delete or rename files from XBMC or other devices. On XBMC you write permission to download subtitles.
async – Increases read/write performance. Only to be used for non-critical files.
insecure – Does not mean the files are insecure but rather this setting allow clients (eg. Mac OS X) to use non-reserved ports connect to a NFS server.
no_subtree_check – Improves speed and reliability by eliminating permission checks on parent directories.
nohide – This option allows the client device to see multiple file systems but only works on one host. Since this is rare, you may choose to omit this option as it can potentially cause problems in some cases.

Starting, Stopping, and Restarting NFS Server

Once you have setup NFS server, you can start NFS share using the following command:

sudo exportfs -ra

Any time you make changes to /etc/exports I recommend that you restart your NFS server using the following command:

sudo service nfs-kernel-server restart

On your XBMC just choose Network Filesystem (NFS) when you select the source. Note that NFS does not work on Windows clients but they should work on all Linux-based OSes that run XBMC. For Windows, you will still have to Setup Samba as the file server.

Setup NFS Client on Ubuntu

If you would like to connect to the NFS server on your local computer running Linux – prefferably Ubuntu, Do the following:

sudo apt-get install nfs-common

Mounts

Check to see if everything works
You should try and mount it now. The basic template you will use is:

sudo mount ServerIP:/folder/already/setup/to/be/shared /home/username/folder/in/your/local/computer

so for example:

sudo mount 192.168.1.42:/home/music /home/nerd/music

Mount at startup

Add the following lines in your fstab to mount NFS directories automatically after system reboot.

sudo nano /etc/fstab

This will mount directories on start up after the server reboots.

Server_IP:/home/music /home/nerd/music  /mnt/music   nfs   auto,noatime,nolock,bg,nfsvers=3,intr,tcp,actimeo=1800 0 0

Conclusion
NFS provides a quick and easy way to access remote systems over a network. However, the protocol itself is not encrypted. If you are using this in a production environment, consider routing NFS over SSH or a VPN connection to create a more secure experience.

How to install Gummi(LaTex) on ubuntu

How to install Gummi on ubuntu
This post is trivial since Gummi has recently released an PPA (Personal Package Archive) for Ubuntu.
btw, there are only 3 commands needed to get Gummi up and running:
1- First, add the PPA to your repository:
$sudo add-apt-repository ppa:gummi/gummi

2- Then, update your repository to reflect the new changes:
$sudo apt-get update

3- Everything is now ready to install Gummi:
$sudo apt-get install gummi

Installing Latex on Ubuntu

Latex is a document markup language for the Tex typesetting program. Due to its high quality typesetting capability, {\LaTeX}/TeX system is popularly used in academia and research communities to prepare professional looking documentations and research papers.

    To install/upgrade to TeX Live 2014:

    Open a terminal with Ctrl+Alt+T

    Add the texlive-backports PPA by typing the below (enter your password when prompted):

    $ sudo add-apt-repository ppa:texlive-backports/ppa 
    $ sudo apt-get update 
    

    Installation: If you are installing TeX Live for the first time,
    type:

     
    $ sudo apt-get install texlive
    $ sudo apt-get install --no-install-recommends texlive-latex-extra
    $ sudo apt-get install --no-install-recommends texlive-fonts-extra
    

    If you already have TeX Live installed and are upgrading, type: sudo apt-get upgrade Warning: this will also upgrade all other packages on your Ubuntu system for which upgrades are available. If you do not wish to do this,
    please use the previous:

    $ sudo apt-get install texlive-base