Multiple Cron jobs in one crontab file - php

I wanted to implement two cronjobs with different execution time. One cron job is for sending emails and second cron job for validating my application subscriptions.
I write one crontab file and write to two cronjob as follows:
2 * * * * path to mailCronjob mail.php
20 * * * * path to check my application's subscriptions sub.php
The problem is first cronjob is working fine. Mail will delivers fine, but the second cronjob
is not working. I tried to run second job manually, its also working fine.
I am using command to set cronjob as:
crontab crontab_file
when I give command crontab -l
it also shows both cronjob in command line.
I wanted to ask, am I missing something here, or what should I do to run those cronjobs.

FACT: you can run as many cron jobs from a single crontab file as you wish.
FACT: you can also run different jobs as different users, each with their own crontab file.
SUGGESTION:
1) Just debug what's wrong with your second job.
2) It could be path, it could be permissions; it's more than likely environment (the environment for "cron" can be different from the environment for the same user from a command line).
PS:
Try this, too:
How to simulate the environment cron executes a script with?
Debugging crontab jobs

Check the owning user's email and see if an error report has been sent to it.
If you need to be a certain user and have that user's environment change your call to
su - -c "/path/to/sub.php" SubScriptUser
If your script only works from a certain directory use
cd /path/to/ && ./sub.php

I recall the same problem. For some reason, I had to press enter after my second cron job. Even in the first cron job, if it is the only job, needs a CR/LF (enter). Cursor needs to be on second line (if there is one cron job)... cursor needs to be on the third line, if there is two cron jobs. I guess, needs to read the file completely to interpret the cron job command. I just share this, because if you dont do that, only the first job will be executed, and skips the second one totally unless enter is pressed at the end of the second job. Best regards and cheers... Test that and let us know.

You need to add an empty line to the end of the config file

I never did 2 actuall cronjobs in one cron-tab file, but rather had the one cronjob execute every 15 minutes and query the database or look into a config file what tasks are there to execute, maybe this concept helps you.

Related

PHP script not running from cron

I have the following command:
php /var/www/html/XYZ/api_new/XYZ-API/src/public/fetch_events/fetch_events.php
This runs perfectly from the command line. However, I want it to run as a cron job every 15 mins, so I added the following entry to my crontab:
15 * * * * php /var/www/html/XYZ/api_new/XYZ-API/src/public/fetch_events/fetch_events.php
This however, does not seem to work at all. It does not even show up in my system logs (all my other cron jobs do show up).
Any ideas?
There's not really enough information to answer your question, but here are some things that might help.
Sometimes cronjobs don't have context for where to find these programs, so do which php and replace the full path with the php command.
/var/log/cron should have a record of it running, but if there's a problem, you might not know why. Try appending the following to the end of the cron line to aid in debuging:
>> /tmp/fetch_events.log 2>&1
This file will probably contain some hints as to what's going on when the cron does fire.

How to run php script on cron job in cpanel

I schedule a new job on cronjob. But it's not working. Is there any way to run cronjob in cpanel using commands.
There is a bunch of way to running script with CRON. I do use this way on my web application most of the time when I need to use CRON.
The first step is to choose a setting. The setting means when you want to run this script.
and after then simply just use CURL with Cron to get this work properly like this: curl http://example.com/script.php on command box and click on the add button and whola! You have successfully added your first CRON Task.
You may have a look at this picture below to get it properly
Go to CRON JOB option on your cpanle menus
Set url like this and set time according to your functionality
php /home/username/public_html/filename.php
Run it in putty
/usr/bin/php /var/www/html/project_name/artisan schedule:run 1>> /dev/null 2>&1 this is for laravel project
you can simply use like this - /usr/local/bin/ea-php74 /home/username/domainname.com/script_path/yourfile.php
replace username to your cpanel username, domain name to your domain name, script path where you want to run it, then replace the main file for corn job in yourfile.php

set up Cron job to run PHP script

