How can I prevent creating log files by CronJob? - php

I have a Cronjob. My command is that: wget mysite.com/bot.php But this command will automatically creates log files that names are like:
bot.php
bot.php.1
bot.php.2
bot.php.3
etc...
How can I prevent this?
Thanks!

I assume you're trying to run your script without storing any information locally.
Try adding &> /dev/null to the end of your cron command, this will dump the output to /dev/null which is basically a black hole ( anything that ends up here is irretrievable ).
An example cron command would be:
*/5 * * * * wget -qO- http://example.com/check &> /dev/null

Related

What command would I use to run a cron job on a file not in public_html

I have a php file which I want to be executed every 15 minutes. I have the cron job set up already. I just want to know what command I would use?
My file is located in ~/app/example/myfile.php. So you can't access it through a URL. Otherwise I would have just used wget -q -O /dev/null "URL"
In your crontab it'd be
0,15,30,45 * * * * php /path/to/file.php

How to start a remote PHP script via cronjob (alternative to wget)?

I need to start a remote PHP script (example.com/cron.php) every minute with a cronjob. At the moment, my cronjob looks like this: wget example.com/cron.php. This works, but puts a cron.php file on my server every time. How can I prevent this? Or are there alternatives to wget?
Quoting from manpage for wget:
-O file
Use of -O is not intended to mean simply "use the name file instead of the one in the URL;" rather, it is analogous to shell redirection: wget -O file http://foo is intended to work like wget -O - http://foo > file; file will be truncated immediately, and all downloaded content will be written there.
That means that -O - redirects output to stdout. And output on stdout you can simply redirect to /dev/null:
wget -O - http://example.com/cron.php >/dev/null
if your sever has lynx installed you could do lynx example.com/cron.php or you could use curl and do curl example.com/cron.php
This solution is for linux server:
To execute a cron job you need to have access to cronTab on the server:
to edit crontab use the commnad line :
sudo crontab -e
add a the script you would like to execute:
* * * * * php /path/to/your/script/cron.php 2>&1
Save your crontab and you should be done.
Please check the link http://en.wikipedia.org/wiki/Cron to understand the asterisk
Check out https://www.setcronjob.com/
This web program enables you to automatically schedule cron jobs on other servers.

Using WGET to run a cronjob PHP

I tried to do a cron and run a url every 5 mintues.
I tried to use WGET however I dont want to download the files on the server, all I want is just to run it.
This is what I used (crontab):
*/5 * * * * wget http://www.example.com/cronit.php
Is there any other command to use other than wget to just run the url and not downlaod it?
You could tell wget to not download the contents in a couple of different ways:
wget --spider http://www.example.com/cronit.php
which will just perform a HEAD request but probably do what you want
wget -O /dev/null http://www.example.com/cronit.php
which will save the output to /dev/null (a black hole)
You might want to look at wget's -q switch too which prevents it from creating output
I think that the best option would probably be:
wget -q --spider http://www.example.com/cronit.php
that's unless you have some special logic checking the HTTP method used to request the page
wget -O- http://www.example.com/cronit.php >> /dev/null
This means send the file to stdout, and send stdout to /dev/null
I tried following format, working fine
*/5 * * * * wget --quiet -O /dev/null http://localhost/cron.php
If you want get output only when php fail:
*/5 * * * * php -r 'echo file_get_contents(http://www.example.com/cronit.php);'
Or more secure:
*/5 * * * * php /var/www/example/cronit.php
This way you receive an email from cronjob only when the script fails and not whenever the php is called.
you can just use this code to hit the script using cron job using cpanel:
wget https://www.example.co.uk/unique-code

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.

storing crontab php outputs in a log file

* * * * * php /home/admin/public_html/domain.com/public/cron/route.php &>> /home/admin/public_html/domain.com/log/cron.log
I have that cron running every minute.
I want to store the errors that occur in route.php in cron.log
This works wonderfully when I run :
php /home/admin/public_html/domain.com/public/cron/route.php &>> /home/admin/public_html/domain.com/log/cron.log
through the command line manually. But when crontab runs it no errors gets stored in cron.log
the cron.log is owned by admin:admin and the permissions are set to 777 just to be sure.
anyone?
Something like this:
* * * * * /usr/bin/php /home/admin/public_html/domain.com/public/cron/route.php >> /home/admin/public_html/domain.com/log/cron.log 2>&1
Where the standard error is redirected to the same destination as standard output (with 2>&1), this will collect any errors outside of the script and add them to the cron.log file.
This can be simply called with > symbol on cron or php -q or -f command.
On your case
php -q /home/admin/public_html/domain.com/public/cron/route.php > /home/admin/log.txt 2> /dev/null
Blockquote php -q /home/admin/public_html/domain.com/public/cron/route.php > /home/admin/log.txt 2> /dev/null

Categories