How-To prevent Cronjob from creating a file - linux

I have the following problem:
I have this cronjob:
* 1-23 * * * /usr/bin/wget http://yannick-w.de/Test/getData.php
and this cronjob always creates a new logfile. I don't need this logfile, so I want to prevent cronjob from creating it. How is this possible?
Cheers

Just redirect stdout and stderr, for example
* 1-23 * * * /usr/bin/wget http://yannick-w.de/Test/getData.php > /tmp/getdata.out 2>&1
If no output is produced (e.g. because it has been redirected), it is not logged and not emailed to you by cron
BTW if you don't want any output at all replace /tmp/getdata.out with /dev/null
See crontab(5), cron(8)

Related

CRON job outputting blank lines

I tried to create a CRON job that runs every minute. As a root user, I ran crontab -e, and in the cronjob, I put in my command * * * * * /usr/bin/php {redacted}/index.php > {redacted}/output.txt
the {redacted} file path has permissions added: chmod ogu+rwx -R so everyone should be able to access it. I even created a new user with no sudo or root privileges, and running /usr/bin/php {redacted}/index.php > {redacted}/output.txt obviously writes the output of {redacted}/index.php to {redacted}/output.txt. However, my cron job ends up overwriting and turning the .txt file to just blank, nothing. I have no idea what is going on since I already made sure there were no permission errors, and cron jobs don't seem to have a visible log or output?
I would love to have any ideas about this. I even tried to add the cron job * * * * * root /usr/bin/php {redacted}/index.php > {redacted}/output.txt, all to the same output.
One more thing... I have tried to set the {redacted} location to /var/www/html, /var/www/{a user with sudo perms}, and {/usr/local/bin} all with the same result.
Update:
I tried something as simple as * * * * * php -v > {redacted}/output.txt and * * * * * /usr/bin/php -v > {redacted}/output.txt still with the same result, so it seems like the error is not with requiring root permissions to run my php program

Crontab - simple echo not running

I've got such situation:
I want to schedule a job with crontab on a linux server. I'm not super-user, so I'm editing (with crontab -l, editor vim) only my crontab file. For testing, I put there:
* * * * * echo asdf
And the job is not running. Is the restart of the server needed? Or maybe some administrator move?
May be it is, cron jobs will run in their own shell. So you can't expect to see asdf on your console.
What you should try is
* * * * * echo asdf > somefile_in_your_home_directory_with_complete_path.log
Next check the file by doing a tail:
tail -f somefile_in_your_home_directory_with_complete_path.log
And if it's not, check if the cron daemon itself is running or is down:
# pgrep crond
OR
# service crond status
If you want to echo something on your shell you could use wall:
* * * * * wall <<< "Hello from cron"
* * * * * echo "Hello from cron" | wall
These two lines basically do the same but the first one might not work on older shell, just choose your favorite.
Anyway, be aware that wall will send your message to every user currently connected.
For me * * * * * /bin/echo text > file is not working...I don't know why, previleges and everything is set.
(This command is running normaly when I execute it as the particular
root user, just to clarify this.)
This can be solved by injecting the path PATH=$PATH:/bin in my example.
Instead * * * * * echo text > file is working fine, probably path issue.
Hope I helped

Cron Job - How to send an output file to an email

