Showing posts with label redhat. Show all posts
Showing posts with label redhat. Show all posts

Tuesday, 26 March 2019

How to run python applications as system service in CentOS 7 or RHEL 7 ?

I started developing python applications from 2012. During my learning period, I was using screen in linux for running my python applications in the background. Majority of my colleagues were also following the same approach for running applications in background. The advantage of this approach was its easiness. But these screens will go away if the system gets rebooted. Also these applications will never get started on boot.

Systemd is a system and service manager for CentOS 7 and RHEL 7. This the first process that gets started after the system boots. This is compatible with the SysV init scripts used by CentOS 6 and RHEL 6.

It is very easy to run any custom application as a system service. In this way, the management will become very easy.

Suppose I have a python flask application app.py and I want to run it as a system service. I want to run this application under my unix user account amal.  

The contents of app.py is given below.




My code is located in the location /home/amal. Usually in case of any production deployment, the code will not be kept in the user home directory. This is to explain the scenario of running the application as a non root user.

The command to run my application is


python app.py 

For running in production mode with a WSGI like gunicorn, the command will be
gunicorn app:app --bind 0.0.0.0:8080 -w 2

For running this application as system service. We just need to do the following steps.

Create a file myapp.service in the location /etc/systemd/system/ The content of myapp.service is given below.


Now save the file and execute the following command. The following command will reload all the systemd configurations.
systemctl daemon-reload

Now we can start the application with the following command
systemctl start myapp

Now check the status of the application with the following command
systemctl status myapp

Also we can invoke the webservice using curl command.
curl -X GET http://localhost:8080/


Monday, 25 March 2019

What is Inode in Linux ? How to check the Inode usage in Linux ?

The inode is a data structure in a Unix-style file system that describes a file-system object such as a file or a directory. Each inode stores the attributes and disk block location(s) of the object's data. File-system object attributes may include metadata (times of last change, access, modification), as well as owner and permission data. Inode is also known as Index node.

Directories are lists of names assigned to inodes. A directory contains an entry for itself, its parent, and each of its children. Sometimes, we may face space issue in disks which has storage space. Mostly this issue occurs because of inode filling. The file system might have storage space available, but the inode table does not have space.

The command to check the storage space is given below.

df -h

The command to check the inode usage is given below.

df -i

If the inode capacity of a disk is full, we will not be able to write any data to the disk. Usually this issue happens when we keep large number of small files.

Tuesday, 22 August 2017

How to list all the local users in a linux system ??

The following command can be used to list all the local users in linux.

cut -d: -f1 /etc/passwd

This will list all the local users in the system. This will not list all the users if it is integrated with LDAP or any other similar systems

Wednesday, 31 December 2014

Linux commands to get IP address and Hostname of a machine

Knowing the IP address and Host name of a machine are some very common requirements.

The command to get the IP address of a linux machine is
>ifconfig
eth0      Link encap:Ethernet  HWaddr 0A:90:74:4A:62:42
          inet addr:172.31.9.245  Bcast:172.31.15.255  Mask:255.255.240.0
          inet6 addr: fe80::890:74ff:fe4a:6242/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:9001  Metric:1
          RX packets:13139146 errors:0 dropped:0 overruns:0 frame:0
          TX packets:12665241 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:10433927585 (9.7 GiB)  TX bytes:16516355937 (15.3 GiB)
          Interrupt:77

lo        Link encap:Local Loopback
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:40162017 errors:0 dropped:0 overruns:0 frame:0
          TX packets:40162017 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0

          RX bytes:6184024837 (5.7 GiB)  TX bytes:6184024837 (5.7 GiB)

The command to get the hostname of a linux machine is
>hostname
masternodemachine

Wednesday, 19 November 2014

Various File Editors in Linux

When we say about linux, most of the people will think about a black command line. Operating with that black command line is not that much difficult as most of the people think. The basic operation that most of us did in the linux command line is creating a file or editing a file. This is required for all configuration files. For this we require a file editor. Here I am listing down some popular file editors in linux.

1) vi
2) vim
3) nano
4) gedit  (This is a desktop editor)
5) gvim
6) emacs

Setting Java home in Linux and Windows Environments

Java is a very popular programming language. Most of the people in the world are using java directly or indirectly. Most of the times we need to set JAVA_HOME environment variable. This is a very basic activity. But just posting here, because it may help someone. When I started my career, I also searched in internet for the same.

Setting up JAVA_HOME in LINUX environments

1) First we have to install java. Java can be downloaded from oracle website. Based on your operating system        architecture( 32 or 64 bit), and requirement, download the proper java installable.

2) If it is a tarball, extract the tar ball and keep the folder in /opt directory
3) Go to the home folder of java and type pwd. Suppose the result is /opt/jdk1.7.0
4) Open the following file and add the entries as  below.

For ubuntu:
open /etc/bash.bashrc file or ~/.bashrc file

For CentOS and Redhat:
open /etc/bashrc or ~/.bashrc file

Add the following lines to the file

export JAVA_HOME=<path to java home directory>
export PATH=$JAVA_HOME/bin:$PATH

Save the file and exit.
Then refresh the file using the command
source /etc/bashrc or source ~/.bashrc


Setting up JAVA_HOME in Windows environments

1) Download the JDK from Oracle website and install it in your machine
2) Click on the start button
3) Right click on My Computer
4) Click on Advanced System settings
5) Click on Environment variables
6) Depending on the use, click on New in the System Variables or User Variables. The scope of user variables is limited to the particular user account, where as of the System Variables will be accessible to all the users.
7) Click on New and Give variable Name as JAVA_HOME and variable value as the complete path to java installation folder in your windows machine.
      Eg: C:\Program Files\Java\jdk1.7.0_60
8) Edit the Path and append the following entry to the end.
     %JAVA_HOME%/bin;
      Don't forget to put  a semicolon delimiter between the existing values and newly added value.

Now your JAVA_HOME is set.. :)
     

How to check the memory utilization of cluster nodes in a Kubernetes Cluster ?

 The memory and CPU utilization of a Kubernetes cluster can be checked by using the following command. kubectl top nodes The above command...