Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cron job creating empty file each time it runs

Tags:

php

cron

wget

I have a php script I want to run every minute to see if there are draft news posts that need to be posted. I was using "wget" for the cron command in cPanel, but i noticed (after a couple days) that this was creating a blank file in the main directory every single time it ran. Is there something I need to stop that from happening?

Thanks.

like image 781
Andelas Avatar asked Feb 23 '10 23:02

Andelas


People also ask

Why does my cron job not run?

You might discover that your job works when run via the command line, but won't run via cron. This may be due to the location you're executing from or the PATH you're using. The use of relative paths is a common cause of cron issues. In cron, the PATH is set by default to /bin:/usr/bin .

How often can you run a cron job?

Unfortunately cronjobs can run only at a maximum of once per minute. So in the worst-case a user has to wait one minute until his Email is really going to be sent.

Can cron run multiple commands at the same time?

We can run several commands in the same cron job by separating them with a semi-colon ( ; ). If the running commands depend on each other, we can use double ampersand (&&) between them. As a result, the second command will not run if the first one fails.

How do you check if Crontabs are running?

Method # 1: By Checking the Status of Cron Service Running the “systemctl” command along with the status flag will check the status of the Cron service as shown in the image below. If the status is “Active (Running)” then it will be confirmed that crontab is working perfectly well, otherwise not.


1 Answers

When wget runs, by default, it generates an output file, from what I need to remember.

You probably need to use some option of wget, to specify to which file it should write its output -- and use /dev/null as destination file (It's a "special file" that will "eat" everything you can write to it)


Judging from man wget, the -O or --output-file option would be a good candidate :

-O file
--output-document=file
The documents will not be written to the appropriate files, but all will be concatenated together and written to file.

so, you might need to use something like this :

wget -O /dev/null http://www.example.com/your-script.php


And, btw, the output of scripts run from the crontab is often redirected to a logfile -- it can always help.

Something like this might help, about that :

wget -O /dev/null http://www.example.com/your-script.php >> /YOUR_PATH_logfile.log

And you might also want to redirect the error output to another file (can be useful, to help with debugging, the day something goes wrong) :

wget -O /dev/null http://www.example.com/your-script.php >>/YOUR_PATH/log-output.log 2>>/YOUR_PATH/log-errors.log
like image 86
Pascal MARTIN Avatar answered Oct 14 '22 09:10

Pascal MARTIN