Jump to content

Compiled script doing self update on network share, IMPOSSIBLE?


Go to solution Solved by MHz,

Recommended Posts

I'm not sure if this possible, but who knows that I'm over thinking (again):

I have a compiled script that I put on network share folder (it's stored at FreeBSD machine with SMB protocol).

This script is accessible by many  users on many machines that connected to the LAN.

When the script run, how can it know how many instance is it running on all of those machines?

I dont need to know who and which machine is running it, but I need to know is there more than 1 instance

running it now (on whichever machine, I dont care)?

In other word, this script would like to know, is it in used (openned/executed by any machines) except by himself right now?

I'm asking this because I would like to do self update, and I cant do it when there is a user access/execute it.

Something like:

If NOT _FileInUsed(@ScriptFullPath) then
     _self_update()
Endif

 

IMPOSSIBLE?

Link to comment
Share on other sites

Maybe you could add an ini file with some sections. When the script starts up, it modifies the ini the following way:

[USER_1]
Active=1

Every user should have his own script (or at least you could provide another ini file with the username of the computer, so you don't have to recompile it 200 times).

Before the script stopped, you could write it again:

[USER_1]
Active=0

So, on the main server you can check if everyones script is closed, or maybe you can add a global disable function while updating.

[ It's not exactly the same as you told, but a bypass way :) ]

Edited by Unc3nZureD
Link to comment
Share on other sites

let's split it into two subjects, shall we.

1) the part of knowing who is running your script is actually very easy.

make sure your script is able to write to a share on the BSD machine.

add a beginning line to your script, which creates a file in that share, that file name is the hostname of the machine running the script. the content of the file is not important, can be empty file.

add an ending line to your script, deleting the same file.

you get the idea, yeah? the point is that you have a list of files, indicating the current machines using the script. if no files exist, then no-one is using your script. you can modify the concept as you see fit.

 

2) the part of self update is even easier:

the script is located only on the server, right? so you have just one place to update, and you can update only when there are no files as described above.

so write an updater script, that loops a files check until no files exist, then replaces the existing script with the new version.

 

edit: damn, i just paused writing the post to pet the cat... he beat me to it

Edited by orbs

Signature - my forum contributions:

Spoiler

UDF:

LFN - support for long file names (over 260 characters)

InputImpose - impose valid characters in an input control

TimeConvert - convert UTC to/from local time and/or reformat the string representation

AMF - accept multiple files from Windows Explorer context menu

DateDuration -  literal description of the difference between given dates

Apps:

Touch - set the "modified" timestamp of a file to current time

Show For Files - tray menu to show/hide files extensions, hidden & system files, and selection checkboxes

SPDiff - Single-Pane Text Diff

 

Link to comment
Share on other sites

If NOT _FileInUsed(@ScriptFullPath) then
     _self_update()
Endif

I do something like that

but

1. You must separate updater.exe

2. You must close mainprogram.exe on your machine

3. updater.exe now can check _FileInUsed(mainprogram.exe)

Edited by mlipok

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

Spoiler

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind. 

My contribution (my own projects): * Debenu Quick PDF Library - UDF * Debenu PDF Viewer SDK - UDF * Acrobat Reader - ActiveX Viewer * UDF for PDFCreator v1.x.x * XZip - UDF * AppCompatFlags UDF * CrowdinAPI UDF * _WinMergeCompare2Files() * _JavaExceptionAdd() * _IsBeta() * Writing DPI Awareness App - workaround * _AutoIt_RequiredVersion() * Chilkatsoft.au3 UDF * TeamViewer.au3 UDF * JavaManagement UDF * VIES over SOAP * WinSCP UDF * GHAPI UDF - modest begining - comunication with GitHub REST APIErrorLog.au3 UDF - A logging Library * Include Dependency Tree (Tool for analyzing script relations) * Show_Macro_Values.au3 *

 

My contribution to others projects or UDF based on  others projects: * _sql.au3 UDF  * POP3.au3 UDF *  RTF Printer - UDF * XML.au3 UDF * ADO.au3 UDF SMTP Mailer UDF * Dual Monitor resolution detection * * 2GUI on Dual Monitor System * _SciLexer.au3 UDF * SciTE - Lexer for console pane

Useful links: * Forum Rules * Forum etiquette *  Forum Information and FAQs * How to post code on the forum * AutoIt Online Documentation * AutoIt Online Beta Documentation * SciTE4AutoIt3 getting started * Convert text blocks to AutoIt code * Games made in Autoit * Programming related sites * Polish AutoIt Tutorial * DllCall Code Generator * 

Wiki: Expand your knowledge - AutoIt Wiki * Collection of User Defined Functions * How to use HelpFile * Good coding practices in AutoIt * 

OpenOffice/LibreOffice/XLS Related: WriterDemo.au3 * XLS/MDB from scratch with ADOX

IE Related:  * How to use IE.au3  UDF with  AutoIt v3.3.14.x * Why isn't Autoit able to click a Javascript Dialog? * Clicking javascript button with no ID * IE document >> save as MHT file * IETab Switcher (by LarsJ ) * HTML Entities * _IEquerySelectorAll() (by uncommon) * IE in TaskSchedulerIE Embedded Control Versioning (use IE9+ and HTML5 in a GUI) * PDF Related:How to get reference to PDF object embeded in IE * IE on Windows 11

I encourage you to read: * Global Vars * Best Coding Practices * Please explain code used in Help file for several File functions * OOP-like approach in AutoIt * UDF-Spec Questions *  EXAMPLE: How To Catch ConsoleWrite() output to a file or to CMD *

I also encourage you to check awesome @trancexx code:  * Create COM objects from modules without any demand on user to register anything. * Another COM object registering stuffOnHungApp handlerAvoid "AutoIt Error" message box in unknown errors  * HTML editor

winhttp.au3 related : * https://www.autoitscript.com/forum/topic/206771-winhttpau3-download-problem-youre-speaking-plain-http-to-an-ssl-enabled-server-port/

"Homo sum; humani nil a me alienum puto" - Publius Terentius Afer
"Program are meant to be read by humans and only incidentally for computers and execute" - Donald Knuth, "The Art of Computer Programming"
:naughty:  :ranting:, be  :) and       \\//_.

