[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 631
  • Last Modified:

Shell commands: Need to run a command only after another has completed

I kicked-off a script to run, called "myscript1".  I need to automatically run another script "myscript2" only after myscript1 has completed.  I realize the simplest way is to do:
   > myscript1; myscript2
However, I am talking about the case where I did not kick-off both scripts together.  What commands can I use to monitor the already running script myscript1 to check when it has completed and automatically kick-off myscript2 ?
Thanks.
0
tawb
Asked:
tawb
  • 4
  • 4
  • 3
  • +3
1 Solution
 
gripeCommented:
You could create a file that indicates that the command is running and check for the existence of this file with your second command. Remember to remove the file when your first command is finished.
0
 
gripeCommented:
myscript1:
#!/bin/sh

touch /tmp/myscript1.running

# do some stuff

rm /tmp/myscript1.running

myscript2:
#!/bin/sh

if [[ -e /tmp/myscript1.running ]]
then
    exit
else
    # do your thing.
fi
0
 
tawbAuthor Commented:
Thanks gripe.  I am looking for the specific shell commands I could use, i.e. copy/paste from your solution, e.g. combinations of the ps, wait, sleep, echo $? commands or others I am not familiar with.  
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
gripeCommented:
I posted a complete example in my second answer. All you need to do is add your shell script to the '# do your thing' and '# do some stuff' sections.
0
 
tawbAuthor Commented:
Gripe - That's useful, thanks.  Now what if it's a script/process that I don't have editing access to.  That is, myscript1 is just a binary process or a script I don't have write permissions to but I need to run it and monitor it for completion before running my second command/script.  Please advise.
0
 
wesly_chenCommented:
Hi,

myscript2:
---
#!/bin/sh

ps -elf |grep myscript1
if [ "$?" = "0" ]; then
  exit
else
  <run your job here>
fi

Wesly
0
 
wesly_chenCommented:
-- myscript2.sh ---
#!/bin/sh

while 1
do
  ps -elf |grep myscript1 | grep -v grep > /dev/null 2>&1
  while [ "$?" = "1" ]
  do
    <run your job here>   # <--- put your script here
    exit
  done

  sleep 100  # wait for 100 seconds, you can modify this value
done
-------
So this script will run "ps -elf |grep myscript1 | grep -v grep > /dev/null 2>&1"
every 100 seconds to check if myscript1 is running or not. If not, it will run you job.

Wesly
0
 
TintinCommented:
Why not just get script1 to kick off script2?  Is there some reason you can't do that?
0
 
wesly_chenCommented:
> myscript1 is just a binary process or a script I don't have write permissions to
Tintin, you might want to check his previous comment.

Wesly
0
 
yuzhCommented:
If you want to make sure script1 is complete ok then start script 2, you do:

#!/bin/ksh
/path-to/script1
 if [[ "$?" -eq 0 ]] ; then
     /path-to/script2
 else
    echo "error with running script1"
fi
exit



0
 
manav_mathurCommented:
myscript1 && myscript2

manav
0
 
manav_mathurCommented:
myscript1 && myscript2
will only run myscript2 if myscript1 finished successfully

myscript1 || myscript2
will only run myscript2 if myscript1 did not execute successfully.

manav
0
 
wesly_chenCommented:
Hi yuzh,

#!/bin/ksh
/path-to/script1  <=== Will this run script1 again? What if it can run multiple script1's at the same time?
 if [[ "$?" -eq 0 ]] ; then

Wesly
0
 
manav_mathurCommented:
>However, I am talking about the case where I did not kick-off both scripts together
>What commands can I use to monitor the already running script myscript1 to check when it has completed and >automatically kick-off myscript2 ?


Why would you like to do that? A script kicked off and still waiting for other scrpt to complete is at best, occupying memory and at worst, creating deadlocks.

In any case, you wnat to run script2 only if script1 finishes off successfully, right?? SO why keep script2 waiting all the while script1 is running.

script1;script2 will run script2 regardless of whether script1 *completed* successfully or failed.
See my earlier posts.

gripe, you are suggesting semaphorical approaches which are best used when the two programs will try to access some common resources in some part. When two whole binaries have to mutually-exclusive, thats another condition.

Manav
0
 
manav_mathurCommented:
>/path-to/script1  <=== Will this run script1 again? What if it can run multiple script1's at the same time?

Wesly, even in this case, there are two possibiliies
- only one myscript2 should run after all instances of script1 have finished
- one myscript2 per myscript1

Which one to follow??

Although I feel the problem is not that complex, its better if the author comes up and clarifies

Manav
0
 
yuzhCommented:
Just clearify a bit, I was talking about to write another litter script, to start script1 and
then script2!
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 4
  • 4
  • 3
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now