Running PHP script with Crontab (Plesk / CentOS6) - php

As a customer in Plesk, I am attempting to run:
php -q /httpdocs/_external/export/test.php
From this tutorial: http://daipratt.co.uk/crontab-plesk-php/
I'm receiving the error
"php: command not found"
Is there something I need to enable from the main user or a different command I would need to use to run the script?
(also tried /bin/php with no luck, there is no php file in that dir)
"which php"
-/usr/bin/php
(when I use this dir I also get "no such file or dir" I guess since when I use / it's pulling from the customers root not the server root)

This answer will help you. My understanding is that Cron runs everything relative to itself, so you should always use absolute paths when running something from Cron.
Good luck, and happy holidays!

Related

php shell_exec() command is not working

I am trying to run a .sh file from php.
I tried doing it with shell_exec(). but its not working
I refered many questions related to this in stack overflow but could not solve
my php code is(web.php)
<?php
echo shell_exec('/var/www/project/xxe.sh');
echo "done";
?>
only done is printed. but it is working from terminal(php /var/www/project/web.php)
In xxe.sh I am calling a python file
python vin.py
I have also changed the file permission to 777 for both .sh n .py files
please help
If you say it works on the terminal and not on apache then apache's php.ini file may be disabling the use of shell_exec().
See http://www.php.net/manual/en/ini.core.php#ini.disable-functions
Your apache's php.ini file may look something like
disable_functions=exec,passthru,shell_exec,system,proc_open,popen
Remove shell_exec from this list and restart the web server, although this is a security risk and I don't recommend it.
shell_exec might not know what directory to look in for your executable's location directory. What solved it for me was this before the shell_exec:
putenv('PATH=/usr/local/bin');
Then the terminal can find the executable. Also check permissions on every part of the command to make sure apache user has read and execute permissions.
If it works well in shell, I think apache is chrooted. So php can't find /var/...
Or user of httpd user does not have permission to enter /var/...
If you are good at PHP. Open dir /var/... And readdir() and check dir exists and check file exists.
This question might help you. scanning /home/ with opendir()
The problem is usually that when you exec code from within php it is run as the webservers user www-data in alot of linux distros. Normaly this user does not have an enviroment set up, and because of that no PATH. By using full paths in your files you can usually overcome this.
xxe.sh
/usr/bin/python /path/to/script/vin.py
While trying to run a script triggered by github post-receive webhook.
Here is where my project directory is located(cloned git repo):
/var/www/html/my-repo
I create a script inside the above directory called webhook.php:
<?php
#webhook.php
$cmd = shell_exec("git pull 2>&1");
#for debugging
echo $cmd;
?>
Execute the following command inside /var/www/html
sudo chown www-data:www-data -R my-repo/
Test it by going to http://www.myserver.com/my-repo/webhook.php
Add the path to your script to github webhooks.
I have been stuck in this problem for several hours.
I have thought about a solution.
1. move your script to a python file "script.py" and place this file to your server root.
2. shell_exec("python script.py");
Any way, it works for me.
On my host I had to give a different path for my php file to be executed from shell_exec().
This didn't work shell_exec('/usr/bin/php backgroundtask.php');.
While this did shell_exec('/opt/php/php-5.5.0/bin/php backgroundtask.php');.
You can visit this Reference.
I had the same issue because PHP backslashes.
PHP escapes the backslashes, so the command that reaches the shell
'COPY E:path1\path2\file.prn /B \127.0.0.1\"PRINTER NAME"'
so I gave command like this
'COPY E:\\path1\\path2\\file.prn /B \\\\127.0.0.1\"PRINTER NAME"'.
You have to double-escape the backslashes: once for PHP and once for the shell.

Running a php script with a .bat file

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

Cron Job uses old non-existent php file

I'm working with Yii and have to implement a script for cron.
I've got a script file, which just calls Yii and starts my php-script file.
Until this point everything is fine. If I'm updating the php-script, Cron just continues executing the old one.
Restart of cron-service, reboot of the server etc didn't help.
I also uninstalled cron and installed it again, but nothing changed. He still executes the old version of this php-script.
Anyone an idea what's wrong or what I could do to solve this? I'm using Ubuntu 12.04.
EDIT:
The cronjob script is running:
#!/bin/bash
cd ../www/protected/ ./yiic Cron ProcessPayments
The php-script
class CronCommand extends CConsoleCommand {
public function actionProcessPayments() {
...
}}
This works, but any change I make on this script is ignored by Cron.
And now I'm on this point: he executes both. My old version and the new version. I've never been this confused by something.
Assuming you have sudo on the machine it's located on...
First check to see if it's located in the usual cron directories /etc/cron.*
sudo find /etc/cron* -mount -iname yiic
If nothing shows up there then search the entire filesystem because otherwise you'll start having to search a lot of other places manually and it takes more time than running a find
sudo find / -mount -iname yiic
In both find commands if the filename is or has ever been anything other than yiic do yiic* so that it searchs for anything that starts with yiic
Once you have found the copies, if there are any others, remove every single one except the newest one, or even that one and then reinstall the script on the machine from version control.
Edit:
Probably more than you really care about, but the find command breaks down like this:
sudo - elevated priviledges so you can search everywhere as opposed to where you're user can read
find - command to look for thigs
/ - the starting point for the search, can be anywhere
-mount - this tells find not to search other filesystems
-iname - case insensitive name to search for
yiic - the search term
Locate all your copies of the script using the find command and delete those copies not needed. Restart cron after you have chosen the copy of the script you want to execute. My guess is a copy of the script might be hanging around in a directory (e.g. config.daily or something similar).
HTH
I had a similar problem. A script in /etc/cron.d was still executed in the old fashion after modifying it. I solved the problem by sending a SIGHUP to cron:
sudo pkill -SIGHUP /usr/sbin/cron

PHP external program call

My setup is as follows: Windows 7, XAMPP with Apache and PHP enabled I have a PHP script in which I call an external program to do run a conversion. This external program is an EXE file, which requires 3 attributes:
The source file
The destination file
Additional flags (conversion type etc)
When I use the command line tool built into XAMPP to execute my script, everything works fine. But when I use the exec() function in my PHP script, no output file is created. I'm pretty sure the conversion is actually happening (it takes about 5 seconds, about the same time it takes to run the PHP script).
I think it's a permissions thing, so I already moved the EXE file to the same folder as my PHP file and adjusted the permissions of the entire folder (I granted all permissions to all users). I also disabled the Windows UAC and tried to put the command in a BAT file. The file just is not created.
Any help or tips would be greatly appreciated!
My PHP code is as follows:
exec('c:\converter.exe c:\src.txt c:\dst.txt -f', $output);
print_r($output);
When I print out $output, the array turns out to be empty. When I put the exact same command in Command Prompt, the code works like a charm (no syntax errors). I use absolute paths as well.
Try to copy your executable file in same folder as your application.
try
exec("script.exe src.txt dst.txt", &$output);
echo $output;
also, do not forget to use escapeshellcmd() to add some security to your application.
Thank you very much for your input! As it turns out, it was Windows issue caused by the 'Interactive Services Detection' feature. Apache was running as a system service, which prevented calls to external programs (with a GUI). I disabled the run-as-service feature in XAMPP, which solved the problem. A more thorough explanation can be found here: http://php.net/manual/en/book.exec.php

PHP script on my MAMP testing server can't access node_module: less

I'm trying to configure my local development environment to read .less files so that I can edit .less files during development and only convert to .css when it's time to go live. Make sense?
I'm running MAMP as my local testing server. I'm following the instructions I found here:
http://programming-perils.com/155/parse-less-files-on-the-fly-and-serve-them-as-css/#comment-920
In short, the plan is to use an htaccess file catch requests to .css files and direct them to a PHP script which compiles the .less file of same name and returns the css code.
Everything seems to be working from the command line. I can compile a .less file from the command line and it spits out the css. I know my rewrite rule is working because I can type the url into a browser and see the output of my php script. For example, if my PHP script calls echo shell_exec('pwd'); I will see a path printed in the browser.
THE PROBLEM is that I can't get the less script to run unless I SSH to the localhost as root. When I exit SSH and run the command I get "Permission denied". I suspect this is what happens when my PHP script tries to call this... so it's returning nothing.
I guess the question boils down to how can I get my PHP script to run the less compiler?
UPDATE! I solved the problem...
It turns out that the less command (path/path/lessc) needed to be sudo'ed. PHP wasn't doing this, so the shell_exec() command wasn't returning anything. That's why my echo statements DID work.
There are a lot of ways to sidestep this, but I determined that editing the list of sudoers with sudo visudo was the best for my purposes. There was a lot of helpful tips on this post. Through trial and error, I figured out that PHP uses the www-data account. Adding this line fixed my problem:
www-data ALL=(ALL) NOPASSWD: /var/root/node/npm/node_modules/less/bin/lessc
Something to remember is that you STILL have to add sudo to the command that gets fed to shell_exec(). Hope this is helpful to someone else.
Maybe it would be easier if you'd use the PHP implementation of lesscss: lessphp
It turns out that the less command (path/path/lessc) needed to be sudo'ed. PHP wasn't doing this, so the shell_exec() command wasn't returning anything. That's why my echo statements DID work...
See my edits to the question above.

Categories