replacing a script while running


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting replacing a script while running
# 1  
Old 12-03-2008
Question replacing a script while running

Hi there,

How can I run a script being sure that it runs all the way to the end even if the source file is deleted or replaced while running?
Actually, I have a script that runs daily on several servers. It replaces local files by updates from a master server. But I'd like it to replace itself too if an update is found on the master server.

Here is an example of what happens if a script is replaced while running:
Code:
~# cat script
echo statement 1
cp $0.update $0
echo statement 2
~# cat script.update
echo statement 3
cp $0.update $0
echo statement 4
~# bash script
statement 1
statement 4

How can I make sure the script runs entirely even if replaced while running?
1) The replacement file should not run immediately, this update will run the next day.
2) I would prefer not to have to create temporary files.
3) Although it would be a simple solution, it's hard for me to wait until the end of the script to do the replacement because other tasks run after files are updated.

Thanks in advance
Santiago
# 2  
Old 12-03-2008
it should be posible to replace a script while it is running withou any problem bcs the file is loaded into memory at execution time
# 3  
Old 12-03-2008
Although I'm a newbie, I think the following code clearly shows that this is not the case:
Code:
~# cat script
echo statement 1
cp $0.update $0
echo statement 2
~# cat script.update
echo statement 3
cp $0.update $0
echo statement 4
~# bash script
statement 1
statement 4

# 4  
Old 12-07-2008
Here is a solution that I found,
Tell me if this looks OK.
Code:
~# cat myscript
(( $inside )) || { inside=1; . "$0"; exit; }
echo statement 1
cp $0.update $0
echo statement 2
~# cat myscript.update
(( $inside )) || { inside=1; . "$0"; exit; }
echo statement 3
cp $0.update $0
echo statement 4
~# myscript
statement 1
statement 2
~# myscript
statement 3
statement 4

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script will keep checking running status of another script and also restart called script at night

I am using blow script :-- #!/bin/bash FIND=$(ps -elf | grep "snmp_trap.sh" | grep -v grep) #check snmp_trap.sh is running or not if then # echo "process found" exit 0; else echo "process not found" exec /home/Ketan_r /snmp_trap.sh 2>&1 & disown -h ... (1 Reply)
Discussion started by: ketanraut
1 Replies

2. Shell Programming and Scripting

Script replacing * with name of file

Hi Guys, I am new to bash and I can't figure this one out. OS: SunOS I have a file: myfile A cat of myfile shows it to contain: one * two The shell is as following: FILE="path/myfile" RESULT=$(cat "$FILE"|grep one) echo $RESULT (2 Replies)
Discussion started by: foxpaws
2 Replies

3. Shell Programming and Scripting

Shell Script [Replacing string]

My friend and I are working on a project together and we are trying to create a shell script that renames, replaces a string in a text file. The newly created file with the substitution would replace the name of the original file. And the original file would be saved as "____.txt.bak" This is... (6 Replies)
Discussion started by: jzhang172
6 Replies

4. Shell Programming and Scripting

Replacing the ipaddress using script

Hi all , I have written the script but the ip address is not getting replaced correct me i ima wrong #!/bin/bash echo "enetr ip" read $a echo $a b=`grep -o '\{1,3\}\.\{1,3\}\.\{1,3\}\.\{1,3\}' /usr/local/kalyan/mysqlup.sh` echo "$b" sed -i 's/'$b'/'$a'/g' /usr/local/kalyan/mysqlup.sh ... (1 Reply)
Discussion started by: kalyankalyan
1 Replies

5. Shell Programming and Scripting

Replacing variable value in perl script

hi I have this line in my perl script if (($cookie =~ /^cookie(.*)\/(.*)/) && ($ !~ /^cookie(10)\/(.*)|/)) { $cookienumber = "$1.$2"; } now if the result is cookie1/0, my $cookienumber would be 1.0 but if the result is cookie1/0/0 ... (5 Replies)
Discussion started by: tententen
5 Replies

6. Shell Programming and Scripting

awk script for replacing 2 strings

Hi I have written a script for automating a program. There is a string in 2 lines that needs altering from input. The 2 lines are: prepare_flexreceptor4.py -r rec_rigid.pdbqt -s TYR119_TRP312 -x rec_flex.pdbqt and prepare_flexdocking4.py -l ind.pdbqt -r rec_flex.pdbqt -s TYR119_TRP312... (3 Replies)
Discussion started by: gav2251
3 Replies

7. Shell Programming and Scripting

problem with replacing using script

Hi, I'm using MKS tool kit to execute a unix script on windows.I'm trying to replace a word in a file. I'm using sed 's/word/$replaceword/g' $OutputFile This is not working.Please advice where i'm wrong. thanks (4 Replies)
Discussion started by: ammu
4 Replies

8. Shell Programming and Scripting

replacing parameter in shell script

I have a script file as below named test1.sh sed -e 's/xxxkeys/$1/g' template1.asp > template1.txt sed -e 's/xxxkeys/$2/g' template2.asp > template2.txt sed -e 's/xxxkeys/$3/g' template3.asp > template3.txt sed -e 's/xxxkeys/$4/g' template4.asp > template4.txt I want to replace $1 with a,... (1 Reply)
Discussion started by: satgur
1 Replies

9. Solaris

Running from Shell Vs running from RC script

Hi, i have a script which need to do behave differently when run as a startup process from init.d/ rc2.d script and when run manually from shell. How do i distinguish whether my script is run by init process or by shell?? Will the command /proc/$$/psinfo | grep "myscript" work well???... (2 Replies)
Discussion started by: vickylife
2 Replies

10. AIX

Replacing tape drive in RS/6000 7025 F40 running AIX 4.2.1?

I've just landed in a situation where an old IBM RS/6000 7025 F40 running AIX 4.2.1 has had it's tape drive fail. The tape drive in question is an HP C1533A 4mm 4GB/8GB DAT drive. It repeatedly gets write failures, and my trusty techique of whacking the cleaning tape in hasn't worked. Unfortunately... (2 Replies)
Discussion started by: phaedrus
2 Replies
Login or Register to Ask a Question