Your right place for "Right to Know"

Systemctl for managing Systemd

Systemd is the newer linux init/system manager tool which is actually a replacement over tradition SysV init system. Systemd can be controlled by systemctl commandline tool. In this article, I would show some of the use/examples of systemctl command.

systemctl list-units


useful options/flags: [–all] [-state=inactive] [–type=service #target: to show available targets]

Viewing unit files: systemctl list-unit-files

root@gopal-virtual-machine:~# systemctl list-unit-files –state=enabled
UNIT FILE                                        STATE
acpid.path                                       enabled
cups.path                                        enabled
accounts-daemon.service            enabled

systemctl cat/show <servicename>.service #cat for showing actual configuration for the service, show for low level properties of unit. In case of show, we can add -p <property name> to show only that property and its config value

root@gopal-virtual-machine:~# systemctl cat rsync.service
# /lib/systemd/system/rsync.service
Description=fast remote file copy program daemon

ExecStart=/usr/bin/rsync –daemon –no-detach

root@gopal-virtual-machine:~# systemctl show rsync.service
TimeoutStartUSec=1min 30s

systemctl edit [–full] httpd.service
systecmtl list-dependencies httpd.service #eg

 systemctl start/stop httpd.service
 systemctl enable/disable httpd.service
 systemctl mask/unmak httpd.service #Mask service wont be started unless unmasked
 systemctl is-active/is-enabled/is-failed httpd.service
 systemctl reload-or-restart sshd.service
 systemctl status httpd.service #Service status, CGroup hierarchy and initial few logs related to service

systemctl poweroff/reboot/halt/rescue

Python Django: Using multiple database, inspectdb and admin for existing database

One of the most powerful parts of Django is the automatic admin interface. It allows all users including developers to manipulated data inside database table. Often people use django admin tool to manage their database rather than using the console management tool provided by the database software itself or any other tool which are very limited and specific to one type of database only.

I have an existing setup mysql database with all the required schema and stored data. I want to use Django’s powerful administration tool to manage some of the stuffs on the database.

Lets get started on.

I have setup a django project called boad with necessary configurations updated in the file. I have added my mysql server as default database for the project.

Since running migration using django management command would create default django tables like django_sessions, django_migrations etc on my database which I dont like as I would like to keep my database very neat and clean as it was prior to django setup. So I decided to use django multiple database feature using database router that would route database operations of all other django related table to other database. I have setup a database router which will route read,write and syncdb operation of all tables not related to my existing database.

First in my I set up two databases as:

    'sqlite': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'HOST': 'localhost'
        'USER': 'root',
        'PASSWORD': '',
        'NAME': 'boad_database'

DATABASE_ROUTERS = ['boad.router.NonBoadAttributeRouter',] #Router's module path

I have used two database sqlite and mysql (default). I would be using sqlite to store data that are specific to django tables.

I created a file in the project module directory as:

class NonBoadAttributeRouter:

    non_boad_attribute_tables = ['auth', 'admin', 'contenttypes', 'sessions', 'messages', 'staticfiles', 'migrations']
    def db_for_read(self, model, **hints):
        if model._meta.app_label in self.non_boad_attribute_tables:
            return 'sqlite'
        return None

    def db_for_write(self, model, **hints):
        if model._meta.app_label in self.non_boad_attribute_tables:
            return 'sqlite'
        return None

    def allow_relation(self, obj1, obj2, **hints):
        if obj1._meta.app_label in self.non_boad_attribute_tables or obj1._meta.app_label in self.non_boad_attribute_tables:
            return True
        return None

    def allow_migrate(self, db, app_label, model_name=None, **hints):
        if app_label in self.non_boad_attribute_tables:
            return db=='sqlite'
        return None

Here I have stated that if application label is either of labels defined in non_boad_attribute_tables python variable, they should operate by “sqlite” database. Each router’s function would check for app label and route to “sqlite” database as per required or would return “None” which would tell django to fall back to “default” database for database operation.

Django have an “inspectdb” management command which is very useful to generate Django model from the existing database. As an example here I have use inspectdb to generate model for my course_category table in mysql.

venv) D:\Learnings\Django\boad\src>python inspectdb course_category
# This is an auto-generated Django model module.
# You'll have to do the following manually to clean this up:
# * Rearrange models' order
# * Make sure each model has one field with primary_key=True
# * Make sure each ForeignKey has `on_delete` set to the desired behavior.
# * Remove `managed = False` lines if you wish to allow Django to create, modify, and delete the table
# Feel free to rename the models, but don't rename db_table values or field names.
from __future__ import unicode_literals

