Wednesday, 4 March 2020

Bash Script: How to check the status of background processes

Example 1 :  Failure case with exit 24

[smpici@c712f6n06 myscripts]$ cat simple_bkGroundproc.sh
#!/bin/sh

cmd() { sleep 5; exit 24; }

cmd &   # Run the long running process
pid=$!  # Record the pid

# Spawn a process that coninually reports that the command is still running
while echo "$(date): $pid is still running"; do sleep 1; done &
echoer=$!

# Set a trap to kill the reporter when the process finishes
trap 'kill $echoer' 0

# Wait for the process to finish
if wait $pid; then
    echo "cmd succeeded"
else
    echo "cmd FAILED!! (returned $?)"
fi
[sachin@host1 myscripts]$ ./simple_bkGroundproc.sh
Wed Mar  4 06:36:34 EST 2020: 96853 is still running
Wed Mar  4 06:36:35 EST 2020: 96853 is still running
Wed Mar  4 06:36:36 EST 2020: 96853 is still running
Wed Mar  4 06:36:37 EST 2020: 96853 is still running
Wed Mar  4 06:36:38 EST 2020: 96853 is still running
cmd FAILED!! (returned 24)
[sachin@host1 myscripts]$

--------------------------------------------

Example 1 :  Passed case with exit 0


#!/bin/sh

cmd() { sleep 5; exit 0; }

cmd &   # Run the long running process
pid=$!  # Record the pid

# Spawn a process that coninually reports that the command is still running
while echo "$(date): $pid is still running"; do sleep 1; done &
echoer=$!

# Set a trap to kill the reporter when the process finishes
trap 'kill $echoer' 0

# Wait for the process to finish
if wait $pid; then
    echo "cmd succeeded"
else
    echo "cmd FAILED!! (returned $?)"
fi

------------
OUTPUT
[sachin@host1 myscripts]$ ./simple_bkGroundproc.sh
Wed Mar  4 06:37:04 EST 2020: 97118 is still running
Wed Mar  4 06:37:05 EST 2020: 97118 is still running
Wed Mar  4 06:37:06 EST 2020: 97118 is still running
Wed Mar  4 06:37:07 EST 2020: 97118 is still running
Wed Mar  4 06:37:08 EST 2020: 97118 is still running
cmd succeeded


 ++++++++++++++++++++++++++++++++++++++++

Example 3 :

#! /bin/bash
fvt_test_arg="Default HCOLL IBV_ENABLE pami_noib"
IFS=', ' read -r -a fvt_test_arg_array <<< "$fvt_test_arg"

items="1 2 3 4"
pids=""

for item in $items; do
    sleep $item &
    pids+="$! "
done
echo "list of pids $pids"
i=0


for pid in $pids; do
    wait $pid
    if [ $? -eq 0 ]; then
        echo "${fvt_test_arg_array[i]} SUCCESS - Job $pid exited with a status of $?"
    else
        echo "${fvt_test_arg_array[i]} FAILED - Job $pid exited with a status of $?"
    fi
    i=$((i+1))
done
-----------------------
OUTPUT:
[sachin@host1 myscripts]$ ./pid_array2.sh
list of pids 81822 81823 81824 81825
Default SUCCESS - Job 81822 exited with a status of 0
HCOLL SUCCESS - Job 81823 exited with a status of 0
IBV_ENABLE SUCCESS - Job 81824 exited with a status of 0
pami_noib SUCCESS - Job 81825 exited with a status of 0
[sachin@host1 myscripts]$

-----------------------

Reference:
https://stackoverflow.com/questions/1570262/get-exit-code-of-a-background-process

Bash script : How to split string to array and count/print array elements based on index

IFS    The Internal Field Separator that is used for word splitting after expansion and to split lines into words with the read builtin command. The default value is <space><tab><newline>.


Example 1 :

