I want to run php script through windows task scheduler.
Following is the content of .bat file -
cd C:\test_folder
"C:\Program Files (x86)\PHP\v5.6\php.exe" -f test.php
This .bat file executes successfully if I run it manually but fails in task scheduler showing last run result as 0xFF.
I have multiple versions of PHP installed and have to run test.php on older version. That is why I have to mention full path of php.exe even though PHP environment variable is set.
Do any of the folders have restricted access? Try setting it up so that the scripts runs with highest privileges or that the account it's running under has proper access to the folders required and see if that fixes it.
Also, try dropping the "-f".
Related
I have a Python script which is encoding a video and then calling a shell script which uploads the new video to dropbox. It works fine from the command line but I needed to make it so others could execute it so I have a PHP script calling the python script.
I don't want the PHP script to run forever (it takes 15-30 mins for it to complete), I just want it to kick off the python script and be done. I figured out what I need to make that happen and like I said it works on the command line. But when it is called via PHP, the video encodes but the file never uploads. I can see the dropbox script was kicked off and is listed as a process using some percent of CPU, that percent never changes, it seems stuck/dead.
the command looks like this, being run using cmd()
script.py -options &>/logs/phptopython.log &
The shell script is kicked off using Popen
Any suggestions?
thanks
It sounds like this could be a permissions issue. Double check the permissions on the directory to which you are trying to upload the video. If you are on Linux you can modify the permissions on that directory like this:
chmod 755 /path/to/dir
This gives the file owner read, write and execute permissions (7). The group and other users get read and execute permissions (5).
Apache is likely running as a different user than when you run the command yourself in bash. A quick test to see if it's a permission issue would be to grant 777 on that directory. I wouldn't leave it that way though – it'd just be a way to quickly identify if permissions are the issue.
If the script works with 777 permissions, you could either change the owner of the directory to the user Apache runs as or add the Apache user to the directory's group and grant the group write permisssions.
Edit:
I just noticed you said you use cmd(), so I'm guessing you are on Windows. My comments might still be relevant but the chmod command won't work on Windows.
I want to run Python script from PHP. The script's syntax is good, when I run it from command line it generates an image file next to it. When I run the PHP file (contains Python exec) from command line it generates an image file. It's good. The problem is: when I use browser and run PHP it isn't generate any image file. I use Xubuntu OS.
Does the script that you are trying to run requires sudo privileges? If so, you will need to add the user "www-data" or web user with the command (full path) to the sudoers file (Ubuntu /etc/sudoers or execute visudo command).
More info here https://help.ubuntu.com/community/Sudoers
Another thing to look at is at memory_limit in your php.ini directive. You might be using all memory allowed to use.
The problem was the matplotlib backend setting. I changed to 'Agg' and the plot function worked fine.
I need to run a php script at midnight every night on my server. On a linux system I'd set up a cron job, but I'm stuck with a windows system.
I know I have to set up a task using the windows task scheduler, and that the task will need to run a .bat file which in turn will run the php file, but I'm stuck trying to write the .bat file.
What I currently have is:
#echo off
REM this command runs the nightly cron job
start "C:\Program Files (x86)\PHP\v5.3\php.exe" -f C:\inetpub\wwwroot\sitename\crons\reminder-email.php
But when I try to manually run the .bat file to test it, I get a windows alert saying
"Windows cannot find '-f'. Make sure you typed the name correctly, and then try again.
What have I missed?
The START command optionally accepts a title for the created window as its first argument; in this case, it thinks that C:\Program Files (x86)\PHP\v5.3\php.exe is the title to display and -f (the second argument) is the executable you want to run.
You can therefore fix this by providing a placeholder title, e.g.
start "email reminder task" "C:\Program Files (x86)\PHP\v5.3\php.exe" -f C:\inetpub\wwwroot\sitename\crons\reminder-email.php
Or, preferably, you can ditch the START command altogether (you aren't using any of its unique facilities) and just run PHP directly:
"C:\Program Files (x86)\PHP\v5.3\php.exe" -f C:\inetpub\wwwroot\sitename\crons\reminder-email.php
Actually, you don't even need a batch-file.
You can run the php-script from the task scheduler.
Just let the task scheduler run php.exe and set the location of the php-file as the argument of the task.
Can I suggest a small change.
echo off
REM This adds the folder containing php.exe to the path
PATH=%PATH%;C:\Program Files (x86)\PHP\v5.3
REM Change Directory to the folder containing your script
CD C:\inetpub\wwwroot\sitename\crons
REM Execute
php reminder-email.php
PS. Putting Apache,MySQL or PHP in Program Files is a bad idea. Dont use windows folders with spaces in their names.
How about this?
set php="C:\Program Files (x86)\PHP\v5.3\php.exe"
%php% -f C:\inetpub\wwwroot\sitename\crons\reminder-email.php
Sadly this took close to 10 hours to figure out. Some protocols such as WinRM and PSEXEC must pass the logged in user account regardless of the credentials supplied (that or PHP overrides whatever you type in). At any rate, to get PSEXEC, WinRM or just kicking off batch files that connect to remote computers, you will need to change the IIS run as account to a user with rights to those resources (service account). I realize this is a huge security hole, but that is by design. PHP is secure so that it can't be hacked easily, you have to override their security by specifying a run as account. Not the same thing as your app pool account - although your IIS credentials can use your app pool account.
Try like this guys!
cd E:\xampp\htdocs\my-project
E:
php artisan schedule:run
I created a script using PHP CLI that I would like to have cd me into a directory based upon my command line input.
While I can get the PHP execution commands to echo output (e.g. echo ls -al), I cannot get them to run cd.
I have searched a lot online to find the solution, but have come up empty.
You can't use cd as it would run in a subshell, and the changed working directory would be lost when you returned to PHP before issuing the next command.
Use chdir instead.
You need to run chdir from php, running cd from exec, system, shell_exec etc. only change directories in subprocesses called by php, every new system call will start in php current working directory.
I have installed localhost/server in my machine and I need to run a php script using windows schedule task. how do I add path in "Actions" tab in schedule task / cofigure the script to run for particular period?
Locate the php.exe executable on your system and pass it the name of the script file using the -f parameter.
Example:
C:\Xampp\php\php.exe -f C:\Xampp\htdocs\my_script.php
Reference:
Introduction to using PHP on the command line
PHP command line options
Here's how did it.
Windows scheduler -> create a new task -> action tab -> Edit
At least I tried out some suggestions but it doesn't work so I tried this.
Use a bat file and schedule to execute that bat file.
For example in the bat file executephp.bat, write this
c:\xampp\php\php.exe -f c:\xampp\htdocs\do_something.php
save that bat file that contains that line.
Go to windows scheduler and create a new task and in action tab, browse to point that executephp.bat and for start in -> direct to the directory u have that executephp.bat.
For example if u save the file under C:\xampp\htdocs put that C:\xampp\htdocs in the start in.
Remember to invoke the script even when the user is not logged on.
Everything is set and it will execute without problem.
You can use PHP Command Line to execute it rather then trying to load it through the browser.
Under the actions tab, create a new action and:
Program/Script: Point to your PHP.exe file
Add Arguments: -f /path/to/php/file.php
Optionally you can make it start in the script's directory as well.
create Schedule task
Scheduler->Actions->Edit Action
if you have php file
Program/script: powershell
Add arguments: curl http://localhost/demo/cron.php
if you have MVC/CMS URL
Program/script: powershell
Add arguments: curl http://localhost/demo/controller/method
I just wanted to leave what i had to do to get this working for server 2012. Which was what has previously been said but with added quotes and using the 'Add arguments' box. So in Task Scheduler->Actions->Edit Action.
Program/script: "C:\xampp\php\php.exe"
Add arguments: -f "<full path and filename>"
Start in: <Path to file>
(I had to use 'start in' as i referred to class's within the script)
Hope this helps.
Here's how I did it.
In the Run box: c:\location_of_my_php_installation\php.exe -f c:\location_of_my_php_file\php_file.php
In the Start in box: c:\location_of_my_php_installation\php.exe
you can directly call your local host url by using
explorer "http://localhost/yourFile.php"
If the answer given by Pekka does not work (C:\Xampp\php\php.exe -f C:\Xampp\htdocs\my_script.php), make sure that you have the correct PHP extensions enabled as well as the correct php.ini file being used for the PHP version you are using.
I ran into this issue recently and resolved it. I was using PHP v5.4 to run my script.php that was nested within the top level PHP folder (v5.3). When I ran the script.php, from within the v5.4 folder, it was using the v5.3 php.ini file with different extensions, which caused the script.php to fail.
To fix this, here is what I did within the Task Scheduler : Actions tab
Program Script:
"C:\Program Files (x86)\PHP\v5.4\php.exe"
Add Arguements:
-c "C:\Program Files (x86)\PHP\v5.4\php.ini" -f "C:\Xampp\htdocs\script.php"
Using the -c option, you can specify which php.ini file should be used (stackflow answer).
I verified which php was in use by following this stackflow answer.
In command line, type php -m to check which extensions are enabled.
Then, use php --ini to check which .ini file(s) is/are being read by PHP.
You may not be using the correct ini file for the PHP version you are using if you have multiple versions installed.
You dont need a .bat file just set the options in the task scheduler:
So here i've just pointed to the php.exe and then in the arguments the
-f fullpath2PHP file
The important bit is the Start in part - this should be the folder your php file (the one you want to run) sits in.
so for example:
File location
-f c:\inetpub\wwwroot\runthisplease\myFile.php
Start in
c:\inetpub\wwwroot\runthisplease\
You can test this by creating a windows shortcut (but you cannot schedule a shortcut link)
In the properties for your task in the Task Scheduler, change the account for which your script is running under to the SYSTEM account.
Beaware! You should ONLY do this for scripts you fully trust - Doing this elevates the privileges to of the script to beyond administrator.
To create a scheduler in Windows, you need to:
Create .bat file on your server or system;
Type the following command in your.bat file: “F:\xampp\php\php.exe” -f “F:/xampp/htdocs/sitefolder/test.php”;
Set the scheduler time and file in your task scheduler in Windows.