Monday, February 26, 2018

Installing Nodejs and NPM

Nodejs provides JavaScript with a way to optimise single threaded and single CPU core processing. 
NPM is a package manager to deliver applications. Its like yum on Centos linux.

In this case, I am installing Cordova for Android development on Centos 7.4 and it required nodejs along with npm. Hence, this article came along the way.

Step 1: Install Epel repository

$ sudo yum install epel-release  
$ sudo yum info nodejs
It shows nodejs version 6.12.3


Step 2: Install nodejs

Install the development environment then nodejs. This will install together the npm (version 3.10.10)
$ sudo yum --setopt=group_package_types=mandatory,default,optional groupinstall "Development Tools"
$ sudo yum install nodejs

Check installed version
$ node -v
$ npm -v

Step 3: Run Hello World

Create this script and name it as helloworld.js to test on a command line and the web browser.

var http = require('http');

http.createServer(function (req, res) {
    res.writeHead(200, {'Content-Type': 'text/html'});
    res.end('<p>This is <b>Hello World</b></p>');
}).listen(8080);
console.log('This example is my world!');


At a command line, type
$ node helloworld.js

This should display the output on the command line.
Next, open a web browser and type the following url

localhost:8080

Continue with Cordova installation from previous article.

Remove Defunct or Zombie processes

On Linux systems (Centos, Fedora, Ubuntu, Mint, Puppy), a running application is identified by one or more processes. Example when running a document with LibreOffice Writer, the process thats related is oosplash and soffice.bin as can be seen below.

tboxmy  5262  0.3  0.0 299672  3272 ?        Sl   10:17   0:00 /usr/lib64/libreoffice/program/oosplash --writer
tboxmy  5277  3.1  1.1 1273804 89564 ?       Sl   10:17   0:01 /usr/lib64/libreoffice/program/soffice.bin --writer --splash-pipe=5

Shown in bold, the unique process identification (id) are 5262 and 5277 for the two processes. Upon using the command ps xl, it can be seen that process 5277 is spawned by process 5262. Making 5262 a parent to 5277.


What is a defunct process?

A "defunct" processes is referred as a dead process that still hogs memory resources. Its not much a problem having a defunct process, but when the system starts to have more defunct processes, the system can slow down and affect applications.

This defunct or dead process that still lingers on the system is also known as zombie process. Most appropriate, like in a horror flick! You can't kill a zombie that is already dead, so what can you do? Watch the next zombie show and try figuring that out. Luckily, on Linux systems, a zombie doesn't go around attacking or destroying resources. It only lingers on, hogging on to resources once given to it when it was a live process, at one time.

What brings about this zombie? On Linux its mostly due to bad programming that lead to errors and bugs that are not handled by the application itself.


Dealing with defunct process

Don't attempt to bring back to live a zombie process. Seek out the zombie and terminate from the system. Here are commands to handle an example zombie processes (defunct) with process id 5277.

Where are the zombie processes?
$ ps ef |grep defunct
5277 ?        Z      0:01 [example-app] <defunct>

Notice the word "defunct" and status as Z (for zombie) for the process 5277. That, is our zombie. Did you find any zombie's in your system?

The command kill is used to terminate any process and free system resources such a memory (RAM). The problem with zombie processes is that, they are dead, and can't be killed (not most of the time). What is done however, is to identify the parent process (the one responsible for creating the zombie) and terminate that process.

Identify the parent process id (5262) and terminate it.
$ ps -o ppid 5277
 PPID
 5262

$ kill -9 5262
$ ps ef |grep defunct

That is the end of the zombie. In some cases, a zombie might be holding on to a resource and it will take sometime for it to detach (give it a bit of a wait).

There are other methods to look for zombies. E.g. the ps command provides a method of displaying the parent hierarchy as follows;
$ ps xlf |grep defunct

Or in the command top, look at the second line for number of zombie processes.

What if the zombie keeps appearing on a daily basis for the same process? 

This is a problem, but sometimes its not apparent as zombie's live among the running processes without bothering anyone. Let me know if you have other than the suggestions I have list below.
  • See if there is a newer version of the application or an alternative (such as different architecture, from a different source) and install. 
  • It is possible that the application itself is not the problem. Identify any other related applications or module that is causing this zombie. Then update or find an alternative application to use.
  • Report to the application project team for a solution.

Friday, February 23, 2018

Cannot open display Error when using VNC

After opening VNC Viewer on MS Windows 10 and connecting to the Centos 7, the desktop appears as planned. However, there is an error when running some apps such as system-config-printer. The error is

Gtk-WARNING **: cannot open display:1

Good to note that the VNC server was providing the session at display 1 on port 5901. If you are running the display on another port, it wouldn't matter.

