LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 02-29-2012, 03:54 AM   #1
prravin1
Member
 
Registered: Feb 2012
Location: India
Posts: 43

Rep: Reputation: Disabled
Unhappy 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?
 
Old 02-29-2012, 05:13 AM   #2
Snark1994
Senior Member
 
Registered: Sep 2010
Location: Wales, UK
Distribution: Arch
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 345Reputation: 345Reputation: 345Reputation: 345
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,
 
Old 02-29-2012, 05:14 AM   #3
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,414

Rep: Reputation: 1967Reputation: 1967Reputation: 1967Reputation: 1967Reputation: 1967Reputation: 1967Reputation: 1967Reputation: 1967Reputation: 1967Reputation: 1967Reputation: 1967
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.
 
Old 02-29-2012, 05:15 AM   #4
eosbuddy
Member
 
Registered: Feb 2012
Location: India
Distribution: Ubuntu, Fedora, Gentoo, Mandrake, RedHat, CentOs
Posts: 30

Rep: Reputation: Disabled
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.
 
1 members found this post helpful.
Old 02-29-2012, 04:01 PM   #5
wpeckham
Member
 
Registered: Apr 2010
Location: USA
Distribution: Debian, Ubuntu, Fedora, RedHat, DSL, Puppy, CentOS, Knoppix
Posts: 807

Rep: Reputation: 175Reputation: 175
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.

Last edited by wpeckham; 02-29-2012 at 04:03 PM.
 
1 members found this post helpful.
Old 02-29-2012, 04:57 PM   #6
sundialsvcs
Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 5,423

Rep: Reputation: 1158Reputation: 1158Reputation: 1158Reputation: 1158Reputation: 1158Reputation: 1158Reputation: 1158Reputation: 1158Reputation: 1158
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.
 
Old 03-04-2012, 09:34 PM   #7
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Quote:
Originally Posted by prravin1 View Post
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 View Post
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
 
2 members found this post helpful.
Old 03-05-2012, 05:20 AM   #8
prravin1
Member
 
Registered: Feb 2012
Location: India
Posts: 43

Original Poster
Rep: Reputation: Disabled
Unhappy

Quote:
Originally Posted by wpeckham View Post
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.
 
Old 03-05-2012, 05:24 AM   #9
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,414

Rep: Reputation: 1967Reputation: 1967Reputation: 1967Reputation: 1967Reputation: 1967Reputation: 1967Reputation: 1967Reputation: 1967Reputation: 1967Reputation: 1967Reputation: 1967
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.
 
1 members found this post helpful.
Old 03-05-2012, 05:41 AM   #10
eosbuddy
Member
 
Registered: Feb 2012
Location: India
Distribution: Ubuntu, Fedora, Gentoo, Mandrake, RedHat, CentOs
Posts: 30

Rep: Reputation: Disabled
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).
 
1 members found this post helpful.
Old 03-05-2012, 06:08 AM   #11
prravin1
Member
 
Registered: Feb 2012
Location: India
Posts: 43

Original Poster
Rep: Reputation: Disabled
Unhappy

Quote:
Originally Posted by eosbuddy View Post
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?
 
Old 03-05-2012, 07:00 AM   #12
eosbuddy
Member
 
Registered: Feb 2012
Location: India
Distribution: Ubuntu, Fedora, Gentoo, Mandrake, RedHat, CentOs
Posts: 30

Rep: Reputation: Disabled
script1:
Code:
...
mkdir /home/myuser/mypath
echo "/home/myuser/mypath" > /tmp/script1path
...
script2:
Code:
...
mypath=`cat /tmp/script1path`
echo $mypath
...
 
1 members found this post helpful.
Old 03-06-2012, 06:47 AM   #13
wpeckham
Member
 
Registered: Apr 2010
Location: USA
Distribution: Debian, Ubuntu, Fedora, RedHat, DSL, Puppy, CentOS, Knoppix
Posts: 807

Rep: Reputation: 175Reputation: 175
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?
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
stop scripts execution using apache asim.mcp Linux - Server 1 04-17-2011 12:08 PM
How to control execution of programs in a shell script. Chrisantha1 Programming 5 04-28-2010 04:29 AM
KNetworkManager and Shell Scripts ~ lost in execution. javaguy78 Linux - Hardware 0 07-17-2006 01:50 AM
Parallel Execution Capable Scripts? irfanhab Programming 5 08-28-2004 07:04 AM
Execution of PERL scripts in Linux which is compiled in WINDOWs environment to_veera Linux - Software 3 06-18-2004 09:50 AM


All times are GMT -5. The time now is 08:31 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration