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()
Related
I'm trying to make a cron file to be placed in /etc/croon. d. My problem is I don't want keep this file updated, so I'm looking for a way to get the software version dynamically from a file.
I have few other variables, but for now I think the problem is with $ (cat /software/VERSION), it works very well in shell script but not on croon.
#!/bin/bash
SHELL=/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
APPLICATION_ENVIRONMENT=SOME_STRING
VERSION=$(cat /software/VERSION)
HOME=/var/www/scripts/$VERSION/cron
CRON_LOG_DIR=/var/www/scripts/logs
*/2 * * * * root cd $HOME & php -f $HOME/do_something.php $APPLICATION_ENVIRONMENT >> $CRON_LOG/something.log
This is the output on cron log:
(root) CMD (cd $HOME & php -f $HOME/do_something.php $APPLICATION_ENVIRONMENT >> $CRON_LOG/something.log)
(CRON) ERROR chdir failed (/srv/www/tdp/public/$VERSION/backend/cron): No such file or directory
Cron table is not a shell script! You cannot put variables there.
You have to call a script from the cron and do the logic there.
If you really have to set environment variables in cron, you can do it like this
*/2 * * * * root SHELL=/bin/bash VARIABLE=something cd $HOME & php -f $HOME/do_something.php $APPLICATION_ENVIRONMENT >> $CRON_LOG/something.log
But it might not work and you might make a mistake (I am not 100% sure I made the syntax right; it's not easy and it's not necessary).
You should put as little logic to cron as possible.
Also, you should not edit the cron file directly; use crontab -e instead, it will check if you made correct syntax.
So you should do
*/2 * * * * user /home/user/your-script.sh
and set the variables in your script. (You also shouldn't run programs as root if it's possible.)
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 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"
I need to start a remote PHP script (example.com/cron.php) every minute with a cronjob. At the moment, my cronjob looks like this: wget example.com/cron.php. This works, but puts a cron.php file on my server every time. How can I prevent this? Or are there alternatives to wget?
Quoting from manpage for wget:
-O file
Use of -O is not intended to mean simply "use the name file instead of the one in the URL;" rather, it is analogous to shell redirection: wget -O file http://foo is intended to work like wget -O - http://foo > file; file will be truncated immediately, and all downloaded content will be written there.
That means that -O - redirects output to stdout. And output on stdout you can simply redirect to /dev/null:
wget -O - http://example.com/cron.php >/dev/null
if your sever has lynx installed you could do lynx example.com/cron.php or you could use curl and do curl example.com/cron.php
This solution is for linux server:
To execute a cron job you need to have access to cronTab on the server:
to edit crontab use the commnad line :
sudo crontab -e
add a the script you would like to execute:
* * * * * php /path/to/your/script/cron.php 2>&1
Save your crontab and you should be done.
Please check the link http://en.wikipedia.org/wiki/Cron to understand the asterisk
Check out https://www.setcronjob.com/
This web program enables you to automatically schedule cron jobs on other servers.
I am problem scheduling and running a script through cron job. I am on linux (ubuntu), it is a VPS.
What I am doing is I have put this line in crontab file that is here: /etc/crontab
I wrote:
*/15 * * * * www-data php /var/www/abs/phpscript.php
I have given 777 to the file and after writing above in crontab , I run command:
crontab crontab
Then after almost some time I got the mail in my /var/mail/username file that says: /bin/sh: root: not found
So I am unable to understand what is the problem.
I also run phpinfo and it shows the third variable as APACHE that probably means that PHP is running as apache module.
Please tell what can be the possible solution.
thanks in advance to every one who will try to solve my problem.
You can try also to run it using "wget -q -O"
or
*/15 * * * * lynx -dump "url" > /dev/null
Wget examples:
*/15 * * * * wget -O /dev/null 'http://www.mydomain.com/document.php?&user=myuser&password=mypass' >/dev/null
If you need to post data you can use
--post-data "login=user&password=pass"
*/15 * * * * wget -O /dev/null 'http://www.mydomain.com/document.php?&user=myuser&password=mypass' --post-data 'foo=bar' >/dev/null
If you edited /etc/crontab, you should re-read the warning at the top of the file:
# /etc/crontab: system-wide crontab
# Unlike any other crontab you don't have to run the `crontab'
# command to install the new version when you edit this file
# and files in /etc/cron.d. These files also have username fields,
# that none of the other crontabs do.
Running crontab(1) on the /etc/crontab file probably contaminated the root user's crontab(5) file (the one stored in /var/spool/cron/crontabs/). I suggest running crontab -e as root to edit the crontab file, and remove all the entries that are identical to the entries from /etc/crontab. (Maybe you just contaminated your own personal crontab(5) -- if crontab -e as root didn't show anything, run crontab -e under your own personal account and see if the system-wide entries were duplicated into your own crontab(5).)
I don't know what file you ran chmod 777 on, but that was probably unnecessary. You really should set your permissions to be as strict as possible to confine the results of malicious attacks or unintentional mistakes.
You are running a crontab as a user, which means you can't specify the user in the cron.
The template you borrowed your example from was for a system (root) cron.
Remove the username and try again.