I have a cronjob that runs the following:
* * * * * php /path/to/phpfile.php >> /cronlog.txt
when I run the php file in bash everything works, but when the cronjob runs it, one command fails:
shell_exec("redis-cli ping"); and returns an error that sh: 1: redis-cli: not found
Does anyone know why the cron user using PHP shell_exec would not be able to use the redis-cli command?
update
git diff /env_term.txt /env_cron.txt
-SHELL=/bin/bash
-TERM=screen
-SSH_CLIENT=*************
-SSH_TTY=/dev/pts/0
-USER=root
-LS_COLORS=rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.
-TERMCAP= { a bunch of giberish }
-PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games
-MAIL=/var/mail/root
-STY=*************
-PWD=*************
-LANG=en_US.UTF-8
-HOME=/root
-SHLVL=2
LANGUAGE=en_US:en
+HOME=/root
LOGNAME=root
-WINDOW=2
-SSH_CONNECTION=*************
-LESSOPEN=| /usr/bin/lesspipe %s
-LESSCLOSE=/usr/bin/lesspipe %s %s
-_=/usr/bin/env
+PATH=/usr/bin:/bin
+LANG=en_US.UTF-8
+SHELL=/bin/sh
+PWD=*************
Did you check if your PATH variable is the same when cron is called.
A quick check is to add a dummy cron job to output the current environment variables passed to cron:
* * * * * env > /tmp/env.out
And then compare this output with when you run the env command from the terminal
try this solution:
cat cronjob
* * * * * php /path/to/phpfile.php >> /path/to/cronlog.txt
Then:
chmod +x cronjob
/etc/init.d/crond start #redhat based servers like centos
/etc/init.d/cron start #debian based servers like ubuntu
crontab cronjob
You can give absolute path of redis-cli to avoid any such issue related to environment variable.
Related
I setup several cron jobs to make things work. laravel scheduler works perfectly but my other cronjobs not working at all.
*/2 * * * * /usr/bin/php /var/www/cronjobs/index.php
when I run on the console /usr/bin/php /var/www/cronjobs/index.php it works properly. I checked executable php path with which php and gives me /usr/bin/php nothing wrong with path afaik. I tried to run php script as apache user www-data I opened crontab with crontab -u www-data -e and paste command there.. it didn't work too.
I also tried send dummy notify with crontab and it also didn't work either
dummy example
* * * * * /usr/bin/notify-send 'test'
both of them doesn't work. What am I missing here ?
The second command will not send notification as cron have no idea of your desktop environment.
The first command probably use some environment variables. So instead of run in command line you can try to create a script:
#!/bin/bash
source /path/to/user/home/.bashrc #you can try also .bash_profile
/usr/bin/php /var/www/cronjobs/index.php
and your cron to be like:
*/2 * * * * /path/to/script.sh
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 am trying to run a Symfony2 command with a cron job but I get an error that the environment is not found. Here is my cron job:
* * * * * /usr/local/bin/php /usr/lib/myApp/app/console >> /usr/lib/myApp/forumLog.txt 2>&1
For now I am just trying to make app/console work and the expected output is a list with all commands. The error that I get is:
[Symfony\Component\Debug\Exception\ContextErrorException]
User Error: The environment was not found
Do you have any idea what is wrong and what is the correct way to run symfony2 commands through cronjob?
In my own Symfony-console running cronjobs, I usually have cron run a shell script, that first changes into the apprppriate directory, and then runs the console command.
Here's an example that has been running for a year or two:
File: /etc/cron.d/systemChecks (run a shell script as user: www-user)
10 7,19 * * * www-data /var/www/dir.../bin/liipMonitor.sh
File: /var/www/dir.../bin/liipMonitor.sh
#!/bin/sh
# Running at 7:10 and 19:10
cd /var/www/dir.../
bin/console --env=prod monitor:health --group=cli -q
I put the cron setup into their own files in /etc/cron.d but much the same would apply in any other crontab file. The shell script changes directory to the base directory of the project, and then runs bin/console.
Set the --env parameter in the cronjob command, like that:
* * * * * /usr/local/bin/php /usr/lib/myApp/app/console --env=prod >> /usr/lib/myApp/forumLog.txt 2>&1
I hava a php-email (phpmailer 5.2.14) script which work fine when I run it in bash:
pi#schnickschnack: php /var/www/html/email.php
when i run this script with cron (sudo crontab -e):
*/1 * * * * root php /var/www/html/email.php
syslog says...
Jan 22 08:53:01 Schnickschnack CRON[4482]: (root) CMD (root php /var/www/html/email.php)
...but I get no mail.
I have another php-script which works fine with crontab. this script inserts values from phpmodbus into a mysql-db...
does anyone have a hint why the mail-script does not work with cron?
try
* * * * * php /var/www/html/email.php
otherwise, cron tries to execute the command "root", which is not a command.
As you are running by cron, all your usual $PATH and ENV are not available.
So CRON has no idea where to find "php".
Depending on your install - determine location of the PHP bin:
which php
use the resulting path in your cronjob. eg:
*/1 * * * * /bin/php /var/www/html/email.php
** Unless intended, dont leave the email.php script where it could be run "unintentionally" by anyone simple hitting the webserver. email.php is certainly on script kiddies hit list.
I want a script file to run once every minute.. I've written this command.
* * * * * php -q /home/<username>/public_html/cron.php
But, this cronjob is not working. whenever, I try to open this file cron.php in browser, it works fine.
I'm using Linux OS. Is there a way to debug it in order to come to know the error?
If you're using Ubuntu as I am, use the full path.
* * * * * /usr/bin/php -q /home/<username>/public_html/cron.php
Have you added an empty line (new line) after your cronjob?
To debug:
Append 2>&1 to the end of your Crontab command. This will redirect the stderr output to the stdout. Then ensure you're logging the crontab's Unix command.
* * * * * php -q /home/<username>/public_html/cron.php; ls -la >>/var/log/cronrun 2>&1
This will capture anything from the Unix command.
A couple of additional hints: Write out the environment variables by issuing the command set with no parameters. And get the shell to echo each command with the set -x command. At the top of your script issue;
set
set -x
For cPanel, you may want to test curl (in case it's installed on your server):
curl --silent --compressed http://www.your-domain.com/cron.php
So it should look similar to: http://grabilla.com/0450d-93d93a32-02ab-457c-ac1c-d2883552a940.html#
You may also want to try removing the -q from your command and see if it helps.
* * * * * php /home/<username>/public_html/cron.php
*/1 * * * * /usr/bin/php -q /home//public_html/cron.php
Add the above line to the crontab file and run it . It will add a cronjob every minute