BashScripts.org
http://bashscripts.org/forum/

Edit script while running
http://bashscripts.org/forum/viewtopic.php?f=16&t=1364
Page 1 of 1

Author:  kirdie [ Thu Apr 28, 2011 4:26 am ]
Post subject:  Edit script while running

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.

Author:  jeo [ Fri Apr 29, 2011 9:42 am ]
Post subject:  Re: Edit script while running

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.

Author:  Patsie [ Sat Apr 30, 2011 2:22 am ]
Post subject:  Re: Edit script while running

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 "[email protected]" | 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 "[email protected]" | 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

Author:  choroba [ Mon May 02, 2011 10:45 am ]
Post subject:  Re: Edit script while running

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.

Author:  crouse [ Tue May 03, 2011 10:57 am ]
Post subject:  Re: Edit script while running

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.

Page 1 of 1 All times are UTC - 6 hours
© 2000, 2002, 2005, 2007 phpBB Group • http://www.phpbb.com