Anticipating Errors :  "Any program that accepts data from a user must include code to validate that data before sending it to the data store. You cannot rely on the data store, ...., or even your programming language to notify you of problems. You must check every byte entered by your users, making sure that data is the correct type for its field and that required fields are not empty."

Signature last update: 2023-04-24

Link to comment
Share on other sites

  • Solution

You may not be able to delete or overwrite a executable file in use, but you can move it. And so the main concern is not the processes of the file but of the file itself.

This is a simple example of how you can do an update when the main script sees the update file and then does the moving while the main script is still running. More comments are with the example.

#cs Information about this self update example
    Known files in example are Self.old, Self.exe and Self.new
    Self.exe is the current file to be used
    Self.old is the old version that is deleted when not in use
    Self.new is the new version to be replace the current version in same directory when detected
    Self.lock is a flag file to help prevent other scripts trying to do the update at the same time
    To test:
        Compile script to Self.exe and place in the shared folder
        Run Self.exe in the shared folder
        Compile update script to Self.exe and rename to Self.new
        Place Self.new in the shared folder
        Run Self.exe again and it should detect Self.new and do the FileMoving in _SelfUpdate()
        Run Self.exe again and you may be running the new version of Self.exe
    Note:
        Self.old will be deleted when file in use does not exist and done when Self.exe is initially executed
        Self.exe will be updated when Self.new is detected and when Self.old can be created or overwritten
        Shared folder can be a local folder
#ce

#OnAutoItStartRegister "_SelfUpdate"

MsgBox(0, @ScriptName, 'Version 1.0')

