Using Omnilight Yii2 Scheduling - cron

I have installed this extension to assist me in my cron jobs omnilight/yii2-scheduling. The extension has good documentation that is why I settled on it amidst all the other cron-jobs extensions available for yii2. However, I am stack at some place that I need assistance. There is a place where I am asked to put a single line of code on the crontab:
* * * * * php /path/to/yii yii schedule/run --scheduleFile=#console/config/schedule.php 1>> /dev/null 2>&1
However, I am not sure where to place it, i.e. where is the crontab in yii2? anyone who has used this extension and is able to get it running to assist me here.

Maybe a little later but you must put that in the crontab, a cron is :
cron is a Unix, solaris, Linux utility that allows tasks to be automatically run in the background at regular intervals by the cron daemon
and the crontab is where are stored all this crons. To edit this file you must use in terminal:
crontab -e
Put that line here. Save and you are ready to go.

Related

Cron Jobs Not working- Sugar CRM

I have set up cron jobs for my SugarCRM as requested by Sugar:
But when we look at last runs it does not seem to work or show anything.
I am using this for email reminders, mail check and also scheduled campaign run.
Is command-line php on your system installed and the executable in whatever PATH that cronjob is using?
If not make sure to specify the full path, e.g. /usr/bin/php or /usr/local/bin/php are common.
Also based on your operating system/distribution the php command line executable may have a different name, e.g. php5.
Make sure to use the web-process user's crontab or execute php with sudo -u webprocessusername, so that the cronjob will be executed with the correct permissions.Running the cronjob as different user or even root is usually not a good idea.
To see potential error messages replace > /dev/null e.g. with > /tmp/sugarcron.log or > /path/to/webfolder/sugarcron.txt and check the file after a minute.
Further info on the SugarCRM Knowledge Base:
Introduction to Cron Jobs
Troubleshooting Cron and Schedulers

Why does my crontab not work?

