Showing posts with label Automate Shell Scripts. Show all posts
Showing posts with label Automate Shell Scripts. Show all posts

Friday, 29 December 2017

Automate Port kill using shell script in Unix/Ubuntu


Many Times, we are getting ''port already in use' while working with server application on different ports.

Find process associated with port and killing that process is time consuming and tedious task.

Solution:

Create port-kill.sh file in home folder and copy below code inside that file.

$sudo gedit ${HOME}/port-kill.sh


for i in "$@"; do
  echo 'killing port= ' $i
  kill -9 $(lsof -t -i:$i)
  #lsof -i -P|grep $i | awk 'NR!=1 {print $2}' | xargs kill -9
  echo 'killed port= ' $i
done



Run port-kill.sh file by passing port number as parameter.

$sh ${HOME}/port-kill.sh <port-numbers-separated-by-space>

For Ex. I want to close process runing on 9000 port number

sachin@sachin-E470:~$ lsof -i -P|grep 9000
java    12335 sachin   31u  IPv6 781666      0t0  TCP *:9000 (LISTEN)
sachin@sachin-E470:~$ sh port-kill.sh 9000 
killing port=  9000

killed port=  9000
sachin@sachin-E470:~$ 
sachin@sachin-E470:~$ lsof -i -P|grep 9000
sachin@sachin-E470:~$ 

ScreenShot:






Automate frequent tasks using shell script in Linux/Ubuntu


As we are developer, We need to start many things frequently. But to start any app/server, we need to navigate to that folder and needs to execute it. This is very time consuming and ridiculous process.

Solution:

Create one shell script file and manage all tasks through proxy gateway number using  if else.

For ex. I am registering proxy gateways for eclipse, android studio, spring boot servers etc. as shown below:

create start.sh file in home:

$sudo gedit ${HOME}/start.sh

Copy below script in ge editor opened:

echo "Please choose any one from below choices..."

echo "1. Eclipse "
echo "2. Android Studio"
echo "3. Products Service - http://localhost:9000/"
echo "4. Crawler Service - http://localhost:9001/"
echo "5. Eureka Service - http://localhost:9002/"
echo "6. Zuul Service Service - http://localhost:9003/"
echo "********************************************"

echo "Enter your choice number"


while :
do
  read INPUT_STRING
  case $INPUT_STRING in
1)
echo "Starting Eclipse!!!"
nohup /home/sachin/eclipse/jee-oxygen/eclipse/eclipse;
break
;;
2)
echo "Starting Android Studio!!!"
nohup sh /home/sachin/softwares/android-studio/bin/studio.sh;
break
;;

3)
echo "Starting Products Service - http://localhost:9000/!!!"
cd /home/sachin/Microservices/Test/products-db/;
gradle assemble
cd build/libs/
java -jar ./products-db-0.0.1-SNAPSHOT.jar 
break
;;

4)
echo "Starting Crawler Service - http://localhost:9001/!!!"
cd /home/sachin/Microservices/Test/products-crawler;
gradle assemble
cd build/libs/
java -jar ./products-crawler-0.0.1-SNAPSHOT.jar 
break
;;
5)
echo "Starting Eureka Service - http://localhost:9002/!!!"
cd /home/sachin/Microservices/Test/eureka-server-module;
gradle assemble
cd build/libs/
java -jar ./eureka-server-module-0.0.1-SNAPSHOT.jar 
break
;;
6)
echo "Zuul Service Service - http://localhost:9003!!!"
cd /home/sachin/Microservices/Test/zuul-module-service;
gradle assemble
cd build/libs/
java -jar ./zuul-module-service-0.0.1-SNAPSHOT.jar 
break
;;
*)
echo "Sorry, I don't understand"
;;
  esac
done

Start Task using start.sh

sachin@sachin-E470:~$ sh start.sh 
Please choose any one from below choices...
1. Eclipse 
2. Android Studio
3. Products Service - http://localhost:9000/
4. Crawler Service - http://localhost:9001/
5. Eureka Service - http://localhost:9002/
6. Zuul Service Service - http://localhost:9003/
********************************************
Enter your choice number
4
Starting Crawler Service - http://localhost:9001/!!!
:compileJava UP-TO-DATE
:processResources UP-TO-DATE
:classes UP-TO-DATE
:findMainClass
:jar
:bootRepackage
:assemble

BUILD SUCCESSFUL

Total time: 2.959 secs
2017-12-29 14:31:07.196  INFO 9929 --- [           main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@28ba21f3: startup date [Fri Dec 29 14:31:07 IST 2017]; root of context hierarchy
2017-12-29 14:31:07.724  INFO 9929 --- [           main] f.a.AutowiredAnnotationBeanPostProcessor : JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
2017-12-29 14:31:07.812  INFO 9929 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'configurationPropertiesRebinderAutoConfiguration' of type [org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration$$EnhancerBySpringCGLIB$$1310cc78] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/

 :: Spring Boot ::        (v1.5.9.RELEASE)

ScreenShot:



This way, we can add tasks and execute it with common gateeway using shell script





Extract error records while inserting into db table using JDBCIO apache beam in java

 I was inserting data into postgres db using apache beam pipeline. it works perfectly with JdbcIO write of apache beam library. But, now, i ...