#!/bin/bash
fvt_test_arg="DEFAULT HCOLL IBV PAMI_NOIB"
echo "This is fvt_test_arg : $fvt_test_arg"
IFS=', ' read -r -a fvt_test_arg_array <<< "$fvt_test_arg"
echo "This is fvt_test_arg_array : ${fvt_test_arg_array[@]}"
echo "Number of elements in the array : ${#fvt_test_arg_array[@]}"
echo "1st element is :${fvt_test_arg_array[0]}"
echo "2nd element is :${fvt_test_arg_array[1]}"
echo "3rd element is :${fvt_test_arg_array[2]}"
echo "4th element is :${fvt_test_arg_array[3]}"


----------------------------
OUTPUT:

[sachin@host1 myscripts]$ ./list4.sh
This is fvt_test_arg : DEFAULT HCOLL IBV PAMI_NOIB        ===> String
This is fvt_test_arg_array : DEFAULT HCOLL IBV PAMI_NOIB  ====> array
Number of elements in the array : 4
1st element is :DEFAULT
2nd element is :HCOLL
3rd element is :IBV
4th element is :PAMI_NOIB
[sachin@host1 myscripts]$

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
EXAMPLE 2:

#!/bin/bash
fvt_test_arg="DEFAULT HCOLL IBV PAMI_NOIB"
echo "This is fvt_test_arg : $fvt_test_arg"
IFS=', ' read -r -a fvt_test_arg_array <<< "$fvt_test_arg"
echo "This is fvt_test_arg_array : ${fvt_test_arg_array[@]}"
echo "Number of elements in the array : ${#fvt_test_arg_array[@]}"
echo "1st element is :${fvt_test_arg_array[0]}"
echo "2nd element is :${fvt_test_arg_array[1]}"
echo "3rd element is :${fvt_test_arg_array[2]}"
echo "4th element is :${fvt_test_arg_array[3]}"

pids="1111 2222 3333 4444"
i=0
for pid in ${pids[@]}; do
    echo " $pid is the PID of the process for :${fvt_test_arg_array[i]} is successful"
    i=$((i+1))
done

 ---------------
OUTPUT:
[sachin@host1 myscripts]$ ./list5.sh
This is fvt_test_arg : DEFAULT HCOLL IBV PAMI_NOIB
This is fvt_test_arg_array : DEFAULT HCOLL IBV PAMI_NOIB
Number of elements in the array : 4
1st element is :DEFAULT
2nd element is :HCOLL
3rd element is :IBV
4th element is :PAMI_NOIB
 1111 is the PID of the process for :DEFAULT is successful
 2222 is the PID of the process for :HCOLL is successful
 3333 is the PID of the process for :IBV is successful
 4444 is the PID of the process for :PAMI_NOIB is successful
[smpici@c712f6n06 myscripts]$


------------------------------------
 EXAMPLE  3:

#!/bin/bash

declare -a num
declare -a words

num=(ddd1 dd2 dd3 dd4 dd5 dd6 dd7)
words=(one two three four five six seven)

n=${#num[@]}
echo $n
for ((i=0;i<$n;i++)); do
  echo ":${num[$i]} :${words[$i]}:"
done
[sachin@host1 myscripts]$

--------------------------

OUTPUT:
[sachin@host1 myscripts]$ ./list3.sh
7
:ddd1 :one:
:dd2 :two:
:dd3 :three:
:dd4 :four:
:dd5 :five:
:dd6 :six:
:dd7 :seven:
-----------------------------------------------

Reference:
https://stackoverflow.com/questions/10586153/split-string-into-an-array-in-bash/13196466

Tuesday, 24 February 2015

VNC Server Setup

Procedure to setup VNC server to access the GUI ( x Server)  from VNC client remotely.
===============================================================
1) cat /etc/sysconfig/vncservers

 VNCSERVERS="1:sachin"  ==>  PORT 2 on secured env will throw connection error
 VNCSERVERARGS[2]="-geometry 800x600 -nolisten tcp -localhost"




2) [root]# su - sachin
-bash-4.1$ whoami
sachin
-bash-4.1$ vncpasswd
Password:
Verify:
-bash-4.1$



3) service vncserver stop

4) service vncserver start

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++



 [root@ ~]# vncserver -help

