If you like DNray Forum, you can support it by - BTC: bc1qppjcl3c2cyjazy6lepmrv3fh6ke9mxs7zpfky0 , TRC20 and more...

 

Cron & wget

Started by sanjana, Aug 03, 2022, 01:46 AM

Previous topic - Next topic

sanjanaTopic starter

I need help from Linux users. I use the following command for all my cron jobs: wget -q http://mysite.com/script.php >/dev/null.

However, it creates a file with the result of the cron operation at the root of the FTP every time it starts. How can I prevent this file from being created? Any suggestions or solutions would be greatly appreciated.
  •  


musorhik

To make sure that no file is created when the cron job starts, I recommend using this command instead: wget -q http://mysite.com/script.php -O - >/dev/null 2>&1. The option -q removes the service info about downloading, while the option -O allows you to save what is downloaded to a file with the specified name. By indicating a hyphen -, standard output is used as the file.

The result is then redirected to /dev/null, which is known as the "black hole", to dump the result and prevent it from being displayed in the console. Any possible errors are redirected to the same place as the result of the wget - to /dev/null.

However, it is not the most efficient way to run PHP scripts. Instead of using wget, consider launching the scripts directly from the console using php-cli. For example, the command for cron can be written as php /var/www/mysite.com/script.php > /dev/null 2>&1.
  •  

chandanthaver

If you're using wget, it's important to specify where the file should be written, as it's possible that it doesn't have write rights in some locations.
Additionally, it's a good practice to use full paths to the commands both in the script and in the cron job, such as sh /usr/local/. Lastly, make sure that the script has execute rights.
  •  

pereeSoadaBor

One possible solution to prevent the file from being created is to use the "-O" option with wget to specify a location for the output. For example, you could modify your command to:

wget -q http://mysite.com/script.php -O /dev/null

This will redirect the output to the null device, effectively discarding it.

Alternatively, you can use the "-o" option to redirect the output to a specific file. For example:

wget -q http://mysite.com/script.php -o /path/to/logfile.log

This way, any output from the cron job will be written to the specified log file instead of creating a new file at the root of the FTP.


Here's some additional information that may be helpful:

Another option you can try is using the "--no-verbose" flag with wget. This will suppress the output, preventing it from being written to a file. So your command would look like:

wget -q http://mysite.com/script.php --no-verbose

This can be useful if you don't need to see any output and just want to discard it.

Additionally, you can redirect the output to /dev/null directly in your cron job. Instead of modifying the wget command, you can modify your crontab entry like this:

* * * * * wget -q http://mysite.com/script.php >/dev/null 2>&1

Adding ">/dev/null 2>&1" at the end of the command redirects both standard output and standard error to /dev/null, effectively discarding the output.


more options and suggestions to prevent the creation of files for your cron jobs:

1. You can disable the output of the cron job altogether by redirecting both standard output and standard error to /dev/null. Modify your cron job command like this:

   ```
   * * * * * wget -q http://mysite.com/script.php >/dev/null 2>&1
   ```

   This will discard all output, preventing any files from being created.

2. If you still want to capture the output but prevent it from being written to a file at the root of the FTP, you can redirect the output to a temporary directory or a specific log file. For example:

   ```
   * * * * * wget -q http://mysite.com/script.php >/path/to/logfile.log 2>&1
   ```

   In this case, the output will be redirected to the specified log file, rather than creating a file at the root of the FTP.

3. Another option is to modify the script itself to suppress any output. If you have access to the script, you can edit its code to remove any unnecessary print statements or logging operations.

4. You can also consider using a different tool or command instead of wget if it better suits your needs. For example, you might use curl or other similar tools that provide more fine-grained control over output and logging.

Here are a few more suggestions to help prevent the file from being created:

1. Use the "--spider" option with wget. This option makes wget act as a web spider and it will not download the file, but will simulate the download process. For example:

   ```
   * * * * * wget --spider http://mysite.com/script.php >/dev/null 2>&1
   ```

   This will perform the cron job without creating any files.

2. Consider using the "curl" command instead of wget, as it provides more control over the output and offers options to suppress any unnecessary output. For example:

   ```
   * * * * * curl --silent http://mysite.com/script.php >/dev/null 2>&1
   ```

   The "--silent" option prevents any progress or error messages from being displayed.

3. If you have access to the server or hosting environment, you can modify the PHP script itself to redirect the output to null. Within the script, you can use the following code:

   ```
   <?php
   // Your script code here
   ob_start();
   // Output buffering starts
   // Any output will be discarded until ob_end_clean() is called
   // You may also use ob_end_flush() if you want to display the output
   ob_end_clean();
   ?>
   ```

   This way, any output generated by the PHP script will be discarded.

4. If none of the above solutions work for you, you can create a cron job that runs a separate command to delete the file that is created. For example, you can add another cron job to delete the file periodically using the "rm" command. However, this approach is more of a workaround.


Here are a few more suggestions to help prevent the file from being created:

1. Use the "-nv" option with `wget` instead of just "-q". This will make wget non-verbose, meaning it won't create any output files unless there are errors. For example:

   ```
   * * * * * wget -nv http://mysite.com/script.php >/dev/null 2>&1
   ```

   This will suppress any unnecessary output and prevent the creation of files.

2. If you want to completely prevent any potential file creation, you can use the `curl` command instead of `wget` with the "--output /dev/null" option. For example:

   ```
   * * * * * curl -s -o /dev/null http://mysite.com/script.php
   ```

   The "-s" option makes the command silent, and the "-o /dev/null" option redirects the output to null, effectively discarding it.

3. Another option is to modify the crontab itself to redirect all output to null by default. You can add the following line at the beginning of your crontab:

   ```
   MAILTO=""
   ```

   This will prevent any email notifications or output from being sent for any cron job.

4. Lastly, you can consider using a task scheduler like `systemd` or `cronie` that allows you to control output and logging more precisely. These tools provide more flexibility in managing cron jobs and their output.
  •  


If you like DNray forum, you can support it by - BTC: bc1qppjcl3c2cyjazy6lepmrv3fh6ke9mxs7zpfky0 , TRC20 and more...