jef
jef

Reputation: 4075

Edit shell script and python script while it's running

Recently, I have found the article about editing shell script while it's running.

Edit shell script while it's running

I prepared that code to reproduce the phenomenon with additional python script calling. I found echo in bash was NOT affected by editing while python script was affected.

Could someone explain this phenomenon? I have expected that all std outputs should be "modified".

#!/bin/bash
sleep 30

echo "not modified"
python my_python.py
echo "not modified"
print("not modified")
$ bash test.sh  // while sleeping, I edited test.sh and my_python.py to "modified"
not modified
 modified
not modified

Upvotes: 1

Views: 1588

Answers (1)

Tarik
Tarik

Reputation: 11209

The bash script is already loaded in memory and executing and the results will not be affected until the next run. The python script is not loaded yet and is loaded in memory after you modify it.

If you do the reverse and launch the bash script from an equivalent python script, you will get the same behavior in reverse.

EDIT 05/10/2020

As pointed out by Gordon Davisson;

"Different versions of bash do different things. Some read the file byte-by-byte as they execute it, some I think load it in 8KB blocks (not necessarily the whole file), some do even more complicated things (AIUI it can also depend on the OS they're running under). See my answer Overwrite executing bash script files. Net result: do not count on any particular behavior."

That said, the OP's OS behavior seem to indicate a complete load of the script which explain the current behavior, albeit does not guarantee it.

Upvotes: 2

Related Questions