Solution:

At the user's terminal (AND as that user NOT sudo or root), add localhost to the xhost permission. Type

$ xhost +localhost

And this allows system-config-printer to run.


Wednesday, February 14, 2018

Cordova: Accepting user input

What would be the first thing in programming? A simple use of variable and user text input comes to mind. Following the previous post (link here), the Cordova programming environment is set up for Android. The existing files will be edited to do the following;

The HelloWorldApp when started, will prompt the user to enter a city name.






The user enters a city name and press "OK" button. The city name will be displayed on the app.






Step 1. Position the text to display user response.

Using a text editor, open the file index.html in HelloWorld/www and replace the previous <p> tag with the text "Hello World" with;

<div id="answer"></div>

Save the file.

Step 2. Prompt user at apps startup and display the reply.

Open the file index.js in HelloWorld/www/js

Add our function called getAnswer to the end of the file;
function getAnswer() {
    var ans = prompt("Name your city");
    if (ans.length > 0) {      
        document.getElementById('answer').innerHTML = "";      
        document.getElementById('answer').innerHTML = ans + " is a great place.";
    } else {      
        getAnswer();
    }
}


Activate the getAnswer function in the initialize:function( )by adding after document.addEventListener(...);

getAnswer( );

Step 3. Build and launch the app.

At the command line interface (CLI) or the command terminal, type

$ cordova build android
$ cordova run android

This will launch the default emulator.

In order to test the app, at any time after editing to the source files, just follow this Step 3.

Troubleshooting

Android emulator error: 
The connection to the server was unsuccessful. (file:///android_assets/www/index.html)

Edit the file config.xml in HelloWorld folder. Add the following line after the </platform> tag.

<preference name="loadUrlTimeoutValue" value="700000" />

Build and launch the app.

Tuesday, February 13, 2018

How to Install Cordova for Android Development

One challenge in developing a mobile application is in choosing a mobile environment. Apache Cordova allows building of an application through the use of Java for Android, AngularJS, SCSS to create reusable code across platforms. I suggest that some study is done on those languages in order to fast track learning. Cordova supports several development platforms, for this article, its focus is on Android platform.

Cordova Installation

Here are the steps to get started on a command line with installation of Cordova and launching the Android emulator on MS Windows 10 (version 10.0.16299). All the versions mentioned here are those that I am using for this article.

1.Install Base Environment

Cordova relies on well known development tools to generate the latest mobile application. Install Java Development Kit (version 8 or newer), Android studio, Node.js and git. Ensure all these are working.
  1. Java Development Kit. This should be version 8 or newer and configure JAVA_HOME.
  2. Android Studio (This article is version 2.3.3). Install the required SDK platform (I choose Android 7.1.1 Nougat), SDK Tools comprising of Android SDK Tools (version 26.1.1), Android SDK Platform-tools (version 27.0.1) and Android Support Repository (version 47.0.0). In SDK Tools, install Android Emulator. On MS Windows platform, also install HAXM (I have version 6.2.1).
  3. Node.js. This article is version 6.9.1 with npm 3.10.8. 
  4. Optional: GIT (version 2.8.1.windows.1)
Ensure environment variables are configured for JAVA_HOME and ANDROID_HOME

Then at the command prompt type (do not type the dollar sign);
$ npm install -g cordova

On linux
$ sudo npm install -g cordova

2. Creating Hello World App.

We will create a Cordova project in a folder HelloWorld. This will be in io.cordova.project with the app title of HelloWorldApp.
$ cordova create HelloWorld io.cordova.project HelloWorldApp
$ cd HelloWorld

Here is the syntax to create any project
cordova create [project_name] [package_name] [apk_name]

Open the folder platforms, notice it is empty.
Cordova Project folder for HelloWorld


Open and identify the codes in index.html located at
HelloWorld\www


In HelloWorld\www edit index.html to display Hello World. At end of the app tag, add 1 line
<div class="app">


<p>Hello World</p>
</div>

3. Install Android Platform.

When it is the first time creating a project, the target platform has to be added. Here its created in a folder CordovaProject.
$ cordova platform add android

Have a look at the Android source files are located at MainActivity.java in

HelloWorld\platforms\android\app\src\main\java\io\cordova\project

Default MainActivity.java file

Each time the apps code is modified, the project is compiled and run in an emulator OR on a mobile device. Compile and build the project for the Android platform.
$ cordova build android

Start the apps in an emulator. The default emulator manager is by Genymotion and the default emulator is Pixel with Android API 25.
$ cordova emulate android.


If you intend to plug in your Android device to run the app, or  start the apps in an external device, type
$ cordova run android



4. Tips

List existing platform installed and what platform is available.
$ cordova platform ls

Blog Archive