from django.db import models

class CourseCategory(models.Model):
 course_cat_id = models.AutoField(primary_key=True)
 coursename = models.CharField(max_length=100)
 code = models.CharField(max_length=10, blank=True, null=True)
 project = models.CharField(max_length=30, blank=True, null=True)
 created_by = models.CharField(max_length=60)
 created_date = models.DateTimeField()
 deleted_by = models.CharField(max_length=60)
 deleted_date = models.DateTimeField(blank=True, null=True)
 deleted = models.IntegerField()

class Meta:
 managed = False
 db_table = 'course_category'

I have generated an model from existing database. I can do same for other mysql tables and create put it inside their own respective django app created using

python startapp courses


Now finally in file of each django app folder, I have register each model in admin as:

# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.contrib import admin

from course.models import CourseCategory

class CourseCategoryAdmin(admin.ModelAdmin):
    list_display = ['course_cat_id', 'coursename', 'code', 'project']
    list_filter = ['project',]
    list_editable = ['code',], CourseCategoryAdmin)


Running django’s migration command wont create any other tables in my mysql database but instead will be created in other sqlite database.

After successful migration I can test my django app using:

python runserver

course_admin_interfaceI can now successfully setup admin interface for managing my mysql database.

This approach of creating admin interface is applicable to most of popular database systems like Oracle, postgres etc. Django is going more popular day by day and equally getting more recognition for its quick and powerful admin interface generation.

Dumping(backing) and restoring database [PostgreSQL, MongoDB, MySQL]

A database backup and recovery plan is creating backup copies of data. When error (internal exception within the DBMS, Application (or transaction) failures, damage to disk storage devices, file system failures, tape degradation or damage, and deleted data files) occur that damages the integrity of the database, a backup copy of the data can be used as the basis to recover or restore the database. When problems impact the database, the DBA can use the copy backups and the database log to recover the database. Whatever the cause of the problem, the DBA must be able to recover data quickly so that the business can continue to operate. In this article, I will teach how simply we can backup and restore any particular database (PostgreSQL, MongoDB, MySQL)

PostgreSQL database backup and restore

pg_dump is a utility for backing up a PostgreSQL database. pg_dump can be used to backup an database or any specific table, then pg_restore can be used select which parts of the database are to be restored.

Scenario: I have a postgres database instance running at server with one of the database named as “my_dbname”.

We need to store postgres server password in PGPASSWORD variable of the console

PGPASSWORD=my_pass (In Linux)

set PGPASSWORD=my_pass (In Windows)

pg_dump -h -U my_username -Fc my_dbname > pg_my_dbname_backup

Make sure that the postgres binary pg_dump is in your environment variable. If not, you need to change your current directory to the postgres binary folder.

-Fc option denotes that we are using custom dump format. We can use following format according to the need

  1. -Fp for plain format
  2. -Fd for directory format
  3. -Ft for tar format

Also we can use -j <no_of_jobs> for parallel jobs executing which could be useful if dumping requires use executing time.

Instead of output redirection ” > pg_my_dbname_backup” we can specify dumped filename using -f option as

-f pg_my_dbname_backup

pg_dump also allows dumping of specific table using -t option as

-t my_tablename

For full options or full documentation on using pg_dump I recommend to go through official postgres pg_dump documentation page.

If you want to create a routine backup script this wiki has some tips on it which provides a demo script for windows user as:

