LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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 06-19-2006, 06:10 PM   #1
kinetik
Member
 
Registered: Dec 2005
Location: The most beautiful city in the world.
Distribution: Mostly RedHat. Also Suse, Ubuntu, PHLAK etc.
Posts: 149

Rep: Reputation: 15
RENAMED: Loop script based on time


Hi again Nix-friends!


I don't know how many questions I've asked here up until now (got answers for most to which I'm eternally greatful), but I've got yet another question:


Say I have a script, called script.sh. This script suffers from a particular problem... sometimes it works, other times it just gets stuck. When it gets stuck I have to do the whole Ctrl+C thing and manually do whatever it is I have to do.

To get around this, I was thinking of composing a new script which will essentially run the script.sh script, time it, then stop it if after xx seconds if it is still running.
Phase 2 will just hold the commands I'll ordinarily run in case of the script.sh script failing.

Now you may ask why don't I just write the darn script with the commands I'd usually use if the script.sh script fails, right? Well, good question! Personally I'd rather would've gone that route too, unfortunately though policy forces me onto the detour...


So, how would I go about this?


Here's what I was thinking:

Quote:
script.sh &
SCRIPTTIME=`ps -efo %c%p%t | grep "script.sh" | sed -n '/grep/!p' | awk {'print $4'}`
SCRIPTPID=`ps -efo %c%p%t | grep "script.sh" | sed -n '/grep/!p' | awk {'print $3'}`

if [ $SCRIPTTIME >= "00:30" ]
then
kill -9 $SCRIPTPID
After the kill thing I'd insert whatever I'd have done when the script.sh script failed. Thing is though, how can I get my script to update the SCRIPTTIME variable every say 2 seconds for 40 seconds?

Thanks in advance

Last edited by kinetik; 06-19-2006 at 07:49 PM.
 
Old 06-19-2006, 07:49 PM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Why not make a sleep function in the script itself that wakes after n time, determine what the progress is (state file?) and kill/continue? BTW, it probably isn't what functions should be used for, but with encapsulation something like: doSomething() { doSomething --really; || ( logger "script exit at $FUNCNAME"; exit 127 ); }
could provide easier status?
 
Old 06-19-2006, 08:01 PM   #3
kinetik
Member
 
Registered: Dec 2005
Location: The most beautiful city in the world.
Distribution: Mostly RedHat. Also Suse, Ubuntu, PHLAK etc.
Posts: 149

Original Poster
Rep: Reputation: 15
Hi unSpawn!

Thanks for the reply! You especially saved my skin quite a few times! Much appreciated.


As for your suggestions, I'm not really sure how to do that unfortunately. Trying to keep it as simple as possible, or more precisely as easy for me to understand as possible.
The sleep thing seems really attractive to me at this point, though I was looking into:

Quote:
for <something> in SCRIPTTIME; do
if [ $SCRIPTTIME >= "00:30" ]
then
kill -9 $SCRIPTPID
I have no idea how the for statement works though...
 
Old 06-19-2006, 08:42 PM   #4
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
You especially saved my skin quite a few times! Much appreciated.
Thanks. Good to know.


The sleep thing seems really attractive to me at this point
Say you have:
state=/var/run/script.sh_state
bill=$$
killThis() { sleep 300s; if [ "$(cat "$state")" = "FAIL" ]; then logger "Look momma, no hands!"; kill -9 $bill; else killThis; fi; }
so your commands run and if exit != 0 dump it:
command || echo FAIL > "$state"


for <something> in SCRIPTTIME; do
until [ "$SCRIPTTIME" = "00:30" ]; do sleep?
 
Old 06-19-2006, 09:36 PM   #5
kinetik
Member
 
Registered: Dec 2005
Location: The most beautiful city in the world.
Distribution: Mostly RedHat. Also Suse, Ubuntu, PHLAK etc.
Posts: 149

Original Poster
Rep: Reputation: 15
You lost me unSpawn...

It's my own fault though. I'm going to go through some documentation and get myself up to speed first, I'll check back in a bit later.


Thanks for the help so far buddy.
 
Old 06-20-2006, 08:36 AM   #6
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
very simple method

Code:
main()
{
    find /
}

main &
sleep 5
kill -9 $!
 
Old 06-20-2006, 09:17 AM   #7
spirit receiver
Member
 
Registered: May 2006
Location: Frankfurt, Germany
Distribution: SUSE 10.2
Posts: 424

Rep: Reputation: 33
Maybe timeout will do the job, have a look at "man timeout". You can query the exit status afterwards using $? .
 
Old 06-20-2006, 04:42 PM   #8
kinetik
Member
 
Registered: Dec 2005
Location: The most beautiful city in the world.
Distribution: Mostly RedHat. Also Suse, Ubuntu, PHLAK etc.
Posts: 149

Original Poster
Rep: Reputation: 15
Cool, thanks everyone for the help!

I'm going to try this and give feedback in a couple of hours.
 
  


Reply



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
Run script each time cd is used c19h28O2 Linux - Newbie 14 05-02-2006 04:19 PM
How run script after certain idle time? nio Linux - Software 2 07-09-2005 02:05 PM
how to run command in two ttys of same computer at the same time? pwangee Linux - Networking 2 07-16-2004 01:17 PM
how to run command in two ttys of same computer at the same time? pwangee Linux - General 1 07-15-2004 01:29 PM
i want to run a script or program at boot time!! FreakboY Linux - Newbie 9 10-12-2003 08:03 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:54 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration