Register
It is currently Thu Oct 30, 2014 12:13 pm

Edit script while running


All times are UTC - 6 hours


Post new topic Reply to topic  [ 5 posts ] 
Author Message
 PostPosted: Thu Apr 28, 2011 4:26 am   

Joined: Tue Apr 26, 2011 6:52 am
Posts: 4
So I read in viewtopic.php?f=15&t=999 that it's not good to edit a bash script while running, however I would like to know if it's ok if I keep the line numbers of the statements and just add something to it.

My case:

Code:
echo "Start doing task 1"
./execute_loooong_task_1
echo "Finished doing task 1"
....
echo "Start doing task n"
./execute_loooong_task_n
echo "Finished doing task n"


Now I want to change it while executing one of the tasks (a task can take days) to:

Code:
echo "Start doing task 1 at `date`"
./execute_loooong_task_1
echo "Finished doing task 1 `date`"
....
echo "Start doing task n `date`"
./execute_loooong_task_n
echo "Finished doing task n `date`"


Is that possible?

Also I would like to know if there is a way to display the estimated time instead of the two dates.


Top
 Profile  
 PostPosted: Fri Apr 29, 2011 9:42 am   
Moderator
User avatar

Joined: Wed May 03, 2006 2:05 pm
Posts: 242
Hi kirdie!

I'm pretty sure that editing the script while it's running won't really hurt. The only problem is that the script is already in memory, so your changes aren't going to be applied until the next time the script is run.

So essentially, you can make the change to the second task, while the first/long task is running, but that change won't actually make a difference in the current run of the script.

As far as timing the run goes, you might want to take a look at the 'time' command!

Something along the lines of 'time ./execute_task' should give you the total elapsed time it takes to run that task.


Top
 Profile YIM  
 PostPosted: Sat Apr 30, 2011 2:22 am   
User avatar

Joined: Sun Jun 27, 2010 12:57 am
Posts: 192
Personally I've had some very bad experiences with changing running scripts and therefor recommend to never ever ever do that. The last time is some while ago (~5 years?) so I'm not sure if things have changed in the mean while. It's just too unpredictable what will happen when you do.

About your other question regarding an estimated time, you could use something like this:
Code:
function eta { echo "$@" | awk '{ time=($4-$3)/($3/($2-$1)); printf("%dh%dm%ds\n",time/3600, time/60%60, time%60); }'; }

Then after each 'long running job' call this with 4 parameters:
o The time when your script started (in unix/epoch seconds)
o The time now (in unix/epoch seconds)
o The number of long running jobs that are finished
o The total number of long running jobs that have to be processed

So for example:
Code:
#!/bin/bash

function eta { echo "$@" | awk '{ time=($4-$3)/($3/($2-$1)); printf("%dh%dm%ds\n",time/3600, time/60%60, time%60); }'; }

start=$(date +%s)

for job in 1 2 3 4 5; do
  echo "Start Job $job/5"
  sleep $((RANDOM%10+5))
  echo "Done Job $job/5"

  eta $start $(date +%s) $job 5
done


Top
 Profile  
 PostPosted: Mon May 02, 2011 10:45 am   
User avatar

Joined: Tue Apr 27, 2010 2:28 pm
Posts: 172
Location: Czech Republic
jeo wrote:
I'm pretty sure that editing the script while it's running won't really hurt. The only problem is that the script is already in memory, so your changes aren't going to be applied until the next time the script is run.


Not true. Consider this script:
Code:
#! /bin/bash

echo 'Edit me!'

sleep 20

echo after

echo DONE

When I run it, edit it (just adding the word "dude" before the exclamation mark) while sleeping, I get this ouput:
Code:
edit me!
./1364.bash: line 6: p: command not found
after
DONE

It might depend on the editor, how it handles backups and so on, but it is dangerous.


Top
 Profile  
 PostPosted: Tue May 03, 2011 10:57 am   
Site Admin
User avatar

Joined: Sun May 15, 2005 9:36 pm
Posts: 669
Location: Des Moines, Iowa
Probably won't work while running, at least not very well as others have posted above.

I had a bash script that had a function that "sourced" in code, that could be edited on the fly as it was sourced in every time the function ran, so I was able to make changes "on the fly"..... but it was coded that way ahead of time. (actually it was jbsnakes and my code in bashbot, it allowed us to "load" and "unload" different "modules" for the irc bot). But again, it was coded with that in mind from the start.


Top
 Profile WWW  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Google [Bot], Yahoo [Bot] and 53 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP