0

I am trying to call the logoff command from my build.xml. I tried several combinations in my ant target such as :

<target name="bat">
<echo>Executing batch script</echo>
  <exec dir="C:\WINDOWS\system32" executable="cmd">
    <arg value="/c"/>
    <arg value="logoff"/>
  </exec>
</target>

But everytime, I get this error : "logoff is not recognized as an internal or external command, operable program or batch file"

I don't know how to fix this issue. Thanks for helping.

EDIT:

logoff.xml

<?xml version="1.0"?>
<project name="logoff" default="off" basedir=".">
    <target name="off">
        <exec executable="cmd">
            <arg value="/c"/> 
            <arg value="logoff"/>
        </exec>
    </target>
</project>

logoff_xml.bat:

@echo off
set ANT_HOME=W:\lib\org.apache.ant
set JAVA_HOME=C:\Program Files (x86)\Java\jdk1.6.0_24 
set PATH=%PATH%;%ANT_HOME%\bin 
set BUILD_PATH=W:\logoff.xml 
set ANT=call ant -buildfile %BUILD_PATH% 
%ANT% 
pause

echo %PATH% (with buid.xml bat target)

[exec] C:\Outils\GPS_510\bin;C:\Outils\GNATPRO_6.1.2\bin;C:\WINDOWS\system3
2;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\Outils\Python25;C:\MinGW\bin;C:\Program
Files (x86)\IBM\RationalSDLC\common;C:\Program Files (x86)\IBM\RationalSDLC\Cle
arCase\bin;C:\Program Files (x86)\doxygen\bin;C:\Program Files (x86)\Citrix\Syst
em32\Citrix\IMA;C:\Program Files (x86)\Citrix\System32\Citrix\IMA\Subsystems;C:\
WINDOWS\System32\Citrix\IMA;C:\Program Files (x86)\Citrix\system32;C:\Program Fi
les (x86)\commonfiles\Citrix\System32\;C:\MinGW\msys\1.0\bin;c:\outils\cygw
in\bin;C:\WINDOWS\system32\WindowsPowerShell\v1.0;C:\Program Files (x86)\commonfiles
\Citrix\System32;C:\Program Files (x86)\Java\jre6\bin;lib\org.apache.an
t\bin

EDIT : When I launch logoff with Ant, I got :

[exec] Disconnecting
[exec] "logoff.exe is not recognized as an internal or external command, operable program or batch file"
Lycris
  • 21
  • 6
  • why would a build log off? – thekbb Sep 16 '13 at 14:15
  • Because, I'm running an Ant script periodically on a remote computer. I need to log off the remote session after build to avoid any conflict with the others. – Lycris Sep 16 '13 at 15:16
  • Right... so what mechanism runs the ant build remotely? How are you checking it's status? I'm pretty sure you're going to have a better time doing the log off there than in the ant script itself. – thekbb Sep 16 '13 at 18:20
  • I'm calling mstsc command since Jenkins. In the rdp file in parameter, I launch a batch script which calls the build. But I want to run the build periodically during the night for instance. The problem is why ant cannot find the logoff command whereas everything is in the PATH variable – Lycris Sep 17 '13 at 06:18
  • Is there a reason you don't run the jenkins slave service on the machine you're wanting to run the build on? It would just work, you wouldn't have to mess with logging off. – thekbb Sep 17 '13 at 14:22
  • I'm running jenkins slave agent on another computer exactly. I have some restrictions. But again, the problem is not here. When I launch a logoff from the console it's working and when I work with Ant it cannot be recognized, whereas simple "echo" works . Let's be clear I'm working in the remote session, no relation with Jenkins – Lycris Sep 17 '13 at 15:25
  • 2
    wait... you have a jenkins master, a jenkins slave and then you're running the ant build on a third computer? I truly believe your real solution will be to not do crazy stuff. – thekbb Sep 17 '13 at 15:49
  • Everything works truly (except logoff)... I tried the ant logoff in another computer and same problem. – Lycris Sep 18 '13 at 06:47
  • well the c:\windows\system32 is missing in PATH – Dusan Plavak Sep 18 '13 at 14:57
  • I got the same error, then I removed JAVA_HOME and problem was solved... – Dusan Plavak Sep 18 '13 at 14:59

3 Answers3

1

I cannot possibly imagine why anyone would ever do this in a build script.

Please don't do this.

But if you do, don't exec cmd.exe - call logoff.exe directly

<project name="logoff" default="logoff" basedir=".">
    <target name="logoff">
        <exec executable="logoff" />
    </target>
</project>

It's a very safe assumption that logoff.exe is on %PATH%, if for some reason it's not, set a property to get the absolute path to logoff.exe;

<project name="logoff" default="logoff" basedir=".">
    <property name="system32.dir" location="C:\Windows\System32" />        

    <target name="logoff">
        <exec executable="${system32.dir}\logoff" />
    </target>
</project>

I feel dirty for having typed this.

thekbb
  • 7,668
  • 1
  • 36
  • 61
0

You should remove that dir atribute.... This is working for me:

    <?xml version="1.0" encoding="UTF-8" ?>
    <project name="logoff" default="default" basedir=".">
        <target name="default">
        <exec executable="cmd">
            <arg value="/c"/>
            <arg value="logoff"/>
        </exec> 
    </target>
    </project>

EDIT:

@echo off
set ANT_HOME=W:\lib\org.apache.ant
set PATH=%PATH%;%ANT_HOME%\bin 
set BUILD_PATH=W:\logoff.xml 
set ANT=call ant -buildfile %BUILD_PATH% 
%ANT% 
pause
Dusan Plavak
  • 4,457
  • 4
  • 24
  • 35
0

Try using shutdown /l /f instead, as suggested here:

http://answers.microsoft.com/en-us/windows/forum/windows_7-security/how-to-force-logoff-from-command-line/8e7507e0-12c8-4ac7-8d5e-a1917e4af997

This might also help (description how to do it in java) as ant uses similar (to java) arguments:

Shutdown Windows with Java

Community
  • 1
  • 1
pkk
  • 3,691
  • 2
  • 21
  • 29