LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   control the execution of the scripts (https://www.linuxquestions.org/questions/programming-9/control-the-execution-of-the-scripts-931891/)

prravin1 02-29-2012 02:54 AM

control the execution of the scripts
 
How to control the execution of the scripts based on time and memory constraints?

How to find out the time and memory requirement of a script?

I have a script which should execute after a previous script finishes execution. How can i know that a script will be finished in particular time so that i can use this information to start the next script?

Snark1994 02-29-2012 04:13 AM

The time requirements will vary, the memory requirements may or may not vary depending on the programme you've written. You can use 'time' to work out how long your programme took to run (I think it's just "time ./programme", but I'd need to check when I get home).

If you wanted to run one script then the other, however, then you could just run:

Code:

./script1; ./script2
Then it will run script1 and wait for it to finish, then run script2.

Hope this helps,

acid_kewpie 02-29-2012 04:14 AM

well how can you ever expect to know when a script is going to finish? That does't sound like a reasonable thing to ever hope to know outside of your own experience of how the script works.

Scripts don't wait for others to finish, they are executed sequentially, that's how scripts work. when one line of code has executed, the next one runs. The script has no implicit idea how long it's taking to do anything.

eosbuddy 02-29-2012 04:15 AM

You might use the time command before the script
Code:

time myscript.sh
to figure out the execution time. However if the script is using network related resources, the time of execution could vary. In which case, you could use an update flag (either in the database or within a file). The next script (run as cron) will then read this flag and execute accordingly.

wpeckham 02-29-2012 03:01 PM

Wrong end of the problem...
 
prravin1,

Most of the other answers illustrate excellent intentions, but a basic misunderstanding about how true multithreaded, multiuser, multitasking operating systems work. The one that recommended "script1 ; script2" was the only correct answer.

While you can time ONE EXECUTION of a script, there is nothing in the script, shell, or kernel that ensures that it will EVER AGAIN execute in exactly the same time. The "most correct" way to ensure this has been one of these methods:
1. execute them sequentially, as the command line provided.
2. Use semaphore controls, a shared memory or file-system flag that can be turned on or off. Have the first script ensure that it is off when it starts, start the second script a few seconds later. The second script loops, checking for the flag to be ON before continuing to run. Have the first script turn the flag ON as it exits, this signals the second script to run. Let the second script turn the flag back OFF as it exits.

The easy way for a non-programmer to implement this is by using a simple file (perhaps under /tmp or /var/tmp) with a unique name, and test on it exists, or it does not.

This uses the characteristics of the system and plays to its strengths, rather than trying to force it into a predictable mode not natural to this kind of system.

-----------
Late addition: you CAN force a system to be a bit more predictable by running the REAL TIME kernel. The overhead it adds makes your maximum performance somewhat less, but it ensures that most processes and calls have a MUCH HIGHER chance of completing in about the same time EVERY time. While very useful for some things, the better answer is to make your scripts EVENT controlled (and event controlling) rather than leaving things at the mercy of load and clock.

I hope that this helps. Please feel free to question if I have left anything unclear.

sundialsvcs 02-29-2012 03:57 PM

Perhaps the simplest way to do this is to create one script that simply executes each of the other programs in turn, as child processes to itself, waiting patiently for each one to complete.

ta0kira 03-04-2012 08:34 PM

Quote:

Originally Posted by prravin1 (Post 4614831)
How to find out the time and memory requirement of a script?

Unfortunately, if the script takes long enough for you to care then most of the memory usage is probably from other processes the script forks.
Quote:

Originally Posted by prravin1 (Post 4614831)
I have a script which should execute after a previous script finishes execution. How can i know that a script will be finished in particular time so that i can use this information to start the next script?

I'm not making the assumption that you're able to just call one after the other from a single place. I've been in situations where 2 hours into a script that would take about 16 hours to complete I decided I wanted something else to run when it finished. In those cases, you need to find out the pid of the first script, then wait for it to complete. This can often be done with pgrep. For example:
Code:

> setsid ./myscript1.sh
... this is taking forever ...
> while pgrep myscript1.sh &> /dev/null; do sleep 1; done; ./myscript2.sh

Kevin Barry

prravin1 03-05-2012 04:20 AM

Quote:

Originally Posted by wpeckham (Post 4615353)
prravin1,

Most of the other answers illustrate excellent intentions, but a basic misunderstanding about how true multithreaded, multiuser, multitasking operating systems work. The one that recommended "script1 ; script2" was the only correct answer.

While you can time ONE EXECUTION of a script, there is nothing in the script, shell, or kernel that ensures that it will EVER AGAIN execute in exactly the same time. The "most correct" way to ensure this has been one of these methods:
1. execute them sequentially, as the command line provided.
2. Use semaphore controls, a shared memory or file-system flag that can be turned on or off. Have the first script ensure that it is off when it starts, start the second script a few seconds later. The second script loops, checking for the flag to be ON before continuing to run. Have the first script turn the flag ON as it exits, this signals the second script to run. Let the second script turn the flag back OFF as it exits.

The easy way for a non-programmer to implement this is by using a simple file (perhaps under /tmp or /var/tmp) with a unique name, and test on it exists, or it does not.

This uses the characteristics of the system and plays to its strengths, rather than trying to force it into a predictable mode not natural to this kind of system.

-----------
Late addition: you CAN force a system to be a bit more predictable by running the REAL TIME kernel. The overhead it adds makes your maximum performance somewhat less, but it ensures that most processes and calls have a MUCH HIGHER chance of completing in about the same time EVERY time. While very useful for some things, the better answer is to make your scripts EVENT controlled (and event controlling) rather than leaving things at the mercy of load and clock.

I hope that this helps. Please feel free to question if I have left anything unclear.

I think that the flag control will be the best solution. But can you show me with an example how to set the flags on and off as i have no idea about file system flags.

acid_kewpie 03-05-2012 04:24 AM

a flag is whatever you want it to be. Normally you'd "touch" a file in one script and look for it in another, but there is no formal way to do this at all.

eosbuddy 03-05-2012 04:41 AM

You can set an exit status (a number that you'd prefer) in the bash script (an example here) or you could do something like this:

Code:

echo "finished" > /dev/shm/script1status
you can
Code:

cat /dev/shm/script1status
to get the status (alternatively, you could write to the /tmp folder as well). If you want this to live behind a more secure wall, you could think of writing it to a database (with more overheads of course; assuming that your script is well protected since you will be leaving authentication parameters within the script).

prravin1 03-05-2012 05:08 AM

Quote:

Originally Posted by eosbuddy (Post 4618771)
You can set an exit status (a number that you'd prefer) in the bash script (an example here) or you could do something like this:

Code:

echo "finished" > /dev/shm/script1status
you can
Code:

cat /dev/shm/script1status
to get the status (alternatively, you could write to the /tmp folder as well). If you want this to live behind a more secure wall, you could think of writing it to a database (with more overheads of course; assuming that your script is well protected since you will be leaving authentication parameters within the script).

To be more specific, my first script creates a output folder at a given location. I need to store the path f this folder in a particular location and my second script should be able to find this location and should process the data in it.

Can you show it using an example code?

eosbuddy 03-05-2012 06:00 AM

script1:
Code:

...
mkdir /home/myuser/mypath
echo "/home/myuser/mypath" > /tmp/script1path
...

script2:
Code:

...
mypath=`cat /tmp/script1path`
echo $mypath
...


wpeckham 03-06-2012 05:47 AM

Script execution
 
Simple framework, and I have not tested this code so take it for what it is.

Script one
Code:

#!/bin//bash
#
PATH=/bin;/usr/bin:/usr/local/bin/~/bin
FLAG=/tmp/bebop##7
if [ -f ${FLAG} ] ; then
  echo "Please check if I am already running: ${FLAG} exists!"
  exit 1
fi
touch ${FLAG}
// lots of code, making folders, etc
/bin/rm ${FLAG}
echo "I am done!"
exit 0

Script two
Code:

#!/bin/bash
#
PATH=/bin;/usr/bin:/usr/local/bin/~/bin
FLAG=/tmp/bebop##7
while [ -f ${FLAG} ] ; do
  sleep 10
  # waiting for first script to finish
done
// lots of code, using folders, etc

Does that make sense to you?


All times are GMT -5. The time now is 02:22 PM.