@echo off
for /f “tokens=1-4 delims=/ ” %%i in (“%date%”) do (
set dow=%%i
set month=%%j
set day=%%k
set year=%%l
set datestr=%month%_%day%_%year%
echo datestr is %datestr%

set BACKUP_FILE=<NameOfTheFile>_%datestr%.backup
echo backup file name is %BACKUP_FILE%
echo on
pg_dump -h <HostName> -p 5432 -U <UserName> -F c -b -v -f %BACKUP_FILE% <DATABASENAME>

Let’s say we have some error or problem in our database “my_dbname” and we need to restore it back from our backup file.

First we need to delete the existing erroneous database and create a fresh empty database of same name.

dropdb -h -U my_username my_dbname

createdb -h -U my_username my_dbname

Now we got a fresh empty “my_dbname” database where we can restore our backup into it

pg_restore -h -U my_username -d my_dbname pg_my_dbname_backup

we need to make sure that “pg_my_dbname_backup” is in current directory or could be replace with the path of the database file.

pg_restore can be used with option -C which could allow creation of the database also before restoring. i.e we can use option -C which would eliminate the use of createdb command before pg_restore

MongoDB database backup and restore

mongodump is a utility for creating a binary export of the contents of a mongo database. mongodump can export data from either mongod or mongos instances. mongodump captures the documents in the database in its backup data

I can create a backup directory called “mongodb_backup” using mongodump utility as

mongodump –host –out mongodb_backup

We can add

–db database_name to backup only database_name database

–collection collection_name to backup only collection_name collection

–username my_username –password my_password if need authentication for instance connection

–excludeCollection Collection_name to exclude any specific collection to be dumped

–archive my_mongo_dump.archive for dump in archive format

–gzip to dump in gzip format rather than directory

More on mongodump can be found in official documentation

Same way we can restore of dumped (backed up) database into running mongo instance using mongorestore as

mongorestore mongodb_backup

mongorestore can be used with same options as provided above for mongodump

More on mongorestore can be found in official documentation

MySQL database backup and restore

MySQL too provides a tool mysqldump to dump/backup any database

mysqldump -h -u my_username -p my_password my_dbname > mysql_backup.sql

If we need a gzip format we can pipe it to our gzip command before output redirection as

mysqldump -h -u my_username -p my_password my_dbname | gzip -9 > mysql_backup.sql.gzip

For restoring mysql database we need to make sure that restoring database exist and is empty.

mysql -u my_username -p my_password my_dbname < mysql_backup.sql

mysqlimport can also be used to import dumped mysql file as

mysqlimport -u my_username -p my_password my_dbname mysql_backup.sql

If we have a gzip format of dumped file we need to extract it before restore as

gunzip < mysql_backup.sql.gzip | mysql -u my_username -p my_password my_dbname

PowerShell is a task automation and configuration management framework from Microsoft, consisting of a command-line shell and associated scripting language. PowerShell is a very powerful tool for automating various windows tasks. Here we will learn to use powerShell as a commandline tool for creating archives (zip) files from folder containing files.

I am using windows 10 pc which comes with powershell version 5.1

I have a folder structure as follows.

I have a folder called Software Documentations with contains 7 files.

Using the explorer address bar, we can start a new powershell session using this current directory.

Since powershell leverages use of .net classes, we will use “System.IO.Compression.FileSystem” class and load it in powershell session as

Add-type -A System.IO.Compression.FileSystem

We will create instance of this class in a powershell variable called $cls

$cls = [IO.Compression.ZipFile]

Now lets see what static functions are provided by our ZipFile class

$cls | Get-Member -static

We can see that we have methods “CreateFromDirectory” and “ExtractToDirectory” which we are interested on.

Now lets begin creating zip file using method CreateFromDirectory

First we need to make sure we are in correct directory


Lets save the directory path of our desired folder to be zipped (which is “3. Software Requirement Specification” in my case”) into powershell variable $source

$source = (gi '3. Software Requirement Specification').FullName

Make sure $source has some value in it


Now lets use our static “CreateFromDirectory” method to convert this folder to a zip file called “”

$cls::CreateFromDirectory($source, "")

Yes, we verified we have a our zip file “” in the same directory.

Now lets demonstrate extracting same zipped file to a new folder “myzip_extracted” folder using “ExtractToDirectory” method provided by our $cls

$cls::ExtractToDirectory('', 'myzip_extracted')

And we can see that it worked and created myzip_extracted folder and put all my files from the zipped archive to this folder.

Deploying Django App using gunicorn and nginx on Linux (Ubuntu) server

According to official site,

Django is a free and open-source web framework, written in Python, which follows the model-view-template architectural pattern.

Nginx is a web server which can also be used as a reverse proxy, load balancer and HTTP cache.

We are going to use above technologies and deploy a sample Django app “dproject” with structure as

Djproject is the main Django project folder containing the import wsgi file, settings, urls. Djapp is my Django app and venv is python virtual environment folder where needed python packages and gunicorn is installed.

We can install gunicorn using pip as

pip install gunicorn

Now we will test if gunicorn can server our Django app or not

gunicorn –bind dproject.wsgi

If that works well we can proceed on further creating a gunicorn service to serve my app on nginx server

Now we’ll create a system service that can manage above gunicorn process.

Create a file for the gunicorn service as

vim /etc/systemd/system/dproject_gunicorn.service

Descriptioin = Gunicorn Daemon for dproject
After =

ExecStart=/home/gopal/dproject/venv/bin/gunicorn –access-logfile - --workers 3 –bind unix:/home/gopal/dproject/dproject.sock dproject.wsgi:application


Our service is dependent on We are using ‘gopal’ and ‘www-data’ as linux os user and group to serve our app. WorkingDirectory is pointed to our project directory.

ExecStart is a command line that will execute on service run. Here we have started our gunicorn process with 3 workers that will serve dproject through unix socket file.

We are also telling that we want our service to be managed by runlevel

Now we will allow our port 80 through firewall

ufw allow 80


firewall-cmd –permanent –add-port=80/tcp
firewall-cmd –reload

Now we will start and enable our gunicorn service so that our service is started automatically after every boot

systemctl start dproject_gunicorn
systemctl enable dproject_gunicorn

Now we’ll create nginx configuration file for our Django app which is served through unix socket file dproject.sock via gunicorn

vim /etc/nginx/sites-available/dproject

server {
  listen: 80;
  location /static/ {
    root /home/gopal/dproject;
  location / {
    include proxy_params;
    proxy_pass http://unix:/home/gopal/dproject/dproject.sock;

We will enable this configuration file by creating a symlink to site-enabled directory of nginx configuration directory

ln –s /etc/nginx/sites-available/dproject /etc/nginx/sites-enabled/dproject

We will now test if our configuration is ok

nginx –t

It says “OK”

Now we will restart out nginx server and allow nginx ports in our firewall

systemctl restart nginx
ufw allow “Nginx Full”


Python defaultdict() versus dict.get()

Python is a great language. Dict is one of data structure available in python which allows data to store in the form of key/value pair. Many times we might encounter a situation where we have to retrieve a value using the dictionary key. When our key is not found in the dictionary it will throw an exception of KeyError.


The solution to this is using defaultdict or dict.get() to return a default value if the key is not found in the dictionary.

Lets dig into both of these

defaultdict can be found in collections module of python. So, in order to use it, we have to import it first as:

from collections import defaultdict
mydict = defaultdict(int)

defaultdict constructor takes default_factory as argument which is a callable. This can be

int : default will be integer value of 0

str : default will be empty string ”

list : default will be empty list []

and so on.

If we want our own default value then we can pass function pointer.

Lets say that we want default value to string ‘default’. We can achieve this in defaultdict as

def mydefault():
        return 'default'

mydict = defaultdict(mydefault)
print mydict['test']

will output ‘default

The same result can also be achieved using the dict.get method as

mydict = {}

will output ‘default

Thus same result can be achieved using both approach. dict.get have to provide a default value every time it is called whereas using defaultdict we cant setup a default value only one time.

Now lets check the efficiency of both of them in term of execution time.

For this we will be using ipython notebook since it has got %timeit command to measure the execution of any python statements over any desired looping of same statements.

In Ipython notebook environment, let us create two function that implements defaultdict in one and dict.get method in another.


Here we have implemented a() function to execute defaultdict value retrieving and b() to use dict.get method. Now lets calculate the execution time of both function in 100 loops as



First executing a() function over 100 loops we can see that it took over 8.88 micro sec per loop using the best of three.


Here when we run b() function over 100 loops we can see that it tooks 19.3 micro sec per loop using the best of 3 approach.

Hence defaultdict seems more efficient over dict.get method.

Lets rerun this test again but this time will be using default %timeit command of notebook which will be looping the statement for default 1000 loops


Clearly here also defaultdict seems more efficient that dict.get method and the experiment shows that defaultdict more that two times faster than dict.get method.

Hunting for processes holding network port

Many times, we may encounter certain errors within the application showing some strange errors with network issues. Like XAMPP showing error when apache is started complaining that the port is already in use. In such case, we need to find which service/program is using the port and reconfigure the application/service as per the requirement.

Today I will be showing how to hunt for such process using the powershell in windows system step by step. Lets hunt for process holding the network port 80.

  • First start run and type in powershell to start powershell terminal1. Powershell in run
  • Run this command

start powershell -verb runas

2. Start powershell in elevated mode

  • to start another powershell terminal in elevated [administrator privileged mode]
  • Run command

netstat -aonb

3. Netstat command

  • to view all the listening / established / connected ports with details
  • Since this is a long list, looking for specific port might be difficult, so we cant search this list output by using command

    netstat -aonb | findstr :80

5. Grep 80 port from netstat

  • It shows that the process with PID 4088 is holding that port.
  • Now lets find the process with that PID ie 4088. To do this, we can any of two commands as

tasklist | findstr 4088


Get-Process -PID 4088

6. Grep process with pid

  • Voila, we have hunted the process we are looking for. Now we know that port 80 is used by process httpd.exe.

Installing Webmin tool on CentOS 7

Webmin is a free system administrator’s tool which gives the admin the direct configuration of the linux system through web. Thus eliminating the hard-to-remember linux commands. Actually this tool can be used for quick modifications on the linux system like setup user accounts, Apache, DNS, file sharing and much more. If you don’t know the exact commands to configure any aspects of the linux command line system, webmin is your tool.

Installation procedures:

  1. Download the webmin rpm through wget
  2. wget
  3. installed required dependencies
  4. yum –y install perl perl-Net-SSLeay openssl perl-IO-Tty
  5. install web from the rpm package
  6. rpm –U webmin-*.rpm

That’ it. It now installed in your system. Now browser https://localhost:10000

If in case of SSL related problem and want to disable it, we can change it in configuration

vim /etc/webmin/miniserv.conf

and change the line containing




and open the link  http://localhost:10000 again and login with your root account

Using find command in Unix and Linux systems

“Find” command searches all the files and directories inside directory that matches the given criteria like name, size, ownership, permission etc.

find <path> [option/criteria] <argument>
option can be:
-atime <+N/-N/N> :- Access time based search
-mtime <+N/-N/N> :- Modification time based search
-name <Name> :- Name based search
-size <+N/-N/N> :- Size based search
-user <owner> :- owner based seach
-group <group> :- Group based search
-perm <permission> :- Permission based search
-type <type> :- File type based search
type can be ‘f’ or ‘d’ denoting searches for files only or directory only respectively

1> Find all the files inside /home/test directory that have been modified within 7 days

2> Find all the files with ‘.conf’ filename extension inside the /etc directory

3> Find all the empty files (0 length files) inside the current directory

4> Find all the files and directories inside the whole system that have the permission ‘770‘ and t hat belongs to the user ‘test’

5> Find all the directories inside the /var/log directory

6> Find all the files in /etc that have length
a) Exactly 100KB

b) Within 100KB

c) Greater than 100KB

d) That have 100 characters in it.


Furthermore, three logical characters can be used to create more filtered search expression for the find query command.
7> Find all the empty files inside the current directory and remove them all

8> Find all the configuration files with .conf filename extension inside the /etc directory and copy them into the /home/test/backup directory

9> Find all the readonly files
10>Find all the directories with 770 permission and chmod to 755
11> Find all empty directories
12> Find all the empty file in current directly only and delete each one showing a prompt
13> Find all the files or folder in current directory and copy all the files to /backup directory preserving the permissions, times and ownership of every file and subdirectory

Blog at

Up ↑