Pkill Inside the Cron Script Kills Next Commands

Pkill Inside the Cron Script Kills Next Commands

I have a hash.sh file which runs as cron job. Inside the script I need to call pkill to clean some redundant process. But when pkill is called whole script ends up immediately and next commands are not executed. Here is the script with one pkill at the end

#!/bin/bash

cd /opt/selenium-server/

# -c returns number of lines in grep result
countHub=$(ps -x | grep -v "grep" | grep "selenium-server" | grep "role hub" -c)
countNodes=$(ps -x | grep -v "grep" | grep "selenium-server" | grep "role node" -c) 

# selenium server jar path
jarFilePath="/opt/selenium-server/selenium-server-standalone-3.141.59.jar"



# if selenium server HUB is in ps -x result
if [ $countHub -eq 1 ] 
then 


  # if there is NO NODE
  if [ $countNodes -eq 0 ] 
  then 
    
    # start two new nodes
    java -jar $jarFilePath -role node -hub  & 
    java -jar $jarFilePath -role node -hub  & 
    exit 0    
  
  # if there is only ONE NODE
  elif [ $countNodes -eq 1 ] 
  then 
  
    # start one new node
    java -jar $jarFilePath -role node -hub  & 
    exit 0
    
  fi  
    

else # if there is NO HUB start new hub and two new nodes


  # kill all possible hubs and nodes for sure
  pkill -9 -f "selenium-server"

  # Start new hub
  java -jar $jarFilePath -role hub & 
  java -jar $jarFilePath -role node -hub  & 
  java -jar $jarFilePath -role node -hub  & 
  exit 0

fi

How to write it correctly? Thanks for help.

7
Marcus Vance
Author

Marcus Vance

Marcus Vance is a cybersecurity auditor and technology writer dedicated to educating the public about online safety, data privacy regulations, enterprise security, and emerging cyber threats.