usage: vncserver [:<number>] [-name <desktop-name>] [-depth <depth>]
                 [-geometry <width>x<height>]
                 [-pixelformat rgbNNN|bgrNNN]
                 [-fp <font-path>]
                 [-fg]
                 [-autokill]
                 <Xvnc-options>...

       vncserver -kill <X-display>

       vncserver -list

[root@ ~]#


=====================================================


[root@~]# vncserver -list

TigerVNC server sessions:

X DISPLAY #     PROCESS ID
[root@ ~]#



=============================================================


[root@]# service vncserver status
Xvnc is stopped
[root@]# service vncserver start
Starting VNC server: no displays configured                [FAILED]
[root@ ]#



============================================
[root@ sachin]# cat /etc/sysconfig/vncservers
# The VNCSERVERS variable is a list of display:user pairs.
#
# Uncomment the lines below to start a VNC server on display :2
# as my 'myusername' (adjust this to your own).  You will also
# need to set a VNC password; run 'man vncpasswd' to see how
# to do that.
#
# DO NOT RUN THIS SERVICE if your local area network is
# untrusted!  For a secure way of using VNC, see this URL:
# http://kbase.redhat.com/faq/docs/DOC-7028

# Use "-nolisten tcp" to prevent X connections to your VNC server via TCP.

# Use "-localhost" to prevent remote VNC clients connecting except when
# doing so through a secure tunnel.  See the "-via" option in the
# `man vncviewer' manual page.

# VNCSERVERS="2:myusername"
# VNCSERVERARGS[2]="-geometry 800x600 -nolisten tcp -localhost"


 VNCSERVERS="1:sachin"
 VNCSERVERARGS[2]="-geometry 800x600 -nolisten tcp -localhost"


[root@]#

NOTE: PORT 2 on secured env will throw connection error
============================================
[root@sachin]# service vncserver status
Xvnc is stopped
[root@sachin]# service vncserver start
Starting VNC server: 2:sachin xauth:  creating new authority file /myhome/sachin/.Xauthority

New 'bgqxxx:2 (sachin)' desktop is bgqxxxa:2

Creating default startup script /bglhome/sachinpb/.vnc/xstartup
Starting applications specified in /bglhome/sachin/.vnc/xstartup
Log file is /bglhome/sachin/.vnc/bgqxxx:2.log

                                                           [  OK  ]
[root@ sachin]#



=====================================================
If you are not able to connect  VNC server  from VNC client , then Change the PORT to 1  in config file and start vncserver again.


[root@sachin]# ps -ef | grep vnc
sachin  2630     1  0 23:17 ?        00:00:01 /usr/bin/Xvnc :2 -desktop bgqxxx:2 (sachin) -auth /myhome/sachin/.Xauthority -geometry 800x600 -rfbwait 30000 -rfbauth /myhome/sachin/.vnc/passwd -rfbport 5902 -fp catalogue:/etc/X11/fontpath.d -pn -nolisten tcp -localhost
sachin  2649  2647  0 23:17 ?        00:00:00 vncconfig -iconic
root      3680 10443  0 23:24 pts/8    00:00:00 grep --color=auto vnc
[root@ sachin]#
=======================================================================

Example : showing TotalView  screen  for MPI_hello_world_Program



NOTE: Putty would throw this error and needs VNC client  to connect X server   to use  TotalView Graphical User Interface .

-bash-4.1$ export LM_LICENSE_FILE=/myusr/mtn/totalview/toolworks/flexlm-10.8.0-3/license.dat
-bash-4.1$ /myusr/mtn/totalview/current/bin/totalview -my_io_interface 172.16.1.2 runjob -a --exe /myusr/mtn/a.out --block R-M-N
Unable to open X display. Please check your $DISPLAY environment
variable to ensure that it is defined correctly and that you are
authorized to connect to this X server.
-bash-4.1$
++++++++++++++++++++++++++END ++++++++++++++++++++++++

Reference :
http://linuxconfig.org/configuring-vnc-server-access-on-a-redhat-linux