Sunday, 23 October 2016

Understanding Exec

At times, you have noticed that there is a usage of exec in shell scripts. What it actually does, and where to use it exactly?

The command works on the theory of, is "never coming back"

exec command is shell builtin, and other members of the commands are execve, execvp, execv.

1. exec command starts the new process, without forking, so once it starts the new process, the old process get finished.

Case 1:
bash:~$ ps -ef | grep -i bash
csblog  13062 12371  1 15:19 pts/5    00:00:00 bash
ksh$ ksh
ksh$ ps -ef | grep -i ksh
csblog  13093 13062  0 15:19 pts/5    00:00:00 ksh
ksh$ ps -ef | grep -i bash
csblog  13062 12371  0 15:19 pts/5    00:00:00 bash

Case 2:
bash:~$ exec ksh
ksh$ ps -ef | grep -i bash
ksh$ ps -ef | grep -i ksh
csblog  13062 12371  0 15:19 pts/5    00:00:00 ksh

You can see, in case 1, i have started the process, without using exec, so the new shell, is subshell for the old one. But in case of case2, a totally new process is started. In this manner, your resources are saved.

2. Sometimes, you don't need user to access the shell, you can directly change in /etc/passwd, but you need to load the environment, so you can add the exec command, at the end of .profile.
3. exec, also used with file descriptor. To open, close, read and write.

exec 3< thisfile          # open "thisfile" for reading on file descriptor 3
exec 4> thatfile          # open "thatfile" for writing on file descriptor 4
exec 8<> tother           # open "tother" for reading and writing on fd 8
exec 6>> other            # open "other" for appending on file descriptor 6
exec 5<&0                 # copy read file descriptor 0 onto file descriptor 5
exec 7>&4                 # copy write file descriptor 4 onto 7
exec 3<&-                 # close the read file descriptor 3
exec 6>&-                 # close the write file descriptor 6

read <&3
echo stuff >&4


Sunday, 16 October 2016

How to log your mysql queries in Ubuntu 16.04

At times, you need to log your mysql queries.

The first you need to locate your configuration file of mysql. You can use either locate command or the find command for it. The name of the configuration file for mysql is "my.cnf", and in ubuntu its usually present in your /etc directory.

$ find /etc -name "my.cnf"


$ locate my.cnf

In my system, it's located in /etc/mysql/my.cnf

Once the file is located, open it using your favorite text editor.
There are chances that the file is not writable by your user, so you need to use sudo to switch your user to root to edit the file.

Open the file and add the lines,

general_log = on
general_log_file = /var/log/mysql/mysql_query.log

It will add the group as mysqld, and start the logging. The logs will be save in file:


Save it and close it.

Then you need to restart your mysql server, to enable the option. Once the mysql service is started, the logging will start.

$ sudo service mysqld restart

Sample log file:

$ cat /var/log/mysql/mysql_query.log
/usr/sbin/mysqld, Version: 5.7.15-0ubuntu0.16.04.1-log ((Ubuntu)). started with:
Tcp port: 3306  Unix socket: /var/run/mysqld/mysqld.sock
Time                                                  Id Command    Argument
2016-10-13T14:02:57.300324Z    3 Connect myuser@localhost on  using Socket
2016-10-13T14:02:57.300563Z    3 Query select @@version_comment limit 1
2016-10-13T14:03:02.895987Z    3 Query SELECT DATABASE()
2016-10-13T14:03:02.896302Z    3 Init DB test
2016-10-13T14:03:02.898415Z    3 Query show databases
2016-10-13T14:03:02.902113Z    3 Query show tables

Saturday, 6 August 2016

npm Error: npm ERR! network read ECONNRESET

Node Error:

npm http GET
npm http GET
npm http GET
npm ERR! network read ECONNRESET
npm ERR! network This is most likely not a problem with npm itself
npm ERR! network and is related to network connectivity.
npm ERR! network In most cases you are behind a proxy or have bad network settin
npm ERR! network
npm ERR! network If you are behind a proxy, please make sure that the
npm ERR! network 'proxy' config is set properly.  See: 'npm help config'

This happened because npm is not able to connect with https. Change the https to http, and it will work.

npm config set registry

Saturday, 14 May 2016

Resume a suspended job in linux ubuntu


Handling jobs in linux environment:

The general job control commands in Linux are:
  • jobs - list the current jobs
  • fg - resume the job that's next in the queue
  • fg %[number] - resume job [number]
  • bg - Push the next job in the queue into the background
  • bg %[number] - Push the job [number] into the background
  • kill %[number] - Kill the job numbered [number]
  • kill -[signal] %[number] - Send the signal [signal] to job number [number]
  • disown %[number] - disown the process(no more terminal will be owner), so command will be alive even after closing the terminal.
That's pretty much all of them. Note the % infront of the job number in the commands - this is what tells kill you're talking about jobs and not processes.

Wednesday, 27 April 2016

Difference bewteen pipe, xargs, and exec

 xargs, pipes and exec:

First off, exec is nothing like the first two. exec does a variety of things from executing a command without returning(the shell is replaced by the new command), as well as opening/closing files.

pipes transfer the output of one program into the input of another. Without the pipe or redirection, they'd be trying to read from your keyboard and trying to print to your screen, but you can feed them data from any source and send their output anywhere you like. Shell utilities are intended to be flexible that way.

# useless example
echo asdf | cat

'echo' prints to standard output, 'cat' reads from standard input, the pipe joins them together so 'asdf' is read by cat and printed.

But what about this:

# This won't print anything
echo something | echo

echo does not read from standard input, it takes commandline parameters and nothing but parameters. So how do we translate from pipe into that?

This is what xargs is for.
echo a b c d | xargs command

is equivalent to command a b c d. Whenever you need to translate from a pipe or file into commandline arguments, xargs serves.

# This prints 'something' into xargs' input, causing xargs to run 'echo something'.
echo something | xargs echo 

Let's consider an example of exec:

$ find . -name "*.txt" -exec ls -l {} \;

The above command will create multiple process and generate the result. Suppose there are 3 text files named file1.txt, file2.txt, & file3.txt. The above command will start below 3 commands:

ls -l file1.txt
ls -l file2.txt
ls -l file3.txt

To restrict to 1 thread or 1 process, you can use xargs or +symbol. For example:

$ find . -name "*.txt" | xargs ls -l
$ find . -name "*.txt" -exec ls -l {} \+

However, you can mention the parallelism in xargs with -P option. The above command will result in:

ls -l file1.txt file2.txt file3.txt

Only 1 thread.


Saturday, 9 April 2016

Composer | Windows | PHP Error : "You must enable the openssl extension to download files via https"

PHP - Composer - Windows

Okay, you are trying to install composer, and encountered the above error. This error occurred, if your php_openssl.dll is missing or commented in your php.ini.

Steps to resolve:

1. Download from here (64 bit version). (Other version can be found here: PHP for Windows)

2. Unzip it and copy all the contents inside of uncompressed folder in to the C:\Program Files\php directory. (Create php directory)

3. Add C:\Program Files\php in your environment PATH variable.

4. Download composer and try to install it.

5. If you get the above error, copy the php.ini-development file present in C:\Program Files\php, and rename it to php.ini file.

6. Open the php.ini file.

7.  Find the ";extension=php_openssl.dll " then change it to "ext/php_openssl.dll" (removed semi-colon and added ext)

and continue with the installation of composer. It will install successfully.