I have looked at other answers they dont fit to this case.
I am using the full path to the file. Code I copied is simplified.
run.php contains:
shell_exec("php /var/www/html/sync/chourly.php $position $quotientx > /dev/null 2>/dev/null &");
if I use manually php run.php - it works great.
here is the line on crontab -e :
05 * * * * /usr/bin/wget -O /dev/null http://sync.eeeww.com/run.php
again the file run.php starts BUT chourly.php doesn't start. I am using centOS 6
any suggestions please?
Addition: I checked the permissions I am using ec2-user to run php run.php and crontab is using the same permission. it is able to run the file but shell_exec is where the issue occurs
Is /var/www/html/sync/chourly.php using $SERVER['DOCUMENT_ROOT'] ? Since you're explicitly calling the php interpreter (not mod_php), a `$SERVER['DOCUMENT_ROOT'] call will not work as you expect.
Try manually running the cron from shell to see where it's failing.
cd /
su - your_httpd_usersame -c "/usr/bin/wget -O /dev/null http://sync.bitpine.com/run.php"
Related
I have an sh file with file-removing commands.
I run it from php like this:
shell_exec("sudo -n ./truncatefiles.sh 2>&1");
Thats works fine if I open the PHP file from browser, but doesnt work from scheduled cron tab.
PHP user: www-data
If i run whoiami from cron, returns same: www-data
I added this to my visudo:
www-data ALL=(ALL) NOPASSWD: /www/sites/..../importscript/truncatefiles.sh
Shell exec for this sh file returns (from cron):
sudo: sorry, a password is required to run sudo
Why works it dirrefent way in cron?
What should I do for get it work?
PLease try to do the following,
Try to log your output from crotab to a file,
* * myscript.php >> /var/log/myjob.log 2>&1
This way you can debug your script.
1. Also the check the user and permissions for your shell script, php file.
2. try with sudo crotab -e
I have a bash program that picks data from a file and delivers these data (if fulfilling a threshold) to another file.
It is a php script inside the bash script named smaoutput-analyse.sh
When executed from the shell it functions perfect.
When executed as a cron job as root is is executed correct, but there is no output.
Here is the output from grep -i cron /var/log/syslog
Aug 14 16:06:01 raspberrypi CRON[6705]: (root) CMD (/home/pi/scripts/SBFspot.sh > /home/pi/test/smaoutput.txt 2>&1 )
Aug 14 16:06:01 raspberrypi CRON[6706]: (root) CMD (/home/pi/test/smaoutput-analyse.sh > /dev/null 2>&1)
The information is (as mentioned before) correctly added when running fom the shell
#!/usr/bin/php
<?php
echo " Programm to read smaoutput.txt",PHP_EOL;
// etc etc
`if(!file_put_contents("sma_saved_data.txt",$sma_saved_data_string,FILE_APPEND)){
// failure
echo "error opening the file sma_saved_data.txt for writing",PHP_EOL;
}
// etc etc
?>
Here are the crontab lines:
# Every minute result of SMA
*/1 8-22 * * * /home/pi/scripts/SBFspot.sh > /home/pi/test/smaoutput.txt 2>&1
# afterwards read and save in file
*/1 10-20 * * * /home/pi/test/smaoutput-analyse.sh > /dev/null 2>&1
I think I have set file permissions correct +rw for the files and +rwx for the bash
What did I miss
You should check the following issues:
Are all the environment variables the same? So call printenv from bash and create a cron-job */1 8-22 * * * printenv > /tmp/printenv.txt --> Compare the ouput of file /tmp/printenv.txt with printenv from bash
Are you executing with the same user and the same permissions? Execute echo "$USER" and create a cron-job */1 8-22 * * * echo "$USER" > /tmp/user.txt --> Compare the ouput of file /tmp/user.txt with echo "$USER" from bash
Check the path you executing the script. Call pwd from bash and create a cron-job */1 8-22 * * * pwd> /tmp/pwd.txt --> Compare the ouput of file /tmp/pwd.txt with the ouput of pwd from bash
Realy interesting.
This was my first post and I received quite a swift response.
Thank you!
The solution was simple: provide the full path to where you want to keep the file.
Remark: To be shure that everything will be executed I alway put cronjobs in a root crontab and not in a user crontab.
Maybe that is "smart thinking" but not so smart acting.
I would appreciate to get some comment on this root cronjob idea.
The post about executing printenv, echo "&USER" and pwd both from bash and from cron was interesting.
The printenv from bash gives a lot of information, amongs which SHELL=/bin/bash, SSH_CLIENT, SSH_TTY. MAIL, PATH, SSH_CONNECTION and lots more starting with LS_COLORS, the printenv from cron is just 6 lines HOME=/root, LOGNAME=root, PATH=/usr/bin, LANG=en_GB,UTF-8, SHELL=/bin/sh and PWD=/root
The echo "&USER" from bash gives pi, whilst from cron gives a blank file
The pwd from bash gives /home/pi/test and from cron /root
These results are understandable.
Can I learn from it that I should create cronjobs as user pi and not as user root?
I'm using crontab to call a php script.
In this script there is
error_log('test');
When the script is executed from http or direct command line like
php -f script.php
Everything is fine, my error is log.
But when called from cron it's not working.
Here is my cron
* * * * * -u www-data /full_path_to/php -f /full_path_to/script.php
Here is what I tried :
error_log with arguments :
error_log('test', 3, '/full_path_to/error.log');
changing error reporting :
error_reporting(E_ALL);
ini_set('display_errors','On');
ini_set('error_log', '/full_path_to/error.log');
cron call ending with > /full_path_to/error.log 2>&1 (don't know if useful)
For http the error_log path is set from htaccess.
I'm lost with php cli...
I can see the cron execution working every minute (syslog), so it should be a PHP config problem ?
Thanks a lot if you can help.
Edit : Cron is executed with "-u www-data"
Here is the call I see in syslog :
CRON[13921]: (www-data) CMD (-u www-data /usr/bin/php -f /fullpath/script.php > /fullpath/error.log 2>&1)
I was facing the same problem but was able to fix it after reading the manual entry for php.
Initially I had something set like:
/usr/bin/php -f /path/to/php/script.php -c 1426 >> /tmp/script.php.log 2>&1
I was able to fix it by changing the line to:
/usr/bin/php -f /path/to/php/script.php -- -c 1426 >> /tmp/script.php.log 2>&1
As per the manual entry the syntax is:
php [options] [ -f ] file [[--] args...]
Also,
args... Arguments passed to script. Use '--' args when first argument starts with '-' or script is read from stdin
Going by that, my cron command becomes:
/usr/bin/php -f /path/to/php/script.php -- -c 1426 >> /tmp/script.php.log 2>&1
and it works!
You have to be aware that there are user specific crontabs and a system wide crontab.
When you are logged in as user foo and type crontab -e in console this will allow you to edit your own user specific crontab. All defined cron tasks will be executed as user foo. This is even true for user root. AFAIK this way you simply can not change the user under which a cron task will be run.
Quite different is the file /etc/crontab. This is the system wide crontab. Within that file you can change the user of a cron task like this:
* * * * * www-data /full_path_to/php -f /full_path_to/script.php
I had in fact two problems :
1) My cron was executed with php.ini for php-cli. I used the -c flag to load the good one.
2) I was relying on $_SERVER to declare important constants using variables that do not exist in cli-mode. Now if these variables are not set I parse commande line vars with getopt()
I have 137 php files i want to run them in one command (in parallel) not by sequence.
But the problem is each file is taking 2-5 seconds.
So i have tried to make a (.sh) file and put each line as :
/usr/bin/php /files/file1.php
/usr/bin/php /files/file2.php
/usr/bin/php /files/file3.php
It will complete file1 and then run file2 and file3 by sequence.
So please what is the php or sh command to run 137 php files all in one click (parallel).
You put them in background.
for ($i=1; $i<=137; $i++) {
exec("/usr/bin/php /files/file$i.php > /dev/null 2>&1 &");
}
Run the scripts in the background by adding 'nohup' and '&'
nohup /usr/bin/php /files/file1.php &
nohup /usr/bin/php /files/file2.php &
nohup /usr/bin/php /files/file3.php &
You can use pcntl lib , that enables threads in php, you can use that and create a php-master file that will call other , and then you can master file from command file
I have this in post-commit:
#!/bin/sh
REPOS="$1"
REV="$2"
/usr/bin/php /home/name/svn/scripts/post-commit.php $REPOS $REV
But whatever I do, post-commit.php isn't being executed, not even with a chmod a+rw on it. Also there is no output from exec.
What am I missing?
Update: removed exec > ./logs/log.txt from this example since it seems to confuse people.
try:
#!/bin/sh
REPOS="$1"
REV="$2"
#debug:
echo "------------------------------"
date >> /tmp/debug.txt
echo "$#" >> /tmp/debug.txt
id >> /tmp/debug.txt
env >> /tmp/debug.txt
/usr/bin/php /home/name/svn/scripts/post-commit.php "$REPOS" "$REV" > /full/path/to/log.txt 2>&1
Also, verify that your post script works fine when executed by hand.
exec replaces the current shell process, and doesn't start a new one. So after the exec command, your shell stops.
The purpose of your particular exec command eludes me by the way ... So just remove it and you should be fine.
you'd better to exec a 'cd' first, to a directory where you really want the shell to execute.
i'm not sure the path SVN will have when running this, but of course your script have potential privilege problems