I am planning to run some bash scripts every minute, and I wrote:
* * * * * bash ~/Dropbox/temp_scripts/run_all_scripts
in crontab.
It was supposed to run every minute, but it did not work. Does anyone have idea why this happens?
Transferring a comment into an answer.
Add I/O redirection to the command line in the crontab entry:
>/tmp/run_all_scripts.out 2>/tmp/run_all_scripts.err
Review the contents of the files after a minute or two has passed. Consider recording the environment to see if that's part of the problem. And consider using bash -x instead of just bash.
If you still don't get anything (the files in /tmp are not created), then you've got issues with cron; the daemon isn't running, or your user does not have permission to use it (but crontab isn't telling you that), or you've not submitted your crontab to the program (what does crontab -l say?), or … whatever is really wrong.
Note, too, that the output from cron jobs is normally (well, at least sometimes — on Mac OS X for a system I currently use, and Solaris for another that I've used previously) emailed to the person whose job it is. You should review the email on the system.
Thank you! I have already fixed it! The reason why it does not work is I used "ls -a .sh" in the script, and when the crontab did not find any *.sh files in the folder it was executing. When modifying it to "ls -a $HOME/Dropbox/temp_scripts/.sh", everything works! This debugging technique is quite helpful!
It is, in many ways, the most basic of debugging techniques — make sure you see what is actually happening. If you're not sure why a shell script isn't working, make sure you can see that it is executing and what it is producing in the way of output, and (very often) make sure you can see what it is executing with bash -x or equivalent. (AFAIK, all shells support -x to trace the execution.)

running cron.php in moodle

I am using a plugin in moodle that requires running cron. I manually run localhost/moodle/admin/cron.php and it worked. So my question is how can I run this script all the time and not manually. I read about C panel but I'm not sure how to use it.
Any advise is appreciated. Thanks in advance.
If you go to your cpanel on your host, then go to the cron options. It should also you how often you want the cron to run and a script to run.
The script is
usr/bin/php /path/to/moodle/admin/cli/cron.php >/dev/null
If you choose to run every 15 minutes then you should see something like this after setting up cron.
*/15 * * * * /usr/bin/php /path/to/moodle/admin/cli/cron.php >/dev/null
http://docs.moodle.org/25/en/Cron

Script runs from terminal, but not cron. What edits to this script do I need to make?

I have a script used for zipping a database and site files, then dumps the output into a backup folder on the server. The script runs fine from the command line, but it will not work through cron.
After much research, I am thinking that cron cannot run it in its current form because it runs in a different environment.
Here is the script, saved as file_name.sh
#!/bin/bash
NOW=$(date +"%Y-%m-%d-%H%M")
FILE="website.com.$NOW.tar"
BACKUP_DIR="/backupfolder"
WWW_DIR="/var/www/website/"
DB_USER="dbuser"
DB_PASS="dbpw"
DB_NAME="dbname"
DB_FILE="website.com.$NOW.sql"
WWW_TRANSFORM='s,^var/www/website,www,'
DB_TRANSFORM='s,^backupfolder,database,'
tar -cvf $BACKUP_DIR/$FILE --transform $WWW_TRANSFORM $WWW_DIR
mysqldump -u$DB_USER -p$DB_PASS $DB_NAME > $BACKUP_DIR/$DB_FILE
tar --append --file=$BACKUP_DIR/$FILE --transform $DB_TRANSFORM $BACKUP_DIR/$DB_FILE
rm $BACKUP_DIR/$DB_FILE
gzip -9 $BACKUP_DIR/$FILE
I currently have the script stored in /usr/local/scripts/
Is there something wrong with the above code that does not allow it to run through cron?
Which crontab should it go in? crontab -e from terminal, or /etc/crontab? They are two different files.
Several things come to mind: first, one of the most common problems with cron jobs is that generally crond runs things with a very minimal PATH (usually just /usr/bin:/bin), so if the script uses any commands from some other binaries directory, it'll fail. Where is mysqldump on your system (run which mysqldump if you aren't sure)? If this is the problem, adding PATH=/usr/local/bin:/usr/bin:/bin (or whatever's appropriate in your case) at the beginning of your script should fix it. Alternately, you can set PATH in the crontab file (put this line before the entry that runs your script).
If that's not the problem, my next step would be to capture the script's output, with something like:
1 1 * * * /usr/local/scripts/file_name.sh >/tmp/file_name.log 2>&1
... and see if the output is informative. BTW, as #tripleee mentioned, the format of your cron entry is suitable for the files crontab -e edits, but not for /etc/crontab. The /etc version has an additional field specifying which user to run the job as, e.g.
1 1 * * * eric /usr/local/scripts/file_name.sh >/tmp/file_name.log 2>&1
Best practice is to always use crontab -e (the resultant files are usually in /var/spool/cron/) and this works on every unix and linux platform I ever worked on.
Other common issues with cron execution are missing environment variables. Any environment variables set in .bash_profile (or .profile if you use korn shell) will not necessarily be present in the cron environment. This can be overcome by including them in your script.
As Gordon said, paths are another suspect. You can always full path you executables in your script (eg /bin/mysqldump). Some of the more cynical of us do this anyway to make sure we are executing what we intended as apposed to some other file of the same name in the current path.
I can only guess at your specific problem since you fixed it by creating /scripts, that perhaps the permissions on /usr/local/scripts directory did not allow execution by the cron user?
I have had to remove the extension (.sh) for cron to run in some instances.
So I fixed it. Not sure what the problem was, but this worked for me.
I originally had the scripts located in /usr/local/scripts/
I created a new directory here - /scripts/ and moved the scripts there. The new crontab -e command looked like this:
1 1 * * * bash /scripts/file_name.sh
Works perfectly. Again, I am not sure what the issue was before, but it works now.

How to execute a php script every day [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Cron job on Ubuntu for php
I am running and ubuntu server and wanted to run a php script every day. I have done some research and found that cron is the best way of doing this however, this is where i got stuck, a lot of the information on the internet about cron is very hard to follow and understand.
So i wanted to execute a simple php script once a day, the script i made for testing simply just deletes a record from a database, but the real script will do a lot more.
I tried setting up a task through plesk which is provided through my web host service but it didn't seem to execute when i wanted it to, i used 1 for minutes, 22 for hours, * for day, * for week and * for month and thought this would execute every day at 22:01.
I have the directories on my server:
cron.hourly
cron.daily
cron.weekly
cron.monthly
I thought i could dump i file in there and it would execute for example every day, but i'm guessing i need to make a cron script to call a php script right?
If i were to go the way of putting a file in the cron.daily folder how would i go about it?
Also if there are any steps i need to take on the php side please let me know?
Thanks a lot for your time.
There's couple of ways to setup cron job. Assuming you got shell access you could do crontab -e from console and define job there, i.e. like this:
1 22 * * * command
which would trigger command (whatever it is) at 22:01 each day (not sure why you set minutes to 1 instead of 0 though). To launch PHP script from there you would either have to install php-cli, and then invoke it that way:
1 22 * * * <path>/php -q script.php
You can also call bash script here, to setup all the stuff like paths etc and then call your php script form bash - sometimes it is simpler to do that way instead of crafting way too long command line for cron. And it's simpler to update it later. also, you could turn your php script into bash-runnable script by setting it execution bit (chmod a+x script.php) and adding shell's shebang:
#!/usr/bin/php -q
<?php
...
If your script got too many dependencies and you'd prefer to call it via web, you could use wget to mimic a browser. so your command would be:
/usr/bin/wget --delete-after --quiet --spider <URL-TO-YOUR-SCRIPT>
wget manual can be accessed by man wget or wget -h, or is on this website. Aternatively you may use HEAD tool from perl-www package - but it requires perl while wget is a standalone tool. If you use HTTPS with self signed certs, add --no-check-certificate to your invocation arguments. And you may also want to setup .htaccess and limit web access to your cron script to localhost/127.0.0.1
every minute:
* * * * * /path/script.php
every 24hours (every midnight):
0 0 * * * /path/script.php
Se this reference for how crontab works: http://adminschoice.com/crontab-quick-reference, and this handy tool to build cron jobx: http://www.htmlbasix.com/crontab.shtml

Resources