Operating System - HP-UX
1837260 Members
2984 Online
110115 Solutions
New Discussion

Issues amending a running shell script

 
SOLVED
Go to solution
Nick Wickens
Respected Contributor

Issues amending a running shell script

I have had a query from a developer who had a running a shell script which he then amended. He claims that the fact that the script was amended whilst the original copy was running caused the script to fail. We feel that once a script is running on the system, editing the script should have no effect and its possible to even remove it.

Does anyone have any views on this. The running script called an Informix isql session and it was this step (within an IF statement) that was runnning when the original script was amended.
Hats ? We don't need no stinkin' hats !!
10 REPLIES 10
Steven E. Protter
Exalted Contributor

Re: Issues amending a running shell script

It would be nice to know what the guy did to the script.

An error message would be helpful.

Inexperienced vi users can inadvertantly put in characters that will break the script.

I recommend starting over with the original, making the changes if not too extensive and seeing if the problem recurs.

I've made a few oracle help desk calls disappear by insisting my dba do this when a script "suddenly" stops working after a modification.

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
John Poff
Honored Contributor

Re: Issues amending a running shell script

Hi,

I've wondered about this issue before, so I wrote a little test script and tried it out. I ran the script and then modified the script while it was running. The changes to the script don't get picked up in the already running process, so it looks like the shell script is read in at invocation and run from memory, which means that your developer's changes shouldn't have mattered to an existing process running that script.

This seems to match up with the man pages for sh-posix and ksh, which say the script is read at invocation. I could be wrong but this is how it looks to me.

JP
John Palmer
Honored Contributor
Solution

Re: Issues amending a running shell script

I've seen this happen before. If you run fuser against a running script file, you'll notice that the shell process keeps it open.

Depending on the size, the shell does appear to occasionally reread the script file and if you have amended it then anything could happen.

The only solution that I know is to rename the in-use script with mv, copy it to the original name then vi that file. I always use a script that does this whenever I edit a script that may be running.

Regards,
John
James R. Ferguson
Acclaimed Contributor

Re: Issues amending a running shell script

Hi Nick:

I've encounonted this too on rare occassions in a large script with plenty of functions, althought that may not related.

Having been "bitten", I always edit a copy of any script that is running and then when I've debugged the new copy, I replace the original script with it (after terminating the original).

Regards!

...JRF...
Ian Lochray
Respected Contributor

Re: Issues amending a running shell script

I am with the developer on this one. This has happened to me in the past when amending large, long running scripts. If I set the script running, make further changes and then save them, then the script can fall over with a syntax error. To avoid this I run a test copy of the script being changed.
Nick Wickens
Respected Contributor

Re: Issues amending a running shell script

Thanks everyone.

It was a long running script with a number of functions.

I wonder if the script is re-read if it gets swapped out/in when its priority decreases because its been running for a long time.?
Hats ? We don't need no stinkin' hats !!
Darren Prior
Honored Contributor

Re: Issues amending a running shell script

I've also been bitten by this, and haven't found the answer yet, though I can give you some ways to demonstrate it:

#!/bin/sh
for i in 1 2 3 4 5 6 7 8 9 0
do
echo $i
sleep 3
done

Run the script and in a separate term append about 10 chars at the end of the echo line, then save it before it finishes. The script will error when it completes.

Run the script again. This time remove the 10 chars and save before it finishes. It will complete successfully.

Edit the script and add 10 spaces to the end of the echo line. Run the script, overwrite the 10 spaces with 10 chars and save before it finishes. It will complete successfully.

regards,

Darren.
Calm down. It's only ones and zeros...
Colin Topliss
Esteemed Contributor

Re: Issues amending a running shell script

The developer is correct. A shell script is interpreted, so each line is read and then executed. It is not read entirely into memory.

You can prove this by openining two sessions. In one session, create a script with the following:

sleep 10
echo "Hello world"

Save it, and chmod it so you can run it.

On the other session, vi the script and change it (don't write it out yet though).

sleep 10
echo "Hello world again"

In the first session, start your script.
In the second session, save your changes.
You'll see your script in the first session fail:

/=> ./test
Hello world
./test[3]: syntax error at line 4 : `"' unmatched

Run your script again straight away, and it will work:

/test
Hello world again
Chris Vail
Honored Contributor

Re: Issues amending a running shell script

The theory, I suppose, is that each command in a shell script runs in its own subshell until completing successfully. So both you and your developer are both correct. You cannot amend a running _command_, but you can amend/modify a shell script that is running commands, as long as it you're amending/modifying commands that are not currently executing. A shell script is basically a series of commands run in sequence, with no command executing until the previous one terminates successfully or otherwise. AFAIK, the shell script runs in a FIFO queue.


Chris
Rodney Hills
Honored Contributor

Re: Issues amending a running shell script

Just a thought-

Could be if the script is small enough it will be run from memory only, but if it is large it may have to do re-reads on the script file.

Thus changes to script file may or may not be executed depending on what needs to be re-read from the script file.

HTH

-- Rod Hills
There be dragons...