Func _SelfUpdate()
    Local $message
    ; remove old file if able
    If FileExists(@ScriptDir & '\Self.old') And FileDelete(@ScriptDir & '\Self.old') Then
        $message &= 'Deleted ' & @ScriptDir & '\Self.old' & @CRLF
    EndIf
    ; update to new file if able
    If FileExists(@ScriptDir & '\Self.new') And Not FileExists(@ScriptDir & '\Self.lock') Then
        $message &= @ScriptDir & '\Self.new found' & @CRLF
        ; create a lock file, actually a flag file to alert other scripts to let me do this
        If FileWriteLine(@ScriptDir & '\Self.lock', 'I am updating') Then
            ; exe to old
            If FileMove(@ScriptFullPath, @ScriptDir & '\Self.old', 1) Then
                $message &= 'Moved ' & @ScriptFullPath & ' to ' & @ScriptDir & '\Self.old' & @CRLF
                ; new to exe
                If FileMove(@ScriptDir & '\Self.new', @ScriptDir & '\Self.exe') Then
                    $message &= 'Moved ' & @ScriptDir & '\Self.new to ' & @ScriptDir & '\Self.exe' & @CRLF
                    $message &= @CRLF & 'Update may be available on next execution of Self.exe'
                Else
                    ; recover if failed
                    FileMove(@ScriptDir & '\Self.old', @ScriptDir & '\Self.exe')
                    $message &= 'Recovered ' & @ScriptDir & '\Self.old to ' & @ScriptDir & '\Self.exe'
                EndIf           
            EndIf
            FileDelete(@ScriptDir & '\Self.lock')
        EndIf
    EndIf
    If $message Then MsgBox(0, @ScriptName, $message)
EndFunc
Link to comment
Share on other sites

I use this funtion to update the script

Func _selfUpdate($delay = 0)
    If Not @Compiled Then Return -1
    Local $tmpBatFH, $internetDelay = 2, $appID = @ScriptName, $SDATA, $SDELAY = "IF %TIMER% GTR " & $delay & " GOTO DELETE", $SFILENAME = @ScriptName, $SIMAGENAME = "IMAGENAME"
    Local $scriptPath = FileGetShortName(@ScriptFullPath)
    Local $REMOTEPATH = "m:\DATEN\common\"
;~  Local $localPath = @ScriptDir & "\"
    Local $scriptName = @ScriptName
    Local $remoteVersion = FileGetVersion($REMOTEPATH & $scriptName)
    Local $localVersion = FileGetVersion(@ScriptFullPath)
    If _VersionCompare($remoteVersion, $localVersion) = 1 Then
        FileCopy($REMOTEPATH & $scriptName, @ScriptFullPath & ".new")
        $SDATA = 'SET TIMER=0' & @CRLF _
                 & ':START' & @CRLF _
                 & 'PING -n ' & $internetDelay & ' 127.0.0.1 > nul' & @CRLF _
                 & $SDELAY & @CRLF _
                 & 'SET /A TIMER+=1' & @CRLF _
                 & @CRLF _
                 & 'TASKLIST /NH /FI "' & $SIMAGENAME & ' EQ ' & $appID & '" | FIND /I "' & $appID & '" >nul && GOTO START' & @CRLF _
                 & 'GOTO DELETE' & @CRLF _
                 & @CRLF _
                 & ':DELETE' & @CRLF _
                 & 'TASKKILL /F /FI "' & $SIMAGENAME & ' EQ ' & $appID & '"' & @CRLF _
                 & 'DEL "' & $scriptPath & '"' & @CRLF _
                 & 'IF EXIST "' & $scriptPath & '" GOTO DELETE' & @CRLF _
                 & @CRLF & 'MOVE "' & @ScriptFullPath & '.new" "' & @ScriptFullPath & '"' & @CRLF _
                 & 'CALL "' & @ScriptFullPath & '" UPDATE' & @CRLF _
                 & 'GOTO END' & @CRLF _
                 & @CRLF _
                 & ':END' & @CRLF _
                 & 'DEL "' & @TempDir & "\" & $SFILENAME & ".bat" & '"' & @CRLF _
                 & 'exit'

        $tmpBatFH = FileOpen(@TempDir & "\" & $SFILENAME & ".bat", 2)
        If $tmpBatFH = -1 Then
            Return SetError(2, 0, 0)
        EndIf
        FileWrite($tmpBatFH, $SDATA)
        FileClose($tmpBatFH)
        Return Run(@TempDir & "\" & $SFILENAME & ".bat", @TempDir, @SW_HIDE)
    EndIf
EndFunc   ;==>_selfUpdate

Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...