cron stop sending error mails if command output is redirected to logfile - linux

I have a problem with the error mail's from cron
If I create two job's executing the same perl script one which redirected to /dev/null or a logfile and one without redirect I only get an error mail for the one without redirect
/etc/cron.d/test-cron
MAILTO="logs#example.com"
* * * * * root /root/test.sh > /dev/null
* * * * * root /root/test.sh
/root/test.sh
#!/usr/bin/perl
use strict;
print "test\n";
exit 1;
syslog output for cron and postfix
May 18 19:14:01 cron-master CRON[31428]: (root) CMD ([31436] /root/test.sh)
May 18 19:14:01 cron-master CRON[31428]: (CRON) error (grandchild #31436 failed with exit status 1)
May 18 19:14:01 cron-master CRON[31428]: (root) END ([31436] /root/test.sh)
May 18 19:14:01 cron-master CRON[31429]: (root) CMD ([31439] /root/test.sh > /dev/null)
May 18 19:14:01 cron-master CRON[31429]: (CRON) error (grandchild #31439 failed with exit status 1)
May 18 19:14:01 cron-master CRON[31429]: (root) END ([31439] /root/test.sh > /dev/null)
May 18 19:14:01 cron-master postfix/pickup[28859]: 5537251A9: uid=0 from=<root>
May 18 19:14:01 cron-master postfix/cleanup[30966]: 5537251A9: message-id=<20200518191401.5537251A9#cron-master#example.com>
May 18 19:14:01 cron-master postfix/qmgr[143]: 5537251A9: from=<cron-master#example.com>, size=674, nrcpt=1 (queue active)
May 18 19:14:01 cron-master postfix/smtp[30968]: 5537251A9: to=<logs#example.com>, relay=smtp.example.com[80.50.67.97]:587, delay=0.42, delays=0.02/0/0.32/0.09, dsn=2.0.0, status=sent (250 Requested mail action okay, completed: id=1Ma1oK-1jXP8H2tyW-00W08q)
May 18 19:14:01 cron-master postfix/qmgr[143]: 5537251A9: removed
cron -> 3.0pl1-136ubuntu1
postfix -> 3.4.10-1ubuntu1
OS -> latest Ubuntu 20.04 docker image (ubuntu:focal-20200423)
Docker Endpoint -> /usr/sbin/cron -f -l -L 15

You can use tee command to "fork" standard output.
* * * * * root /root/test.sh | tee -a logfile_name
man tee
tee - read from standard input and write to standard output and files
Synopsis
tee [OPTION]... [FILE]...
Description
Copy standard input to each FILE, and also to standard output.
-a, --append
append to the given FILEs, do not overwrite

Related

Cron task running but not producing output [duplicate]

This question already has answers here:
Cron error with using backquotes
(2 answers)
Percent sign % not working in crontab
(1 answer)
Closed 6 months ago.
I have a very simple cron task I want to run hourly, its a bash script that I want to store the output of in a daily log file. The crontab looks like:
[srvc_user]$ crontab -l
15 * * * * /path/to/script 2&>1 >> /path/to/logs/script_$(date +"%Y%m%d").log
What I'm seeing from the syslog is that the task is running hourly as scheduled but no new log files are being produced:
[root]# journalctl -u crond.service -g script
Aug 17 04:15:02 myhost.server.com CROND[3827797]: (srvc_user) CMD (/path/to/script 2&>1 >> /path/to/logs/script_$(date +").log)
Aug 17 05:15:01 myhost.server.com CROND[3742589]: (srvc_user) CMD (/path/to/script 2&>1 >> /path/to/logs/script_$(date +").log)
Aug 17 06:15:01 myhost.server.com CROND[3655718]: (srvc_user) CMD (/path/to/script 2&>1 >> /path/to/logs/script_$(date +").log)
Aug 17 07:15:01 myhost.server.com CROND[3567401]: (srvc_user) CMD (/path/to/script 2&>1 >> /path/to/logs/script_$(date +").log)
Aug 17 08:15:01 myhost.server.com CROND[3479789]: (srvc_user) CMD (/path/to/script 2&>1 >> /path/to/logs/script_$(date +").log)
Aug 17 09:15:01 myhost.server.com CROND[3386346]: (srvc_user) CMD (/path/to/script 2&>1 >> /path/to/logs/script_$(date +").log)
[srvc_user]$ ls /path/to/logs
## No new files here!
I can run the script on its own as the crontab user with no problems; I can also copy-paste the full crontab line and it runs the script and pipes the output to the logfile as expected:
[srvc_user]$ /path/to/script
# Normal output here!
[srvc_user]$ /path/to/script 2&>1 >> /path/to/logs/script_$(date +"%Y%m%d").log
[srvc_user]$ ls /path/to/logs
# New files here!
I also verified that the crontab user has access to the log folder:
[srvc_user]$ ls /path/to
drwxr-xr-x 2 srvc_user users 4096 Aug 17 01:26 logs
# other files...
Not sure what to look at next.

How to send UDP packet from Debian 9 CRON?

I am trying with these 2 scripts:
test1,sh:
#!/bin/sh
NOW=$(date +"NOW;%Y;%m;%d;%H;%M;%S")
echo -n $NOW | nc -u -q 2 -w 2 192.168.0.252 4210
test2.sh:
#!/bin/sh
NOW=$(date +'NOW;%Y;%m;%d;%H;%M;%S')
echo -n $NOW > /dev/udp/192.168.0.252/4210
Both scripts works fine when directly execute on terminal, client successfully receiving the UDP packets.
*/5 * * * * root /etc/test/test1.sh
*/5 * * * * root /etc/test/test2.sh
But doesn't work when executing in CRON, client did not receive the UDP packets.
**sudo grep CRON /var/log/syslog**
Mar 10 16:40:01 localhost CRON[12281]: (CRON) info (No MTA installed, discarding output)
Mar 10 16:40:01 localhost CRON[12286]: (root) CMD (command -v debian-sa1 > /dev/null && debian-sa1 1 1)
Mar 10 16:40:01 localhost CRON[12287]: (root) CMD (root /etc/test/test1.sh)
Mar 10 16:40:01 localhost CRON[12289]: (root) CMD (root /etc/test/test2.sh)
Please help
Is the next to last block the output of crontab -l? Looks to me like there's an extra "root" in there.
The hint is in your syslog output:
Mar 10 16:40:01 localhost CRON[12289]: (root) CMD (root /etc/test/test2.sh)
Remove the root from your crontab and you'll have better luck.

2 questions, cron job not running restart, kill not gracefully restarting

I put the following cron job in my root crontab under var/spool/cron
*/5 * * * * service php-fpm-5.5.11 restart
I see it called in the cron logs every 5 minutes, so I know it is being called, but it is not restarting php-fpm.
Question 1:
Is there a different way to restart services when calling them in cron?
What would be the correct way to call this restart?
Another question and the root of the problem is I have another call that runs every night that sometimes kills my website altogether because php-fpm is not restarting correctly:
/bin/kill -SIGUSR1 `cat /opt/pifpm/php-5.5.11/var/run/php-fpm.pid 2>/dev/null` 2>/dev/null || true
I get:
[12-Jul-2015 00:52:29] ERROR: An another FPM instance seems to already listen on /opt/pifpm/fpmsockets/5.5.11.sock
[12-Jul-2015 00:52:29] ERROR: FPM initialization failed
Question 2
Is there a better way to call the kill statement? For instance:
[ ! -f /opt/pifpm/php-5.5.11/var/run/php-fpm.pid ] || kill -USR2 `cat /opt/pifpm/php-5.5.11/var/run/php-fpm.pid`
This is an nginx and centos setup.
Here is a portion of the cron log:
Jul 15 12:15:01 insp CROND[7325]: (root) CMD (service php-fpm-5.5.11 restart)
Jul 15 12:15:01 insp CROND[7326]: (root) CMD (/usr/local/cpanel/scripts/recoverymgmt >/dev/null 2>&1)
Jul 15 12:15:01 insp CROND[7327]: (root) CMD (/usr/local/cpanel/bin/dcpumon >/dev/null 2>&1)
Jul 15 12:15:01 insp CROND[7332]: (root) CMD (/usr/local/cpanel/scripts/autorepair recoverymgmt >/dev/null 2>&1)
Jul 15 12:15:01 insp CROND[7333]: (root) CMD (/usr/local/cpanel/bin/dbindex >/dev/null 2>&1)
Jul 15 12:16:53 insp /usr/bin/crontab[7530]: (root) BEGIN EDIT (root)
Jul 15 12:16:57 insp /usr/bin/crontab[7530]: (root) END EDIT (root)
Jul 15 12:20:01 insp CROND[7842]: (root) CMD (/usr/lib64/sa/sa1 1 1)
Jul 15 12:20:01 insp CROND[7845]: (root) CMD (/usr/local/cpanel/bin/dcpumon >/dev/null 2>&1)
Jul 15 12:20:01 insp CROND[7846]: (root) CMD (service php-fpm-5.5.11 restart)
Jul 15 12:20:01 insp CROND[7847]: (root) CMD (/usr/local/maldetect/maldet --mkpubpaths >> /dev/null 2>&1)
Answer to the Question number 1.
/etc/rc.d/init.d/php-fpm-5.5.11 restart is the correct path to use in cron.
The /etc/rc.d/init.d has most of the services in the directory including httpd

Crontab not work

I have a crontab in fedora 17 with command
13 10 * * * /home/pk1/Templates/dump.sh
In dump.sh
#!/bin/sh
mysqldump --host=localhost --user=root --password=toor mba_new | gzip > /home/pk1/Templates/`date +"webt-backup_%m-%d-%y"`.gz
In log file have an error
Mar 25 10:13:01 area7 crond[31770]: (*system*) RELOAD (/etc/crontab)
Mar 25 10:13:01 area7 crond[31770]: (CRON) bad command (/etc/crontab)
if I run through terminal
sh dump.sh
everything is ok
Why my dump.sh not work through crontab? Thanks
Your crontab needs a username Please specify it as
13 10 * * * pk1 /home/pk1/Templates/dump.sh

Crontab is running command 3 times each run

I have the following crontab set up on a RHEL server ...
MAILTO=me#mydomain.com
*/2 * * * * wget --spider -q http://mydomain.com/cronjobs/importxml.php
As you can see this should run every 2 minutes, which it does, but it runs the command three times and I can't figure out why.
If I run
tail /var/log/cron
I get the following
Dec 12 13:56:01 msvsc02-g283nc crond[1431]: (root) RELOAD (cron/root)
Dec 12 13:56:01 msvsc02-g283nc crond[3224]: (root) CMD (wget --spider -q http://mydomain.com/cronjobs/importxml.php)
Dec 12 13:56:01 msvsc02-g283nc crond[2504]: (root) RELOAD (cron/root)
Dec 12 13:56:01 msvsc02-g283nc crond[3226]: (root) CMD (wget --spider -q http://mydomain.com/cronjobs/importxml.php)
Dec 12 13:56:01 msvsc02-g283nc crond[2472]: (root) RELOAD (cron/root)
Dec 12 13:56:01 msvsc02-g283nc crond[3228]: (root) CMD (wget --spider -q http://mydomain.com/cronjobs/importxml.php)
Can anyone shed any light on this?
You may have more than one cron process running in that server. This normally wont happen. But anyway confirm it with
ps aux | grep cron
You can stop it by,
/etc/init.d/cron stop
or
service cron stop
or use 'kill PID' ( not recommended ).
And to start use start - instead of stop in either of above two commands.

Resources