Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: only one instance of batch  (Read 8339 times)

0 Members and 1 Guest are viewing this topic.

Blisk

    Topic Starter


    Intermediate

    Thanked: 1
    • Experience: Familiar
    • OS: Windows 7
    only one instance of batch
    « on: March 25, 2019, 02:13:09 PM »
    I have this script which prevents to run two instances of the same batch.
    But problem is because I like to set path for that lock file and whatever I do doesn't work?
     What I amd doing wrong?
    original code
    Code: [Select]
    :init
    set "started="
    2>nul (
     9>"%~f0.lock" (
      set "started=1"
      call :start
     )
    )
    @if defined started (
        del "%~f0.lock" >nul 2>nul
    ) else (
        echo Process aborted: "%~f0" is already running
        @ping localhost > nul
    )

    exit /b




    :start
    cd /d %~dp0
    start /b notepad.exe

    my changed code
    Code: [Select]
    :init
    set "started="
    SET PATH=%lockpath%;"d:\test"
    2>nul (
     9>"%lockpath%%~f0.lock" (
      set "started=1"
      call :start
     )
    )
    @if defined started (
        del "%lockpath%%~f0.lock" >nul 2>nul
    ) else (
        echo Process aborted: "%lockpath%%~f0" is already running
        @ping localhost > nul
    )

    exit /b

    :start
    cd /d %~dp0


    start /b notepad.exe

    Salmon Trout

    • Guest
    Re: only one instance of batch
    « Reply #1 on: March 25, 2019, 04:39:23 PM »
    What does variable %lockpath% contain?

    if you have killed the system PATH variable by doing this
    Code: [Select]
    SET PATH=%lockpath%;"d:\test", why do you expect ping to work?

    Do you know why it is a bad idea to name your own private variable "PATH"?

    Did you write this script yourself or copy it from somewhere?

    Blisk

      Topic Starter


      Intermediate

      Thanked: 1
      • Experience: Familiar
      • OS: Windows 7
      Re: only one instance of batch
      « Reply #2 on: March 26, 2019, 01:23:28 AM »
      I have copied that script, point of that script is to alow only one instance of batch to be run.
      with this I tried to make script to create lockfile in folder d:\test
      Code: [Select]
      SET PATH=%lockpath%;"d:\test"because now script create lockfile in folder where is runned from.

      Blisk

        Topic Starter


        Intermediate

        Thanked: 1
        • Experience: Familiar
        • OS: Windows 7
        Re: only one instance of batch
        « Reply #3 on: March 27, 2019, 01:50:32 AM »
        Any idea about path?

        Salmon Trout

        • Guest
        Re: only one instance of batch
        « Reply #4 on: March 27, 2019, 11:46:10 AM »
        Run this batch and consider what it tells you

        @echo off
        Echo (1) running Ping command
        echo ------------------------
        ping -n 1 127.0.0.1
        echo.
        set PATH=blablabla
        Echo (2) running Ping command
        echo ------------------------
        ping -n 1 127.0.0.1
        pause
         



        Blisk

          Topic Starter


          Intermediate

          Thanked: 1
          • Experience: Familiar
          • OS: Windows 7
          Re: only one instance of batch
          « Reply #5 on: March 27, 2019, 12:11:17 PM »
          (1) running Ping command
          ------------------------

          Pinging 127.0.0.1 with 32 bytes of data:
          Reply from 127.0.0.1: bytes=32 time<1ms TTL=128

          Ping statistics for 127.0.0.1:
              Packets: Sent = 1, Received = 1, Lost = 0 (0% loss),
          Approximate round trip times in milli-seconds:
              Minimum = 0ms, Maximum = 0ms, Average = 0ms

          (2) running Ping command
          ------------------------
          'ping' is not recognized as an internal or external command,
          operable program or batch file.
          Press any key to continue . . .

          Salmon Trout

          • Guest
          Re: only one instance of batch
          « Reply #6 on: March 27, 2019, 12:18:36 PM »
          What does that tell you?

          Blisk

            Topic Starter


            Intermediate

            Thanked: 1
            • Experience: Familiar
            • OS: Windows 7
            Re: only one instance of batch
            « Reply #7 on: March 27, 2019, 12:42:30 PM »
            What does that tell you?
            Sorry, really have no clue

            Salmon Trout

            • Guest
            Re: only one instance of batch
            « Reply #8 on: March 27, 2019, 01:32:36 PM »
            Did the ping command work both times?

            Salmon Trout

            • Guest
            Re: only one instance of batch
            « Reply #9 on: March 27, 2019, 02:27:15 PM »
            I told you way up above you shouldn't use a variable called PATH  or path in your scripts. Maybe you didn't read that. You didn't ask why. You don't understand anything about batch scripts, which makes it risky to run scripts you found on the web, and doubly risky to alter them when you don't know what you are doing. You could try something like this below, but maybe you would be better off actually trying to learn how to write your own scripts? or another language? People say good things about Python.

            @echo off
            setlocal enabledelayedexpansion
            echo my name is %~nx0
            echo now I look for my name using Tasklist
            set found=0
            for /f "delims=" %%A in ('tasklist /v ^| find "cmd.exe" ^| find "%~nx0"') do set /a found+=1
            echo Found this batch in Tasklist %found% time(s).
            if %found% gtr 1 (
               echo Script with same name already running
               echo Exiting...
               pause
               exit
               )
            REM rest of script
            echo If I reached here I am the only %~nx0 running
            pause


            « Last Edit: March 27, 2019, 03:06:15 PM by Salmon Trout »

            Blisk

              Topic Starter


              Intermediate

              Thanked: 1
              • Experience: Familiar
              • OS: Windows 7
              Re: only one instance of batch
              « Reply #10 on: March 27, 2019, 03:49:07 PM »
              yes you are right.
              I have intention to take class for powershell and learn that.
              I know using path which is command is not good like using the name of the batch as some exe program in script.
              I learn something but still need alot of it to learn.
              thank you

              when I test this notepad opens everytime when I run it but it should not.

              Code: [Select]
              @echo off
              setlocal enabledelayedexpansion
              echo my name is %~nx0
              echo now I look for my name using Tasklist
              set found=0
              for /f "delims=" %%A in ('tasklist /v ^| find "cmd.exe" ^| find "%~nx0"') do set /a found+=1
              echo Found this batch in Tasklist %found% time(s).
              if %found% gtr 1 (
                 echo Script with same name already running
                 echo Exiting...
                 pause
                 exit
                 )
              notepad.exe
              echo If I reached here I am the only %~nx0 running
              pause

              Salmon Trout

              • Guest
              Re: only one instance of batch
              « Reply #11 on: March 28, 2019, 09:54:25 AM »
              I thought you wanted a batch which would only allow one running instance of itself.

              Blisk

                Topic Starter


                Intermediate

                Thanked: 1
                • Experience: Familiar
                • OS: Windows 7
                Re: only one instance of batch
                « Reply #12 on: March 28, 2019, 11:34:05 AM »
                I thought you wanted a batch which would only allow one running instance of itself.

                Yes it should run only one instance of batch but I see sometimes is runned two or more.
                That's why I was searching for a script which will prevent starting another when first one already runs.
                So script which I post it do that, but problem is I can't set a path where it save that lock files.

                Salmon Trout

                • Guest
                Re: only one instance of batch
                « Reply #13 on: March 28, 2019, 11:43:24 AM »
                When I run that script, while it is still running, if you open a new command window, and run it again, the new instance exits without starting Notepad.

                Blisk

                  Topic Starter


                  Intermediate

                  Thanked: 1
                  • Experience: Familiar
                  • OS: Windows 7
                  Re: only one instance of batch
                  « Reply #14 on: March 28, 2019, 01:57:57 PM »
                  When I run that script, while it is still running, if you open a new command window, and run it again, the new instance exits without starting Notepad.
                  When I do that it opens notepad everytime.
                  I tested 3 times and opened 3 notepads.

                  Salmon Trout

                  • Guest
                  Re: only one instance of batch
                  « Reply #15 on: March 28, 2019, 02:53:21 PM »
                  Do you have tasklist.exe on your system?

                  Blisk

                    Topic Starter


                    Intermediate

                    Thanked: 1
                    • Experience: Familiar
                    • OS: Windows 7
                    Re: only one instance of batch
                    « Reply #16 on: March 29, 2019, 01:36:52 AM »
                    I have it. As far as I know all windows 7,8,10 have it.

                    Blisk

                      Topic Starter


                      Intermediate

                      Thanked: 1
                      • Experience: Familiar
                      • OS: Windows 7
                      Re: only one instance of batch
                      « Reply #17 on: April 01, 2019, 10:21:37 AM »
                      Any more ideas for this?  ???

                      Geek-9pm


                        Mastermind
                      • Geek After Dark
                      • Thanked: 1026
                        • Gekk9pm bnlog
                      • Certifications: List
                      • Computer: Specs
                      • Experience: Expert
                      • OS: Windows 10
                      Re: only one instance of batch
                      « Reply #18 on: April 01, 2019, 12:07:47 PM »
                      Yes, you asked for one instance of batch and that is what  yu got.
                      Did you want something else?
                      If so, what?
                      You are starting another Notepad.exe.
                      The batch file does not control that.
                      You did not make the batch file owner for notepad.You transferred control to notepad and the batch finished, the batch file is closed after notepad opens.
                      Keep the batch open while notepad is running.



                      Blisk

                        Topic Starter


                        Intermediate

                        Thanked: 1
                        • Experience: Familiar
                        • OS: Windows 7
                        Re: only one instance of batch
                        « Reply #19 on: April 02, 2019, 12:48:18 AM »
                        Yes, you asked for one instance of batch and that is what  yu got.
                        Did you want something else?
                        If so, what?
                        You are starting another Notepad.exe.
                        The batch file does not control that.
                        You did not make the batch file owner for notepad.You transferred control to notepad and the batch finished, the batch file is closed after notepad opens.
                        Keep the batch open while notepad is running.

                        but it is not one instance of batch, when I run it second time it opens second notepad.
                        I just like to have a script which do the same as script I posted. I only want to choose in which folder lock file is.

                        Geek-9pm


                          Mastermind
                        • Geek After Dark
                        • Thanked: 1026
                          • Gekk9pm bnlog
                        • Certifications: List
                        • Computer: Specs
                        • Experience: Expert
                        • OS: Windows 10
                        Re: only one instance of batch
                        « Reply #20 on: April 03, 2019, 03:20:21 AM »
                        but it is not one instance of batch, when I run it second time it opens second notepad.
                        I just like to have a script which do the same as script I posted. I only want to choose in which folder lock file is.
                        You are wrong.
                        The batch given by Salmon Trout  is one instance.
                        Pay attention to the detail.The batch must be the owner of the NOTEPAD invocation.
                        Your script gives control to Notepad.exe and becomes not an instance.
                        You must use the CALL testament.
                        Like this:
                        Code: [Select]
                        call notepad.exe
                        That works.  ;D

                        Blisk

                          Topic Starter


                          Intermediate

                          Thanked: 1
                          • Experience: Familiar
                          • OS: Windows 7
                          Re: only one instance of batch
                          « Reply #21 on: April 03, 2019, 07:54:35 AM »
                          no it doesn't work
                          everytime I start batch it opens another notepad.exe and I used
                          call notepad.exe

                          Geek-9pm


                            Mastermind
                          • Geek After Dark
                          • Thanked: 1026
                            • Gekk9pm bnlog
                          • Certifications: List
                          • Computer: Specs
                          • Experience: Expert
                          • OS: Windows 10
                          Re: only one instance of batch
                          « Reply #22 on: April 03, 2019, 09:29:07 AM »
                          On my system the batch file is one one instance. I can not even start another instance of it.
                          When your said  "only one instance of batch", what did you mean?
                          Will the batch file take control of another computer in another country? No.
                          Will the batch file become Lord and Master of the Windows Operating System? No.
                          Any batch file has a limited scope of control.
                          All the batch can do is prevent another instance of itself inside of its own dominion. The batch file itself is a child of CMD.EXE.

                          When I test this in Windows 7, I can not even get a commend prompt until Note Pad has closed.

                          I agree with Salmon Trout. You do not understand what  your are doing. You should learn a modern scripting language suitable to your needs. Python is a very good choice and currently there are many places where you can find tutorials.

                          I think you should read this:

                          Python – a Multiplatform Alternative to Bash/Batch Scripts?


                          The article is very good in making a point. Batch is not good for development of a powerful program that uses all of the features of your computer.

                          Instead of Python, you could learn to use Ruby. Or even VBA script.  Even javascript.
                          Batch is good for some scenarios. But it is a bad learning tool, unless you enjoy pain.  ;D

                          EDIT: This is not just what I say. Look here:
                          The Five Best Programming Languages Worth Learning.
                          Quote
                          No matter whether you’re hoping to develop websites, software, games or a bit of everything, expanding your knowledge of different programming languages is essential in today’s ever-changing industry climate.

                          Blisk

                            Topic Starter


                            Intermediate

                            Thanked: 1
                            • Experience: Familiar
                            • OS: Windows 7
                            Re: only one instance of batch
                            « Reply #23 on: April 03, 2019, 10:29:58 AM »
                            thank you for your advice and ponting suggestion what I should start learn. I will do that, first I will go to take some powershell lessons.

                            About this script as I said before when I start batch file it opens notepad, when I double click again on batch file it opens another notepad and if I dobule click third time on batch file I get third notepad open.

                            Whole idea is to prevent starting batch file until first instance runs, but this script doesn't and one I posted it does but problem is lockfile path.

                            I will give you my computer to test it but I can't.
                            I have wind 10 professional and also tested on win 7 enterprise and on both is the same, no matter how many times I double click on batch file, everytime it opens notepad, so I can open as many as I want and that means script doesn't work.

                            Salmon Trout

                            • Guest
                            Re: only one instance of batch
                            « Reply #24 on: April 03, 2019, 10:32:41 AM »
                            @echo off
                            set lockfile=%~dpnx0.lock
                            echo Lockfile is %lockfile%
                            if exist "%lockfile%" (
                               echo already running
                               exit /b
                            ) else (
                               echo Lockfile not found
                               echo Writing lockfile
                               echo !date! !time! > "%lockfile%"
                            )
                            echo.
                            echo Rest of the batch file
                            echo.
                            echo Ready to delete lockfile
                            echo and exit batch file
                            pause
                            del "%lockfile%" && echo Lockfile deleted

                            Salmon Trout

                            • Guest
                            Re: only one instance of batch
                            « Reply #25 on: April 03, 2019, 02:14:37 PM »
                            Note: the lockfile can be anywhere you like, and named anything you want:

                            set lockfile=d:\locks\batchlock.xyz

                            Also note:

                            The echo statements in the batch, and the final pause command, are there so you can see how it works, and can be removed if you want.

                            Blisk

                              Topic Starter


                              Intermediate

                              Thanked: 1
                              • Experience: Familiar
                              • OS: Windows 7
                              Re: only one instance of batch
                              « Reply #26 on: April 04, 2019, 03:05:01 AM »
                              Thank you this works I changed
                              set lockfile=d:\locks\batchlock.lock

                              I also tried if lockfile can be generated from file name with this
                              set lockfile=d:\locks\%~dpnx0.lock
                              but that didn't work
                              %~dpnx0 is for full path variable

                              I will use first option.
                              thank you again.

                              Salmon Trout

                              • Guest
                              Re: only one instance of batch
                              « Reply #27 on: April 04, 2019, 11:10:07 AM »
                              I also tried if lockfile can be generated from file name with this
                              set lockfile=d:\locks\%~dpnx0.lock
                              but that didn't work
                              %~dpnx0 is for full path variable

                              Let's break down what %~dpnx0 means

                              %0 is is the batch file name and path, also, so is %~dpnx0

                              %~ and....
                              d is the drive letter (and colon) (:)
                              p is the path
                              n is the name
                              x is the extension

                              You can combine them

                              If your file is C:\Files\Sub\MyFile.bat

                              %~d0 expands to C:
                              %~p0 expands to \Files\Sub\
                              %~n0 expands to MyFile
                              %~x0 expands to .bat

                              So to make MyFile.lock you need %~n0.lock and you can put it in folder D:\whatever\somewhere\ with D:\whatever\somewhere\%~n0.lock

                              Reminder: don't make a batch variable called PATH, in upper or lower case, unless you really want to, and you know what you are doing. It will kill lots of batch functions.




                              « Last Edit: April 04, 2019, 11:52:03 AM by Salmon Trout »

                              Blisk

                                Topic Starter


                                Intermediate

                                Thanked: 1
                                • Experience: Familiar
                                • OS: Windows 7
                                Re: only one instance of batch
                                « Reply #28 on: April 06, 2019, 01:51:04 PM »
                                thank you.
                                you are a great man I learned alot from you.