LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How to update script by itself? (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-update-script-by-itself-915299/)

sscn 11-24-2011 05:07 AM

How to update script by itself?
 
Hi all,

Is there a way to update one script by itself from another location? Say once run /root/Desktop/test.sh, I can copy /tmp/test.sh to replace it.

I know that it should work if I use one separate update script like up.sh to do this work.

But any possible update the script itself once run it? It shall be OK the test.sh script stops after run. And I get a new test.sh so that I just re-run the script.

This is a process to update one test program trigerred by a script. From the script, I can tell whether there is any updating and do updating accordingly.

Thanks a lot in advance,

sscn

mukt-kalandar 11-24-2011 05:18 AM

I think SVN tool can be used to update such script as it is used to maintain configuration files and same can be used to maintain script files as well.

KenJackson 11-24-2011 08:00 AM

I think your proposed scheme should work on Linux file systems (but not Windows' NTFS) because of the way they work. This assumes you use mv or a similar command to replace the executing script, not an edit command.

If an executing script is replaced, the old version isn't really deleted. Rather, the directory node that points to it is just changed to point to the new version while the old version continues to execute. Once execution completes, the file is closed and the file system sees there are no links to the old version so it's deleted.

However, I think it would be much better and safer to not actually replace a script but to modify a parameter file, maybe ~/.config/mystuff.conf or similar. Then have your script read the config file.

There are lots of examples of this kind of configuration file in /etc/sysconfig/.

KenJackson 11-24-2011 08:12 AM

Quote:

Originally Posted by sscn (Post 4532653)
Say once run /root/Desktop/test.sh, I can copy /tmp/test.sh to replace it.

On a security note, copying a script from /tmp/ for root to execute would be a bad idea in a multi-user environment. It may provide an opportunity for someone to modify or replace the file, maybe even accidentally, and get their substitute executed by root.

Even if you're sure there are no possible other users, there's a very remote chance that some cron job could accidentally do the same thing.

sscn 12-28-2011 12:42 PM

Where to put user program and logs?
 
Hi KenJackson,

I worked one way to update my script itself automatically.

1. Script itself can tell if there is any difference against another same name script by md5sum;
2. If there is any difference, script can call another script to run in background and exit;
3. The background copy script will sleep 3 seconds and then recover the target script automatically.

Another question is that where I can put my program if /tmp folder is not good for user test program and logs.

Thx,

sscn


All times are GMT -5. The time now is 09:13 AM.