Operating System - Linux
1752805 Members
5597 Online
108789 Solutions
New Discussion юеВ

Re: run shell scripts sequentially.

 
Rohit Khaladkar
Occasional Contributor

run shell scripts sequentially.

Hi All,
I have a master script , which would run 4 scripts in sequence. When the first script gets executed , I need to check if a particular process has been completed. If it is completed , only then proceed with the second script.

This same rule applies to script3 and script4.

Can someone please help me out.This is the function that I wrote for the process check.


process_check ()
{
p1=`ps -ef | grep o1 |grep -v grep| wc -l`
if [ "$p1" = 0]
echo "Process not running"
return 0
else
echo "Process completed"
return 1
}
3 REPLIES 3
Ralph Grothe
Honored Contributor

Re: run shell scripts sequentially.

As this is posted in the Linux section
why not use pgrep like

$ proc_is_up() { pgrep ${1:-''} >/dev/null 2>&1; }

and simply base your control flow on it like

$ if proc_is_up bla;then echo runs;else echo absent;fi
absent

$ if proc_is_up httpd;then echo runs;else echo absent;fi
runs

Madness, thy name is system administration
Fredrik.eriksson
Valued Contributor

Re: run shell scripts sequentially.

Maybe pidof could work?
something like this:

if pidof name_of_proc; then
return 0
else
return 1
fi

If there are multiple process with the same name this probably won't work. It'll return the all the pids of all the process by that name. But if this is a unique name I would say that it's a good slim variant :)
Ivan Ferreira
Honored Contributor

Re: run shell scripts sequentially.

>>> I have a master script , which would run 4 scripts in sequence. When the first script gets executed , I need to check if a particular process has been completed. If it is completed , only then proceed with the second script.

I would then add the checking on the first script. If the process is running, exit with 0, if not, exit with 1.

Then in your master script set:

script1.sh && script2.sh

And so on.
Por que hacerlo dificil si es posible hacerlo facil? - Why do it the hard way, when you can do it the easy way?