How to run cron job automatically without any action in wordpress? - wordpress

I have used wp_schedule_event but this is not full my requirements.It can be triggered on any click or reload the page.
How can i create cron job that automatically trigger on a specific time without any action in wordpress.

The only way, as far as I know is to use the cron ability of your server.
For example, in Red Hat based distributions such as CentOS, crontab files are stored in the /var/spool/cron directory, while on Debian and Ubuntu files are stored in the /var/spool/cron/crontabs directory. Open crontab file and write something like:
* * * * * php /var/www/html/your-website/your-cron.php
This will execute the file your-cron.php every minute.
Note: Although you can edit the user crontab files manually, it is recommended to use the crontab command.

Related

cPanel Cron Permissions (Denied)

What permissions does a cron job run with on cPanel? Or even better, can I run a cron as a specific user? I had this working on a plesk panel but can't seem to get it going on cPanel.
I am trying to run a cron job for a Symfony project
php-cli /home/appname/public_html/website/app/console appname:images:clean
Within this command in Symfony I am trying to log what I am doing. The error that I get back from the cron is:
[UnexpectedValueException]
The stream or file "/home/appname/public_html/website/app/logs/dev.appname.log" could
not be opened: failed to open stream: Permission denied
The permissions on the file are
-rw-rw-r--+ cpaneluser:cpaneluser
You have probably added cron job to your own user's crontab instead of cpaneluser's one.
You can edit other user's crontab with:
crontab -u cpaneluser -e
And paste this cron job there.
Another solution could be to change the way you handle permissions in general, but that a bit more complex topic.

How do I add a cronjob to a makefile?

How do I add a cronjob to a makefile so when I compile a C program, I add a cron job to crontab so that program gets executed every minute:
Cron job I am trying to add:
* * * * * /Users/jenna/desktop/ && ./myProgram command_line_arg1
how do I add this to a make file ?
I believe it is a bad idea to add a crontab job from a Makefile. Adding a cron job is a sysadmin issue. Compiling your program is for a developer's hat.
If you insist on doing that, you might use crontab -l > mycrontab to list the current crontab jobs, then appending with
echo '* * * * * cd /Users/jenna/desktop/ && ./myProgram command_line_arg1' \
>> mycrontab
you probably want to make that more robust (perhaps use grep or awk) to avoid the appending if the crontab already contains that. BTW, you probably want to use $HOME/Desktop (and even that might not work on a French configured user, it would be $HOME/Bureau).
Then, set the new crontab with crontab mycrontab
At last, running something every minute seems suspicious to me. What is your program doing?
I would suggest to at the very least prompt the user before updating his/her crontab file....
Perhaps you want the at command....

R script as cronjob not executing

I've created a short R script that continuously downloads data from twitter using the streamR package. This script is supposed to run on a standard Amazon EC2 server running Ubuntu 14.04. When testing it in the standard command line, it runs fine. However, it is not run as specified in the cronjob. I used the following command:
sudo crontab -e
and then added the following line to the file
0 * * * * Rscript /home/mydirectory/docs/phd-research/data-collection/cron-script.R
in the hope that it would execute the R script every hour.
Is there anything I might have got wrong with the permissions? I've already checked that cron is running and I chmodded the directory the r script is supposed to write to to 775.
Thanks in advance!
OK - it turns out I used the wrong command to call up the crontab. The directory it was supposed to be working in was owned by user rather than root, so calling a root crontab didn't work.
The proper command for editing the crontab is the following:
crontab -u user -e
The Rscript didn't require the full path, it worked with both.
The $PATH$ is not known (i.e. the directory of Rscript is not part of the standard $PATH$) during the execution of a cronjob. Writing the full path to Rscript will work:
0 * * * * /.../Rscript /home/mydirectory/docs/phd-research/data-collection/cron-script.R

Issues with cron

I'm really stuck. I've used cron on many machines, but I simply can not get it to work on an Ubuntu server. I've tried everything I can think over the weekend and I'm baffled. I'm hoping someone can help me out.
I can verify that cron is running via pgrep cron. However, pgrep crond returns nothing. I am trying to run a simple shell script (test.sh) with cron:
#!/bin/sh
/usr/bin/touch /home/jarvis/test.txt
I have run chmod +x on the shell script and my crontab looks like this:
01 * * * * /home/jarvis/test.sh
I also have a new line ending after the line.
Try redirecting all output to a file and see if there are any error messages that can help you diagnose the problme, i.e.
01 * * * * /home/jarvis/test.sh > /tmp/jarvis_test.log 2>&1
Also, if you created any of those files from a windows environment, don't forget dos2unix filename
edit
My point is that you won't know for what your script is doing in the crontab environment unless you see if it is outputing an error message. Error messages also go to local crontab user's email, so try mail (as the same user) and see if you have a bunch of messages from your crontab.
pgrep crond returning nothing sounds like the problem, but I'm not a sysadmin. Maybe you want to flag this and ask for moderator to move to https://serverfault.com/
I hope this helps.
I think it is related to the user permissions. Try putting in default crontab hourly,weekly files, if it works there then your cron is good. Also, check /var/log/messages for any cron entry. View /etc/crontab for detailed configuration.

Drupal: can I setup cron with absolute paths?

I need to setup cron on my ubuntu server. I have several Drupal websites (with different domains).
In the drupal.org help pages, it is suggested to use the domain http://www.example.com/cron.php
However, I was wondering what to do if I have multiple domains ? Should I add multiple lines for each website ??
Also, what happen if I'm hiding the website using a subdomain while development ? Isn't better to specify the Drupal absolute path on the server instead ?
Can I use instead:
0 * * * * wget -O - -q -t 1 /var/www/drupal/cron.php
?
thanks
When to run
Yes, you want to have one entry per line. Unless the output of errors or simliar are prefixed with what site they came from, you would have no way of knowing what site failed.
You should also offset when each sites cron.php is executed, this is more important if you have a larger number of sites.
Trigger
You can call /var/www/drupal/cron.php without the webserver, although you need to specify the path to PHP and have your script as the argument, not use wget. What this path is will differ depending on your system. Try which php in your shell to see where it is.
$ which php
/usr/bin/php
So, your crontab could end up looking like this:
0 * * * * /usr/bin/php /var/www/drupal/cron.php
30 * * * * /usr/bin/php /var/www/other_drupal_site/cron.php
The first one is started every hh:00 and the second one hh:30.
It is mentioned in http://drupal.org/cron that you can execute the script locally, search for /usr/bin/php on the page to go there (didn't find an anchor).
Use drush instead. here's how to run drush for multi-site,
http://blog.thetajoin.com/content/drush-multisite-cron

Resources