Crontab suddenly stop working on server? - php

I have some crontab set on server on linux platform.Before that 2 days all the cron was running.I dont know what happen with crontab that they are not working now.
All the cron was running before and i have added a new crontab after that they are not running may be this is the problem or is there other problem with that.
I have check ther permission but that is ok with.
New cron i have add look like that:
*/15 * * * * php myproject/sendmail.php
30 5 * * * php myproject/sendmailOnDiscount.php
* */1 * * * php myproject/sendInvitaion.php
The last one have added and before that other was running well.
After adding crontab sendInvitation.php crontab has stop working.
Could any one tell me why crontab is not working now.(All the crontab has stop working)

Maybe it is off, you can turn it on with this command
service crond start

Mostly this problem occurs due to script file permission and ownership of script files. The same problem was faced by me. I found that my script owner was not a super user e.g. root.
So, you have to set the permission and ownership of your scrip as super user. Find below.
First of all edit your crontab as super user.(in RHEL like below)
[abc#host] crontab -e
and save crontab :wq!
Now set permission for script
[abc#host] chmod +x script.sh
[abc#host] chown root:root script.sh
Now restart your crontab.(in RHEL like below)
[abc#host] /etc/init.d/crond restart

Related

Cron tab doesn't run most of tasks

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

Cron is setup correct (I think) but is not running

I have set some commands in my crontab, but it seems like they are not executed.
PHP version is 7+
CentOS 7
This is my list when I run "crontab -u username -l"
45 0 * * * username /usr/bin/php /var/www/production/site/scripts/process1.php
*/2 * * * * username /usr/bin/php /var/www/production/site/scripts/process2.php
25 * * * * username /usr/bin/php /var/www/production/api/scripts/process3.php
username is an existing user and has rights to the PHP files. Same results when I use the root user.
When I run "Which php" I get the result below:
/usr/bin/php
So that seems to be OK too.
/var/log/cron shows me lines like this, so it appears to be called
CROND[29053]: (username) CMD (username /usr/bin/php /var/www/production/site/scripts/process1.php)
When I run the commands manually all works well, so the processx.php files seem to be OK.
I am running out of ideas... Can anybody point me to the right direction to tackle my (probably silly) mistake?
Alright, it is working... Still not 100% sure why it was not before.
But this is what I changed:
removed the processes with the command "crontab -e" and deleted the 3 commands there.
Then:
nano /etc/crontab
Added the commands there (with the right user to execute them) e voila!
Difference was that I used /etc/crontab directly and not using crontab -e.
Although I am now still curious why this is working and my first attempt through crontab -e is not... So, if anybody knows why, please let me know!

Set cronjob script on server

I'm trying to set up a cronjob script on my server. I've followed this tutorial and I now have a folder "scripts" with "cronjob.php":
<?php
define("_CRONJOB_",true);
require(APPLICATION_PATH . '../public/index.php');
// my executions
?>
In my "index.php" file:
if(!defined('_CRONJOB_') || _CRONJOB_ == false)
{
$application->bootstrap()->run();
}
But how can I set this on my server?
I've done the following as a start: chmod 755 cronjob.php, but what's next?
Use crontab, make sure you have both crontab and php-cli installed.
First edit the cron by doing
$ crontab -e
Then insert something like this
*/10 * * * * /usr/bin/php /path/to/scripts/cronjob.php
This examples does execute the script every 10th minute.
For more on the syntax see https://en.wikipedia.org/wiki/Cron#Predefined_scheduling_definitions
Alternate method for setting Cron job on Linux Server
Step 1: Open Terminal
Step 2: Type
$ sudo crontab -e
Enter System Password
Step 3: Put Cron URL to be executed for every 5 minutes interval
*/5 * * * * curl http://testwebsite.com/hitcronscript
Reference: https://crontab.guru/every-5-minutes
*Note: For test purpose one may set mail or file/Db entry.
Hope it will help developers.

Problem running a small script as cron job

I am problem scheduling and running a script through cron job. I am on linux (ubuntu), it is a VPS.
What I am doing is I have put this line in crontab file that is here: /etc/crontab
I wrote:
*/15 * * * * www-data php /var/www/abs/phpscript.php
I have given 777 to the file and after writing above in crontab , I run command:
crontab crontab
Then after almost some time I got the mail in my /var/mail/username file that says: /bin/sh: root: not found
So I am unable to understand what is the problem.
I also run phpinfo and it shows the third variable as APACHE that probably means that PHP is running as apache module.
Please tell what can be the possible solution.
thanks in advance to every one who will try to solve my problem.
You can try also to run it using "wget -q -O"
or
*/15 * * * * lynx -dump "url" > /dev/null
Wget examples:
*/15 * * * * wget -O /dev/null 'http://www.mydomain.com/document.php?&user=myuser&password=mypass' >/dev/null
If you need to post data you can use
--post-data "login=user&password=pass"
*/15 * * * * wget -O /dev/null 'http://www.mydomain.com/document.php?&user=myuser&password=mypass' --post-data 'foo=bar' >/dev/null
If you edited /etc/crontab, you should re-read the warning at the top of the file:
# /etc/crontab: system-wide crontab
# Unlike any other crontab you don't have to run the `crontab'
# command to install the new version when you edit this file
# and files in /etc/cron.d. These files also have username fields,
# that none of the other crontabs do.
Running crontab(1) on the /etc/crontab file probably contaminated the root user's crontab(5) file (the one stored in /var/spool/cron/crontabs/). I suggest running crontab -e as root to edit the crontab file, and remove all the entries that are identical to the entries from /etc/crontab. (Maybe you just contaminated your own personal crontab(5) -- if crontab -e as root didn't show anything, run crontab -e under your own personal account and see if the system-wide entries were duplicated into your own crontab(5).)
I don't know what file you ran chmod 777 on, but that was probably unnecessary. You really should set your permissions to be as strict as possible to confine the results of malicious attacks or unintentional mistakes.
You are running a crontab as a user, which means you can't specify the user in the cron.
The template you borrowed your example from was for a system (root) cron.
Remove the username and try again.

Yii and cron jobs

I have created a Yii command that needs to be run every month. If I go to my protected folder and run the command manually:
protected/yiic ganadores
It works fine. I have tried to add the following command line to etc/cron.hourly and etc/crontab with no success:
/usr/bin/php5 /var/www/path/to/project/protected/yiic ganadores (etc/cron.hourly/ganadores)
0 0 1 * * root /usr/bin/php5 /var/www/path/to/project/protected/yiic ganadores
(etc/crontab)
If I run the file ganadores inside etc/cron.hourly manually, it's working also.
What am I missing here?
Edit: Finally got it solved. I had some extra spaces in the cron line. Used tab instead spaces and it started working..
This is how I run my Yii cron jobs (in the root crontab file):
45 23 * * * sudo -u www-data php /path/to/yii/app/protected/console.php mycommand
Basically just regular crontab syntax, but I am running console.php instead of yiic, and I am setting the user to Apache (www-data) so the permissions are correct for my script. I am not sure why yours isn't working, but hopefully looking at mine will help you out. :)

Categories