In a DOS script, is there a way to identify if the script is being run from the CMD line or as a Scheduled Task ?

Answered In a DOS script, is there a way to identify if the script is being run from the CMD line or as a Scheduled Task ?

  • 2012年3月18日 下午 01:29
     
     

    It's all in the Title I guess ?


    Cheers, Cameron Young

所有回覆

  • 2012年3月18日 下午 01:43
     
     
    You can have a look on what all scheduled tasks got triggered and the completion status by checking the scheduled task log. But its difficult to track the other way.

    Shaba

  • 2012年3月18日 下午 08:07
     
     
    If you launch the scheduled task under a dedicated scheduler account (which is recommended in most cases) then you can examine the %UserName% environmental variable to tell how the batch file got invoked.
  • 2012年3月18日 下午 08:17
     
     已答覆

    Add named arhgument that will be set in scheduler.

    /scheduled true

    Check this in the script.


    ¯\_(ツ)_/¯

  • 2012年3月19日 上午 12:24
     
     已答覆 包含代碼

    Another solution ...

    If the parent process id of the batch is the Taskeng.exe process id we are "inside" a scheduled task ...

    @echo off setlocal

    set cmd=%~0 set okcmd=%cmd:\=\\% wmic process where "commandline like "%%%okcmd%%%" and not caption="wmic.exe"" get ParentProcessId 2>nul |findstr /v /i "ParentProcessId" 1>"%temp%\_ppid.txt" set /p ppid= <"%temp%\_ppid.txt" if not defined ppid (echo %0 run from a command line "%temp%\_log.txt" tasklist /fi "imagename eq taskeng.exe" /nh |findstr /i " %ppid% " && echo %0 run from a Scheduled Task >"%temp%\_log.txt" )



    Gastone Canali >http://www.armadillo.it

    Se alcuni post rispondono al tuo quesito (non necessariamente i miei), ricorda di contrassegnarli come risposta e non dimenticare di contrassegnare anche i post utili . GRAZIE!


  • 2012年3月19日 上午 12:34
     
     
    Gastone - nice way to do it for batch scripts.  Excellente!

    ¯\_(ツ)_/¯

  • 2012年3月23日 上午 12:14
     
     
    Gastone - nice way to do it for batch scripts.  Excellente!

    ¯\_(ツ)_/¯


    Thanks!

    Gastone Canali >http://www.armadillo.it

    Se alcuni post rispondono al tuo quesito (non necessariamente i miei), ricorda di contrassegnarli come risposta e non dimenticare di contrassegnare anche i post utili . GRAZIE!