Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => General

PHP from scheduled task can't see folders, CLI works

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

PHP from scheduled task can't see folders, CLI works

Postby raphael75 » Mon Mar 31, 2014 10:36 am

Here is our setup:

Server A - Windows 2008 R2
PHP script on server A that copies files

Drive O: on computer B - Windows computer
contains source files

Drive V: on computer C - Debian Linux computer
target folder

We have the PHP script on server A that copies files from the mapped drive O: (on computer B) to the mapped drive V: (on computer C). The script works perfectly when run manually from the command line.

However, when the same PHP script is run from Task Scheduler it can't see the folders on drive O:. I had it echo out is_readable and is_writable on the O: drive folders, and when run from CLI they both return 1 but when run from task scheduler they return 0. I also had both echo out get_current_user and they both return the same user.

The scheduled task is set to the same user the CLI one is, and is set to run with highest privileges.

The scheduled task calls a batch file that then calls the php file. This is the contents of the batch file:

@echo off
start "" /b /separate php -f "d:\scripts\get_files.php"

If I run the batch file manually the script works, so it definitely seems to be something with the scheduled task itself, but I can't figure out what. Any help would be greatly appreciated. Thank you!
raphael75
New php-forum User
New php-forum User
 
Posts: 20
Joined: Fri Apr 06, 2012 9:54 pm

Re: PHP from scheduled task can't see folders, CLI works

Postby seandisanti » Wed Apr 02, 2014 1:38 am

It's all about context and permissions. verify the account that the script is being run with, and double check the permissions on the resources for that account. You should still try to abide by the rules of least privilege.
seandisanti
php-forum Fan User
php-forum Fan User
 
Posts: 838
Joined: Mon Oct 01, 2012 12:32 pm

Re: PHP from scheduled task can't see folders, CLI works

Postby raphael75 » Wed Apr 02, 2014 5:22 am

After a lot of testing I eventually figured it out. It turns out when a scheduled task runs another program, all mapped drives become "unavailable" to the program. You have to "re-map" the drives when the program runs in the context of a scheduled task.

I had the PHP script do:

Code: Select all
$cmd = 'net use > net_use.txt';
exec($cmd);


and every mapped drive has a status of "unavailable" when run as a scheduled task, but status of "Ok" when run directly from the command line. If I re-mapped the drives (or mapped new drives to the same paths with different drive letters) it worked.
raphael75
New php-forum User
New php-forum User
 
Posts: 20
Joined: Fri Apr 06, 2012 9:54 pm


Return to PHP coding => General

Who is online

Users browsing this forum: No registered users and 4 guests

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.