I have this line in crontab:
* * * * * /var/www/dir/sh/mysql_dumb.sh | mail -s "mysql_dump" example#mail.com
(every minute only a sample)
So, all works fine, but the email is empty.
UPDATE:
The output from mysql_dumb.sh is a *.sql file and they save the file in a directory.
How can I send a copy (*.sql file) from this output -> mysql_dumb.sh to my email?
mysql_dumb.sh:
#!/bin/bash
PATH=/usr/bin:/bin
SHELL=/bin/bash
/usr/bin/mysqldump -u USER -pPASS DATABASE > /var/www/dir/backup/backup_DB_`date +%d_%m_%Y`.sql
If the script is reporting errors, they may be going to stderr, but you're only redirecting stdout. You can redirect stderr by adding 2>&1 to the command:
* * * * * /var/www/dir/sh/mysql_dump.sh 2>&1 | mail -s "mysql_dump" example#mail.example
From a crond perspective more accurate is to place in to your cron:
MAILTO=example#mail.com
* * * * * /var/www/dir/sh/mysql_dumb.sh
* * * * * /var/www/dir/sh/other.sh
* * * * * /var/www/dir/sh/other2.sh
Look at the last line of mysql_dumb.sh:
/usr/bin/mysqldump -u USER -pPASS DATABASE > /var/www/dir/backup/backup_DB_`date +%d_%m_%Y`.sql
The > is redirecting the output of mysqldump to the file /var/www/dir/backup/backup_DB_date +%d_%m_%Y.sql
Do you want to store a backup of the database locally?
If not, take out the the > /var/www/dir/backup/backup_DB_`date +%d_%m_%Y`.sql and put the crontab entry back to
* * * * * /var/www/dir/sh/mysql_dump.sh 2>&1 | mail -s "mysql_dump" example#mail.example
If you do want a copy of the file locally, I would suggest using tee which will write the output to the file and put the output back out on stdout, which will later be picked up by crontab.
I would change the last line of mysql_dumb.sh to be:
/usr/bin/mysqldump -u USER -pPASS DATABASE | tee /var/www/dir/backup/backup_DB_`date +%d_%m_%Y`.sql
Again I would change the crontab entry back to:
/usr/bin/mysqldump -u USER -pPASS DATABASE > /var/www/dir/backup/backup_DB_`date +%d_%m_%Y`.sql
The advantage here is mail can read the information from stdout and isn't dependent on the file being written and then read correctly. While that may be a small difference, in my experience using tee will be more reliable.
If you want to store your cron job's output on the file on server and also want to send that output file to your mail address then you can use below command.
And you can use it on cronjob to automatically run on specific interval of time, here i am dumping mysql db and sending it to my mail with attachment of dumped sql file.
* * * * * mysqldump --user username --password='p#ssword' DBNAME | gzip > /home/user/db_backup/db_backup_`date +\%d-\%m-\%Y-\%H-\%M`.sql.gz && mailx -a /home/user/db_backup/db_backup_`date +\%d-\%m-\%Y-\%H-\%M`.sql.gz -s 'database backup date:'`date +\%d-\%m-\%Y-\%H:\%M` example#gmail.com

crontab email send

Cron example: */1 * * * * /usr/bin/php /html/includes/CRON.php > /html/includes/logs/CRON_LOG.txt
This is how everything look like, but how add an option not to send me an email's?
You're probably still receiving emails because you're only redirecting stdout, but not stderr. To redirect stderr to /dev/null, use
*/1 * * * * /usr/bin/php /html/includes/CRON.php > /html/includes/logs/CRON_LOG.txt 2> /dev/null
(or wherever you want stderr to be redirected to.
You might define MAILTO in the crontab.
If MAILTO is defined but empty MAILTO="", no mail will be sent.
kind of
MAILTO=""
*/1 * * * * /usr/bin/php /html/includes/CRON.php > /html/includes/logs/CRON_LOG.txt
Add this at the top of the cron file
MAILTO=""

Cron job: problem with crontab, it automatically sending me an email

How can I disable the email on the cron?
Send the standard output (and standard error) of your cron job to /dev/null
* * * * * /some/cron/job 1> /dev/null 2>&1
If you'd prefer a log file, change /dev/null to a real filename instead, but be aware of the security issues. Specifically, if you're running with any sort of privileged account and the log file doesn't already exist, a hacker can pre-create a symlink in place of your log file, pointing at some other file. When your cron job runs the target of the symlink gets overwritten.
You can redirect the output to /dev/null like this:
* * * * * my_command > /dev/null
If an error occurs, you'll still get an email, though.

Resources