Cron Jobs - Doesn't seem to execute - php

Looking through the internet at examples on how to run a php script from a cron job I've settled on this command:
php -q /home/myuseraccount/public_html/crontest/crontest.php
I tried running this from SSH and had success! But when I try this as a cron job nothing happens. I have it set to run every minute. Nothing happens. It should create a text file with the current time in it:
<?php
$file = '/home/myuseraccount/public_html/crontest/test.txt';
$now = time();
file_put_contents($file, $now, FILE_APPEND | LOCK_EX);
?>
It should also email me. It doesn't. I'm assuming it would email me no matter what, like if there was a failure?
It's not possible cron jobs are somehow disabled is it? - I'd assume I wouldnt even be able to set them up...
Is there any way, like from SSH, to test cron jobs?
I've never successfully been able to get cron jobs to work, so I don't know if it's a server issue or just ignorance.
--EDIT--
My cron log file seems to just show a bunch of LIST's and REPLACE's

Consider checking your logs for cron at /var/log/syslog
Please also make sure you can actually write to the file/directory (chmod permissions). Even further ensure that the crontab userlevel can write to the file/directory as well.
I just tried your file and it does execute fine for me, will attempt to do it as a crontab int he same way as you.
EDIT: Ran it as a cronjob and that worked as well, not sure what the issue is!

Apparently crond wasn't running....
#service crond restart
Stopping crond: cannot stop crond: crond is not running. [FAILED]
Starting crond: [ OK ]
Works now.

Related

PHP script is not being executed by cron

I have a very weird issue that I'm gonna pull my hair off..
I have php script ran by cron. When I manually run
php /var/www/html/mypath/mycron.php > output.html
it works fine, the script takes about 3 mins, and I can get the output I need.
But when it's called by cron like this
0 1 * * * /usr/bin/php /var/www/html/mypath/mycron.php > output.html
it does not work, nothing's written to output!
I've tried add -f, doesn't work; I've check php max_execution_time, it's more than enough;
I've check the cron log, it says it ran, but I got nothing in output;
I've checked the permission, the crontab is under the user I manually run, and all the directories in mypath under html are owned by that user, and have rwx for owner, shouldn't have had any permission issue; I've try to create new cron under the same directory just for testing, and it worked...
It's driving me crazy, any idea why this is happening?? Thanks!

How to create a cron job in cpanel to run a PHP file every hour?

First time I am creating a cron job in cpPanel to run a PHP file every hour.
My cron command is:
/usr/local/bin /home/simplemediaplaye/public_html/new.php
What things am I missing here? This file runs well manually, but not by a cron job. Can you suggest what I need to do to run my cron successfully?
Hello If I am not mistaking, you're missing the "-q" command and PHP ? see below:
/usr/bin/php -q /home/simplemediaplaye/public_html/new.php
Let me know if that helps.

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.

unix `at` from php script, works in shell but not from web

I am having trouble executing a unix at job command from a php webscript.
the php with the at job looks like this:
$output=exec('echo "/usr/bin/perl /home/dir/test.pl" | /usr/bin/at now +1minute')
This will work fine from the shell command prompt. I can execute the php script, run atq to see that job has been set and the job belongs to root. Then wait and confirm that the test.pl script has run. The test.pl script performs a simple update to a database which I can check to confirm everything has worked.
When I execute the php script from the web, go back to my shell and run atq again. I can see that the job has been placed, this time the job belongs to apache, so everything seems fine. But after waiting for the scheduled time, the test.pl script does not execute which I can confirm after seeing the update to the db has not worked.
I belive this must be a permissions issue, but I am not sure. I tried giving /home/dir and the test.pl script ownerships to apache but this still does not work. I tried createing a symlink to /home/dir in the webroot folder but this did not seem to help either. I do not know apache permissions to well so I could be overlooking something.
here is the directory and test.pl ls -l ouput
/home/dir
drwxrwxr-x 2 apache apache
test.pl
-rwxr-xr-x 1 apache apache
Thanks for any help on this one.
If the job is being enqueued then we can discount the possibility that the apache uid is denied access to 'at'.
This then implies that the job is probably failing to execute (how do you know it is failing to run?)
Since you have root access, the next step would be to look at the mail file for the apache uid - or determine where mail for this uid is sent and look there: from the man page:
The user will be mailed standard error and standard output from his commands,
if any. Mail will be sent using the command /usr/sbin/sendmail
(If you don't find anything there, then you might want to try something like pwd | mail -s test apache or running at with -m to check that mail does get handled as it should)
Normally a daemon owner won't have any shell configured - did you set $SHELL before invoking at? (if this is the case and the mail is getting sent then there will be a corresponding error waiting to be read).
You've checked the permissions on the test.pl file and that does not appear to be the issue (assuming fACLs or SELinux restrictions are not being applied). Have you checked the permissions of the perl executable? Could test.pl be attempting to access other files which it might have permissions on?

Cannot execute crontab command from a php script...from shared host

I'm running an application where I need to execute the cron job, once a form is submitted,contains more than 1 lac records in a loop(its on-demand execution of cron job and deletes the job once finished).
I'm using php, I tried to configure the job through cpanel as a test and it works...the command was.."/ramdisk/bin/php5 -f /home/user/public_html/domain/cron.php -q", it worked fine.
But when I tried to run the same job with the exec() of php, like I wrote this whole command to a file (I do not know where the original crontab file is located otherwise could have written to it, please suggest a way to find its location) and ran that file like "exec("crontab /home/user/cron/Feed_cron"); ", in this cron is not working...
I doubted whether the crontab command will not work for particular user.. I tried it from the "root" user....which is the root user....tried the command via SSH or Putty and the response was "command not found". Please find a solution for this, also please give me an idea whether my concept works fine...
please suggest a way to find its location
Try
whereis crontab

Categories