I know this question has been asked before, but none of the answers are working for me.
I'm trying to run a simple PHP script every night at midnight. I created a file called "autoDelete.php" that contains just this code:
<?php
include 'my-database-connection.php';
mysql_query("DELETE FROM meetings WHERE indexDate < NOW()");
?>
I know this script is working because if I navigate to it in a browser, it does what it should.
I then set up the Cron job (via GoDaddy cPanel) to run every minute, with a command to run the script using this:
* * * * /usr/bin/php -q /home/username/public_html/autoDelete.php
However, this is not working. I suspect this has something to do with whatever precedes the "/home" in the command.
Some things to check:
1) cron jobs' default "working directory" is the home directory of the user ID they're running under. That'd most likely be /home/username in this case. That means if you have any relative-pathed include/require commands, they're going to be relative to /home/username, NOT /home/username/public_html. Make SURE that all necessary files are accessible.
2) You're simply assuming the query call succeeded. That's exactly the wrong the thing to do. Calls to external resources (DBs in particular) have exactly ONE way to succeed, and a near infinite number of ways to fail. ALWAYS check for failure, and treat success as a pleasant surprise.
Combining these two (failing to include your connection script, and failing to check for failure), and you end up with what you've got: "nothing" happening. At least try something like
mysql_query(...) or die(mysql_error());
^^^^^^^^^^^^^^^^^^^^^^
The error message will become your script's output, and get emailed to the controlling account's mailbox.
I've had issues in the past with running PHP scripts in a cron job when trying to invoke the PHP binary directly. My solution was to use wget in the cron job since the script was servable by Apache anyway (0 0 * * * wget url/of/script.php). Apache already has the right PHP environment set up so might as well just ask it to handle the job.

Symfony2 Command not working with cron

the command works when its manually called, but does not when its on cron.
I have used Cron Job in symfony2 this for referencing. did not work.
command in cron is:
55 09 * * * /usr/bin/php /var/www/symfony2/current/app/console mycommand >> /var/www/symfony2/logs/logthis.txt
the /current/ is a link from capistrano to the current version.
EDIT:
I would like to add to peter_the_oak list.
A obvious one but still.
if its not working
Check cron status
sudo su
/etc/init.d/cron status
if it returns failed
/etc/init.d/cron start
was the case for me.
Sometimes if cron jobs are not working, it's because one of these points:
Make sure the file permissions and the user are matching. https://askubuntu.com/questions/189189/how-to-run-crontab-as-userwww-data I know you'll say "it's just root", but as we don't see everything here, maybe there's some mistake.
Make sure you edit the tab the right way (crontab command)
Make sure the time definition are what you really mean. Your script will be run at 9:55 am, and what is the current time of your server?
So I suggest first write a definition with only stars to have every minute a call, and first call just some echo to observe. Then alter the echo to your real PHP command and observe again. After that, set the time parameter and check the server time. That should do it.

Create cronjob in kloxo to run .php file every x minutes

I am trying to add a cronjob in the kloxo based user interface,
So i navigate into cron scheduled tasks -> add standard -> Select x minutes, select 'all' in all other fields (hour, day, week, month, ..), then add the path in the last field: /home/piscolab/public_html/keepyourlinks.com/includes/boot.php
But the file is never executed,
what am i missing?
find the problem: check the cron log. if you dont know where your cron log is going to check the syslog. if you cant find anything there kill the cron proccess and start a new one with the -L /yourlogpath/ parameter specified. wait till the job is supposed to run and check the log after.
fix your problem or come back here to get help
as i see you dont specify what program should run the *.php file. change that to whatever program you want to run the php file.
if you use linux and the gui/webgui sux, fall back to shell. cron jobs are defined within /etc/spool/crond/crontab/root (or something similar to that, cant remember the exact path right now, but using TABSTOP will do the job for you). the crontab syntax can be seen here and here. Cron newbies go here as well.

Categories