Shell script for running simultaneously two interfaces

Hi!

I have two environments on a machine and each one has some scripts for checking if some interfaces are down, and if so, it restarts them.

The issue now is that i cannot keep both interfaces running, on both environments i see the same process running.

How can i modify my scripts in order to reach that?

I have one script for checking if my .sh is running, and another one for restarting it (the first one calles the latter if it's down)

isRunning.sh is something like this:

ps -ef |grep -v grep |grep -v 'isProgRunning.sh'| grep -v 'stopProg.sh' |grep -v 'runProg.sh' |grep -v 'restartProg.sh'| grep -v 'ProgStopper' | grep 'XX:SurvivorRatio=4 -Xnoclassgc' >/dev/null 2>/dev/null

if [ $? -eq 0 ]
then
	echo ""
	echo "Process: " `ps -ef |grep -v grep |grep -v 'isProgRunning.sh'| grep -v 'stopProg.sh' |grep -v 'runProg.sh' |grep -v 'restartProg.sh'| grep -v 'ProgStopper' | grep 'XX:SurvivorRatio=4 -Xnoclassgc' | awk '{print $2}'`
	echo ""
	echo " is running"
	exit 0
else
	echo " is down"
	exit 1

fi

and the second sets a variable to hold the directory hierarchy from where it's executed, and it starts the interface, using nohup command.

what should i do?

I in such case use a different UID when having 2 instances of the same program running to differenciate them (different UID - same GID with group set as -rws-on the directories ...)

The traditional way to check if a process is running is PID files. When you start it, arrange for a file containing its PID to be created to reference later. This avoids the ps | awk | grep | sed | cut | kitchen | sink nonsense, and is a lot more portable, and allows you to run more than one of the same thing independently without them conflicting.

#!/bin/sh

# Often /var/run, but only root can write there
MYPIDFILE=/path/to/mypid.pid

if [ -f "$MYPIDFILE" ] && read -r PID "$PIDFILE" && ps "$PIDFILE" >/dev/null
then
          echo "Process is still running as PID $PID" >&2
          exit
fi

echo "Process is not running." >&2

# If your shell complains about 'disown', then your shell doesn't need it and it can be removed
/path/to/processtostart & disown # Assumes the process does not fork other PIDs

echo "$!" > "$MYPIDFILE"

echo "Now running as PID $!" >&2
exit

It's possible that your daemons are creating PID files somewhere already. Take a look around their files.

What you are saying is to set a variable with the pid of my process and check if it's running based on that pid?

Would that alone solve the problem of parallel processes or do i have to combine this solution with the change of uid?

More specificly, on each of the environments i have this binary folder i have different scripts for verifying the processes, i mean i run them from different paths, being different scripts. They have the same permissions now. Each pair of isProgRunning and runProg to be in the same gid, but have different uid?

No two programs will have the same PID while they're running.