Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    New Coder
    Join Date
    Mar 2009
    Posts
    74
    Thanks
    3
    Thanked 3 Times in 3 Posts

    problems with exec/shell_exec/etc.

    I have a Web page running on a Windows 2008 server that creates a scheduled task on the server:

    PHP Code:
    $fname 'd:\scripts\labor\scheduled\repost_labor.bat';
    $cmd '';
    $cmd 'schtasks /create /sc once /ru <username> /rp <pwd> /tn "repost_labor" /tr "' $fname '" /st ' $cur_dt->format('H:i:s');

    exec($cmd$out$retval); 
    It runs perfectly. However, when I try this:

    PHP Code:
    $cmd 'schtasks.exe /query /tn "repost_labor" > d:\scripts\labor\scheduled\repost_labor.txt';
    exec($cmd$out$retval); 
    it refuses to execute.

    I've tried setting permissions on cmd.exe, schtasks.exe, the folder that the file the scheduled task is executing, and all files inside that folder to have read & execute permissions for:

    everyone
    authenticated users
    nt authority\iusr
    domain users
    network service
    domain users
    users
    service

    If I print_r($out) I always get an empty array. The d:\scripts\labor\scheduled\repost_labor.txt file gets created but there is never anything in it (0 bytes).

    I've also tried all the PHP program execution functions (exec, shell_exec, system, etc.) and they all gave the same result (empty array, 0-byte file).

    Nothing worked.

    I don't understand why schtasks /create works but schtasks /query doesn't. Any help would be greatly appreciated. Thank you!
    Last edited by Raphael; 02-13-2013 at 04:12 PM.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    What's the $retval giving you back? I'd assume that schtasks would return 0 on success.
    The entire purpose has me a bit confused though. You schedule to run once notepad and launch a file, which you then write to with the information about the task. That seems a bit pointless IMO.
    The $out should contain the output of the call, which is nothing (erm, I think its nothing). When you've redirected the output to the file, the stdout won't have anything in it to present back to PHP. So that cannot be used to determine if it was successful.
    0-byte file would say either it doesn't find the task, or that there is no permissions to write. In either case I would actually expect that the $out would contain that, but its possible that you may need to redirect the stderr to the stdout as well.

    Windows web servers are such a nightmare to work with :/
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #3
    New Coder
    Join Date
    Mar 2009
    Posts
    74
    Thanks
    3
    Thanked 3 Times in 3 Posts
    Quote Originally Posted by Fou-Lu View Post
    What's the $retval giving you back? I'd assume that schtasks would return 0 on success.
    The entire purpose has me a bit confused though. You schedule to run once notepad and launch a file, which you then write to with the information about the task. That seems a bit pointless IMO.
    The $out should contain the output of the call, which is nothing (erm, I think its nothing). When you've redirected the output to the file, the stdout won't have anything in it to present back to PHP. So that cannot be used to determine if it was successful.
    0-byte file would say either it doesn't find the task, or that there is no permissions to write. In either case I would actually expect that the $out would contain that, but its possible that you may need to redirect the stderr to the stdout as well.

    Windows web servers are such a nightmare to work with :/
    I apologize, I copied & pasted the wrong line for the $fname. It's supposed to be repost_labor.bat, not repost_labor.txt. I corrected it above. It should've been:

    PHP Code:
    $fname 'd:\scripts\labor\scheduled\repost_labor.bat'
    This is how $out looks:
    Code:
    $out => Array (0)
    (
    )
    and $retval has a value of:

    Code:
    1
    When I run the .bat file from the command line it works.

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    It sounds like a permission issue. Try redirecting the stderr as well: schtasks.exe /query /tn "repost_labor" 1> d:\scripts\labor\scheduled\repost_labor.txt 2>&1 to see if that puts any errors in the file (I think I've got the redirection correct :P).
    The return value of 1, is likely an error. Apps should be returning 0 on success, but there's nothing error message wise to tell what's going on.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • Users who have thanked Fou-Lu for this post:

    Raphael (02-13-2013)

  • #5
    New Coder
    Join Date
    Mar 2009
    Posts
    74
    Thanks
    3
    Thanked 3 Times in 3 Posts
    Quote Originally Posted by Fou-Lu View Post
    It sounds like a permission issue. Try redirecting the stderr as well: schtasks.exe /query /tn "repost_labor" 1> d:\scripts\labor\scheduled\repost_labor.txt 2>&1 to see if that puts any errors in the file (I think I've got the redirection correct :P).
    The return value of 1, is likely an error. Apps should be returning 0 on success, but there's nothing error message wise to tell what's going on.
    Great idea about the error redirection!

    I tried it and got this error:

    Code:
    ERROR: The system cannot find the path specified.
    Which is very strange. To narrow down which file it couldn't find (schtasks.exe or repost_labor.txt) I took out the file redirection part so it was just trying to execute this:

    Code:
    c:\windows\system32\schtasks.exe /query /tn "repost_labor"
    and it gave the same error.

    How is it possible that it can't "find" schtasks.exe at this point in the program to do a query and just a few lines later it uses almost the same command to create a scheduled task?

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    It doesn't make sense. You can verify the path on the server by executing where schtasks to verify the path. The only difference otherwise is you specify one as an .exe and the other you do not, but neither should make a difference.
    Are you sure that the second error reporting isn't the first? Did you delete the output file after the first run then try again to verify the same error? You'll want to make sure you're not running cached as well, so just give it a random querystring on the request.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #7
    New Coder
    Join Date
    Mar 2009
    Posts
    74
    Thanks
    3
    Thanked 3 Times in 3 Posts
    I tried the where schtasks command and it said c:\windows\system32\schtasks.exe

    Here's something interesting. I tried simply putting this in:

    PHP Code:
    $cmd 'schtasks /query /?';
    echo 
    exec($cmd$out$retval);
    print_ri($out); 
    and it echo'ed back:

    Code:
    $out => Array (45)
    (
    |    ['0'] = String(0) ""
    |    ['1'] = String(57) "SCHTASKS /Query [/S system [/U username [/P [password]]]]"
    |    ['2'] = String(70) "         [/FO format | /XML [xml_type]] [/NH] [/V] [/TN taskname] [/?]"
    |    ['3'] = String(0) ""
    |    ['4'] = String(12) "Description:"
    |    ['5'] = String(66) "    Enables an administrator to display the scheduled tasks on the"
    |    ['6'] = String(27) "    local or remote system."
    |    ['7'] = String(0) ""
    |    ['8'] = String(15) "Parameter List:"
    |    ['9'] = String(67) "    /S    system         Specifies the remote system to connect to."
    |    ['10'] = String(0) ""
    |    ['11'] = String(57) "    /U    username       Specifies the user context under"
    |    ['12'] = String(59) "                         which schtasks.exe should execute."
    |    ['13'] = String(0) ""
    |    ['14'] = String(61) "    /P    [password]     Specifies the password for the given"
    |    ['15'] = String(68) "                         user context. Prompts for input if omitted."
    |    ['16'] = String(0) ""
    |    ['17'] = String(61) "    /FO   format         Specifies the format for the output."
    |    ['18'] = String(56) "                         Valid values: TABLE, LIST, CSV."
    |    ['19'] = String(0) ""
    |    ['20'] = String(68) "    /NH                  Specifies that the column header should not"
    |    ['21'] = String(60) "                         be displayed in the output. This is"
    |    ['22'] = String(53) "                         valid only for TABLE format."
    |    ['23'] = String(0) ""
    |    ['24'] = String(54) "    /V                   Displays verbose task output."
    |    ['25'] = String(0) ""
    |    ['26'] = String(61) "    /TN   taskname       Specifies the task name for which to"
    |    ['27'] = String(68) "                         retrieve the information, else all of them."
    |    ['28'] = String(0) ""
    |    ['29'] = String(65) "    /XML  [xml_type]     Displays task definitions in XML format."
    |    ['30'] = String(0) ""
    |    ['31'] = String(87) "                         If xml_type is ONE then the output will be one valid XML file."
    |    ['32'] = String(0) ""
    |    ['33'] = String(75) "                         If xml_type is not present then the output will be"
    |    ['34'] = String(0) ""
    |    ['35'] = String(71) "                         the concatenation of all XML task definitions."
    |    ['36'] = String(0) ""
    |    ['37'] = String(52) "    /?                   Displays this help message."
    |    ['38'] = String(0) ""
    |    ['39'] = String(9) "Examples:"
    |    ['40'] = String(19) "    SCHTASKS /Query"
    |    ['41'] = String(22) "    SCHTASKS /Query /?"
    |    ['42'] = String(49) "    SCHTASKS /Query /S system /U user /P password"
    |    ['43'] = String(61) "    SCHTASKS /Query /FO LIST /V /S system /U user /P password"
    |    ['44'] = String(36) "    SCHTASKS /Query /FO TABLE /NH /V"
    )
    $retval was 0

    I also tried:

    PHP Code:
    $cmd 'schtasks.exe /query /s <server name> /u <username> /p <password> /tn "repost_labor" > test.txt >2>&1';
    echo 
    exec($cmd$out$retval);
    print_ri($out); 
    After running it, I got an empty array for $out, $retval was 1, and test.txt said:

    ERROR: Access is denied.

    I have practically every account set to have read & execute access to cmd.exe and schtasks.exe. Could there be another file involved, or a different user account? It's a Windows 2008 server with IIS.

  • #8
    New Coder
    Join Date
    Mar 2009
    Posts
    74
    Thanks
    3
    Thanked 3 Times in 3 Posts
    I think I got it!

    I had to specify the domain\username like this:

    $cmd = 'schtasks.exe /query /s <server name> /u <domain>\<username> /p <password> /tn "repost_labor" > test.txt >2>&1';
    echo exec($cmd, $out, $retval);
    print_ri($out);

    When I put that in it worked!

    Thanks so much for your help, Fou-Lu, especially with the idea and code to output the STDERR.

  • #9
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Yeppers you betcha.
    Been working in networked environments for so long I don't remember the last time I haven't typed in domain\user lol.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •