Laravel command in cronjob will not executed correctly - laravel

I've edited the crontab (centos) like this:
0 6 * * * php /var/www/site/artisan eex --update=true
The crontab (/var/log/cron) shows that the command has been executed successfully:
Dec 10 06:00:01 yyy CROND[19946]: (user) CMD (php /var/www/site/artisan eex --update=true)
However, the database has not been updated. If I start the command manually, it works fine:
[user#yyy energiems]$ php /var/www/site/artisan eex --update=true
> Updated 1 element(s) (43f70960-772c-11e4-92a4-57928d74f84f)
(...)
> Updated 1 element(s) (43fd1470-772c-11e4-b1f2-b58d1e3307a3)
Any ideas?
//edit: In addition to Dwights answer, I also noticed, that the cronjob runs in the wrong timezone. I've adjusted it to Germany and it works fine now.

Find out where the location of php is on your server with the which php command.
Then, update your crontab to reference php at it's precise location.
Odd solution I know, but I had a similar issue and this turned out to be the fix.

Related

Laravel not executing scheduled command

I'm using laravel telescope. I used the following code to schedule the prune command in kernel.php (it has 48 hours but I already tried with less hours):
$schedule->command('telescope:prune --hours=48')->daily();
I tested it in my local environment and it prunes the data correctly. But once in my production server it doesn't seem to be working.
Telescope is runing there and capturing data. I thought it could be a telescope command issue, but I ran
php artisan telescope:prune
and it works, it says "25404 entries pruned", I also verified this in the DB. Also, my cron task is runing every minute, and I have some scheduled calls like this:
$schedule->call('*some irrelevant stuff*')->cron('0 */3 * * *');
$schedule->call('*some irrelevant stuff*')->dailyAt('00:00');
And they are working as expected. I also tried calling the scheduller manually inside a function:
Artisan::call('schedule:run');
and the schedule calls work, but "$schedule->command" does not.
So, I'm guessing that the problem is that somehow "$schedule->command" is not working. Do you have any idea or suggestion to fix this?
You should set Laravel Task Scheduler to your's servers crontab file, adding the following entry:
* * * * * php /path/to/artisan schedule:run 1>> /dev/null 2>&1

Cron Job Running Shell Script to Run Python Not Working

As written in the title, I am having some problem with my cron job script not executing. I am using CentOS 7.
My crontab -e looks like this:
30 0 * * * /opt/abc/efg/cron_jobs.sh >> /opt/abc/logs/cron_jobs.log
My cron_jobs.sh looks like this:
#!/bin/bash
#keep this script in efg folder
#run this daily through crontab -e
#45 0 * * * /opt/abc/efg/cron_job.sh
cd "$(dirname "$0")"
export PYTHONPATH=$PYTHONPATH:`pwd`
#some daily jobs script for abc
date
#send email to users whose keys will expire 7 days later
/usr/local/bin/python2.7 scripts/send_expiration_reminder.py -d 7
#send email to key owners whos keys will expire
/usr/local/bin/python2.7 scripts/send_expiration_reminder.py -d -1
# review user follow status daily task
# Need to use venv due to some library dependencies
/opt/abc/virtualenv/bin/python2.7 scripts/review_user_status.py
So, what I've found is that the log for the cron jobs in /var/logs/cron states that the cron ran at 0:30 am accordingly.
Strangely, I find that /opt/abc/logs/cron_jobs.log empty, and the scripts does not seem to run at all. It used to output some log before I re-inputted the crontab (to re-install the cron jobs), and replaced cron_jobs.sh, so I think the problem might have arose from those actions.
And also, I would like to know if there are any ways to log the error from executing a python script. I have been trying to run /opt/abc/virtualenv/bin/python2.7 scripts/review_user_status.py but it never seem to work as intended (does not run the main function at all), and there is no log output whatsoever.
I tried to run this on a different machine and it works properly, so I am not sure what is wrong with the cron job.
Here is a snippet of the log I got from /var/log/cron to show that the cron called the job:
Mar 22 18:32:01 web41 CROND[20252]: (root) CMD (/opt/abc/efg/cron_jobs.sh >> /opt/abc/logs/cron_jobs.log)
There are a few areas to check if you haven't performed these already,
if your executable permissions set on the script,
chmod +x <python file>
in addition permissions for the user to access the directories.
Run the script manually to test the script works from beginning to end, as the user who will be running the script, will be more realistic.
You can test your crontab schedule by temporarily setting every minute for testing, unlike Windows where you can right, click and click Run.
First, thank you all for the suggestions and heads up. I found out that what was ruining my script is the existence of /r in the line break. Apparently, Linux in general does not accept /r and only accepts /n.
It is because I ftp my files to the machine where the script breaks. On the other hand, it works fine on another machine because I used git pull instead of ftp.
Hope that this info will also be a helpful to others!

Laravel4 task to cron

I'm trying to create a cron using a task I made. The problem is I have no clue how to build the cron. My task name is meeting:close, and it's in my commands folder.
Can anyone help me to build the url to call this task every hour? I guess it will start with 0 * * * *
Thanks everyone!
To create a cronjob, you have to edit a file. It is pretty easy, run crontab -e to edit the cron file for the current user.
Now just add a line to the file that opens:
0 * * * * php /full/path/to/your/application/artisan meeting:close
Maybe you also have to specify the absolute path to your php executable
Now save the file and you're all set.
By the way, if you didn't know, you can run your commands with php artisan command:name from the terminal (that's also what the cronjob is going to do...)

How to set cronjob to send emails during an condition fails? and How to test it in local ubuntu system?

I need to perform a cronjob which sends emails when a perticulor condition fails. Cronjob should run everyday. And How to test the cronjob in local machine (I am using ubuntu 12.04).
I have already setup the sendmail for php. mails are going from magento.
Thanks in advance.
First, to run PHP from a command line on ubuntu:
sudo apt-get install php5-cli
The way to handle exceptions in PHP is
try {
} catch (Exception $e) {
mail('test#example.com', 'Error subject of thing failed', "Body of thing failed\n".print_r($e,1));
}
If you want the script to stop once the error has occurred, just put a die() or exit after the mail() call.
But your question is a little light on details since we don't know what's failing or where. It could just be that your PHP code is checking for X in the OS/filesystem, then all you need is to call mail() when the condition is found.
As far as the cronjob side goes you need to do this:
crontab -l > crontab.txt
Use editor of choice to open crontab.txt
Add a line at the bottom of crontab.txt (example below)
crontab crontab.txt
Then run crontab -l | less to review the new crontab to make sure it is correct
This will run every day at 1:45am server time
45 1 * * * /usr/bin/php /path/to/script/to/run.php
http://en.wikipedia.org/wiki/Cron#Examples
You can test the script by running it manually or setting the cronjob to start in the near future instead of 1:45am

Crontab with Ruby script returning error code 1

I'm rewriting some shell scripts in Ruby that will be used to backup specific content from our site. I am running Ruby 1.8.7 and have the script running locally as root and it works fine.
I have just attempted to add the script to cronttab on Ubuntu 11.10.
*/5 * * * * ruby /root/code/backup_images.rb local
When the cron runs I receive the following error in the syslog:
Mar 21 16:15:01 ubuntu CRON[4942]: (root) CMD (ruby /root/code/backup_images.rb local)
Mar 21 16:15:02 ubuntu CRON[4941]: (CRON) error (grandchild #4942 failed with exit status 1)
I've tried the following solutions but to no avail:
env -i $SHELL --norc
Which supposedly creates a new naked shell that mimics what crontab runs and this works fine with my script.
#!/usr/bin/env ruby
I have added that declaration to the top of my script which hasn't changed the functionality when running it manually nor has it resolved the issue with the crontab.
I have even tried to output the results of the call to a file, however that only creates a new file with nothing in it.
I'm not sure what else could be the problem, but I'm assuming it has to be something with Cron not being able to find Ruby or something of that nature. I'm rather new to this, so I'm reaching a dead end here.
Thanks in advance!
I believe the fault is that you need to apply the path to ruby in your crontab
but personally I prefer just to path the ruby file to be executed.
*/5 * * * * /root/code/backup_images.rb local
and then append
#!/usr/bin/env ruby
to the top of backup_images.rb
you also need to make the ruby file executable with
chmod +x
So with the help from #tomodachi I went a step further and explicitly wrote out the following crontab line and it works now.
*/15 * * * * cd /root/code/ && ./backup_images.rb local > /root/code/log/script.log
So basically I tried to execute the command from a bash shell and it wasn't finding the script. So I explicitly go into the direct and then execute the backup script.
Everything worked fine from this point forward.
Thanks of your help.

Resources