I am able to run sh /var/www/html/runcron.sh manually
but when i put below lines in crontab -e. It is not getiing called.
*/2 * * * * /var/www/html/runcron.sh
Am I missing anything here?
I have written below code inside runcron.sh
php -f /var/www/html/cronscan.php
Make sure the user running the cron has the proper permissions.And can access/ has ownership of the script. And just a quick reassurance, you want the job to run twice a minute?
Related
i want to set a cron job on a server but its not working. I know there are hundreds of links on web that shows how to setup a cron job but i cant seem to make it work. What im doing now is:
1) Running crontab -e.
Then it shows bunch of lines in the command line.
2) I go to the bottom and add */5 * * * * path/to/myfile.php
and then i exit the editor in command line. Please tell me whats wrong here. Do i need to put my file in a specific folder? or do i need to go to the desired folder and then use crontab -e, or something else. Please forgive me, this is my first cronjob, hoping to be better next time.
Here are the pictures of what im doing.
Did you restart the cron service after you updated the file?
Have you tried executing the php script from the command line first to verify that it's executing as expected? It might be that the cron task is executing but the script is failing. If the script is fine, you might want to try using php as a command followed by the path and filename of the php file and then quitting the execution after it's done with -q.
*/5 * * * * php path/to/myfile.php -q
The problem could well be that you are trying to execute a PHP file and your system is unaware of what to do with it.
Is your PHP file executable?
You can make it executable by running
$ chmod +x file.php
and if you add a shebang to it
#!/usr/bin/php
<?php
// ...
the PHP script can be executed by running
$ ./file.php
Alternatively, you need to run the PHP interpreter and pass it the path to the file as an argument.
$ php file.php
For reference, see:
http://php.net/manual/en/features.commandline.usage.php
I have php function that I wnat to run every 15 minutes. I found some questions like this:
How can I make a cron to execute a php script?
and in my case I should use this:
*/15 * * * * /usr/local/bin/php -q /path/to/my/file.p
But should I run this command in terminal or put it in my file? And once, it is executed, will it run all the time or will have time limit?
Thanks!
PHP doesn't run cron jobs, your server (or operating system) is doing this. There are two ways to put your cron job to work:
#1
Using the shell command crontab. The command crontab -l will list all existing cronjobs for your user (most likely there are none yet). crontab -e will open an editor window where you can put in a you cron job as a new line. Save, and your cron job is now running. crontab -l again and you will see it listet. crontab -r to remove all the cont jobs.
You can also start a cron job from a file. Simply type crontab filename (eg. crontab textfile.txt)
Alternatively you can also start it from within PHP. Just put your cron job into a file and start it via exec() like so:
file_put_contents( 'textfile.txt', '*/15 * * * * /usr/local/bin/php -q /path/to/my/file.php' );
exec( 'crontab textfile.txt' );
#2
If you have admin privileged on your system you can create a file in /etc/cron.d/ (for example, call it my_cronjob) and put your cron job there. In this case you probably want to run it as a user (not as admin, that would be rather insecure). This is quite easy to do, just add the user name like so:
*/15 * * * * user_name /usr/local/bin/php -q /path/to/my/file.p
(In this case the cron job will not be listet under crontab -l)
Answering your second question: As long as the cron job is listet in crontab -l or as long as the file is sitting in /etc/cron.d the cron job is running, in your case, every 15 minutes.
10 * * * * /usr/bin/php /www/virtual/username/cron.php > /dev/null 2>&1
There are two main parts:
The first part is "10 * * * *". This is where we schedule the timer.
The rest of the line is the command as it would run from the command line.
The command itself in this example has three parts:
"/usr/bin/php". PHP scripts usually are not executable by themselves. Therefore we need to run it through the PHP parser.
"/www/virtual/username/cron.php". This is just the path to the script.
"> /dev/null 2>&1". This part is handling the output of the script. More on this later.
Please read this tutorial http://code.tutsplus.com/tutorials/scheduling-tasks-with-cron-jobs--net-8800
I've been having some issues with crontab recently. After switching servers, I realized none of my cronjobs are being run. After looking at PHP info, I realized php was run with CGI, so I realized I had to switch lynx -dump URL_HERE to php -q PATH_HERE.
In the actual PHP file, I stared it out like #!/usr/bin/php -q to define where php is located on my server. However, it's not getting run. I've even set up crontab to send me an email once anything runs. No email. I've checked my junk, trash, spam, and I've even tried switching emails. Nothing.
Here's what I have now: * * * * * php -q /home/USER/public_html/file.php.
If I copy & paste it into the command line, it works wonderfully. If I run it through crontab, it doesn't get run.
You are missing envrionment variables. Try a simple test like this
add this to your crontab
* * * * * set > /tmp/vars
wait 2 -3 minutes, go back and remove the crontab entry you just created.
Next,
From the shell command line you normally use
set > myvars
diff myvars /tmp/vars
This will show you the differEnce in envIrionment. Modify your cron job environment. Just add what is needed.
You can do a couple things in order to debug this.
1) In your crontab change your entry to:
* * * * * php -q /home/USER/public_html/file.php > /tmp/filecron 2>&1
Make sure you edit the entry by typing:
crontab -e
Then run:
tail -f /tmp/filecron
To debug the output as it runs.
2) As sudo user or root, tail the cron log to make sure your cron is executing properly:
sudo tail -f /var/log/cron
The first step will give you information related to the php file itself (syntax errors etc) if that is what is failing. The second step will help you out if the crontab itself is not configured properly.
You are intending this job to run every 5 minutes right ? This is what it will do with your stated line.
I am trying to run in mac a php script using cron. I want this php script to run every one minute. I read several sources online and from my understanding is better if I use launchd. In any case, I try to make it work with cron and then if it works fine I might try to use launchd.
So here is what I do:
I wrote this command in a txt file:
* * * * * /usr/bin/php /Applications/MAMP/htdocs/php_test/main_script.php
There I have the path to php (I found it with the "which php" command) and the path to my php script.
I named the txt file: crontasks.txt and I run it through terminal with this command:
crontab /Users/dkar/Desktop/crontasks.txt
When I list the crons (crontab -l) I see that this job is listed. But nothing comes as output every one minute. The output is supposed to be an image stored in a specified folder. What am I missing here?
Thanks in advance
D.
You might have a slight miscomprehension how crond processes the "crontab" files. It would suffice to run the command crontab -e, which would let you edit your personal crontab or you edit the system crontab.
If you use crontab -e it will open the default editor (vi/vim) and you can enter the line:
* * * * * /usr/bin/php /Applications/MAMP/htdocs/php_test/main_script.php
Then you simply save your file. If you want to use the system crontab you will have to edit it directly and enter the line. Additionally the system crontab has one more field for the username. Like this:
* * * * * /usr/bin/php root /Applications/MAMP/htdocs/php_test/main_script.php
On the next full minute your script will be executed by crond.
I am running Ubunutu Linux server, PHP5, Apache2 and am having trouble getting any sort of cronjob to run through the crontab.
I edit the crontab using
crontab -e
I save the file I want to run:
*/5 * * * * php /home/user/public_html/crx/cronx.php
it saves fine. I can run the file from the console and goes through fine. I can't even find any existing logs for the file. I checked cron was running, stopped and started... no change.
The current php file is just a simple test script that inserts a single line into a database.
I checked the permissions for the file and has read and write. Am absolutely stumped. I can't seem to get ANYTHING to run through cron. Is there something I can run to test permissions?
EDIT
I have also tried the following command
/usr/bin/php /home/user/public_html/crx/cronx.php
I used whereis php and which php to locate and confirm it is all running in the right area
You have too many * values for your times.
Also, cron may not have a PATH set up correctly to use PHP.
Instead try:
*/5 * * * * /usr/bin/php /home/user/public_html/crx/cronx.php
Where /usr/bin/php is the actual path to PHP. From the console you can run which php to see the path to the PHP binary you should use.
EDIT: Here are a couple of more things to try in order to troubleshoot:
# see if cron is running just by having it create a file
*/5 * * * * touch /tmp/crontab-$(date +%s)
Another option:
Set the permissions of your PHP script to 755, and change the beginning to:
#!/usr/bin/php -q
<?php
// rest of script
Then change your cron tab to:
*/5 * * * * /home/user/public_html/crx/cronx.php
I'm still not sure if cron is the issue or the running of the PHP script.