Shell Script behaviour and syntax

Here are the 3 scripts(FirstScrpt.sh,firstcallScrpt.sh,startProcess.sh) that were running in the server for the past several years.
I understand the way it is written is that for every 1 hour 'firstcallScript.sh' is being called in that it kills the pid of 'startProcess.sh'
and starts again.

2 things I noticed

  1. Value of pid.txt never changes if it is being is killed and created every hour.
  2. It never runs in the background even when it is run with '&' like (./FirstScrpt.sh &)

FirstScrpt.sh

#!/bin/bash

if [ $# -ne 1 ]
then
	echo "Usage:  $0 test|prod"
	exit 1
fi
envRun=$1

  echo "Level is :  ${envRun}"
	while [ 1 ]
	do
	cat pid.txt | xargs -l1 kill
	./firstcallScript.sh ${envRun}
	sleep 3600
	done

firstCallScript.sh

#!/bin/bash

if [ $# -ne 1 ]
then
	echo "Usage:  $0 test|prod"
	exit 1
fi
envRun=$1

if [ $envRun = "test" ] 
then
  echo "Level is  :  ${envRun}"
     ./startProcess.sh ${envRun}
   
 else
   echo "Level is test Invoke test script :  ${envRun}"
   	./startProcess.sh ${envRun}
	
fi

startProcess.sh

#!/bin/bash 
envRun=$1
echo "${envRun}"
echo "I'm a Linux user."
echo $! > pid.txt

And the question is?

  1. Value of pid.txt never changes if it is being is killed and created every hour. why ? i guess it should be chnaged after every hour of killing
  2. It never runs in the background even when it is run with '&' like (./FirstScrpt.sh &) why doe sit show on the screen
  1. In StartProcess.sh you only set an environment variable and echo a couple of messages. $! is supposed to be the PID of the last command run in the background, but nothing is being run in the background.

  2. So are you saying that if you type ./FirstScrpt.sh & that the prompt doesn't return and the script has to be killed (or CTRL-C) to get control back to your invoking shell?

The PID never changes because it takes the 'value' of the current process id of the shell.
There is NO subprocess - to my understanding.

The PID would only change upon something like:

echo $!
( echo $! ) &
echo $!
( echo $! ) &
echo $!

In a similar way, you could use the shell id:

echo $$
(echo $$) &

Hope this helps

For Chuber:

  1. In StartProcess.sh you only set an environment variable and echo a couple of messages. $! is supposed to be the PID of the last command run in the background, but nothing is being run in the background.
    I removed a line of code to post here. It actually calls a process /opt/$[folderpath}/bin/java ${JAVA_OPTIONS} WregProcessor.jar > /dev/null 2>&1 &

  2. So are you saying that if you type ./FirstScrpt.sh & that the prompt doesn't return and the script has to be killed (or CTRL-C) to get control back to your invoking shell? Yes

For Sea: the code is below is typed 2 times is this how it should be ?

echo $!
( echo $! ) &
echo $!
( echo $! ) &
echo $!

I know it looks that way - because it is, yes, intentional.
Just have a look :wink:

EDIT:
But it seems my previous terminal was borked in some way, only 2 lines should be enough.