Cron task running but not producing output [duplicate] - linux

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.

Related

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.

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

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

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

script in crontab not being run

i have a problem with crontab not running a script of mine at all. i have simplified the script down to a single line but it still won't run:
$ cat /etc/crontab
# /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.
SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
# m h dom mon dow user command
17 * * * * root cd / && run-parts --report /etc/cron.hourly
25 6 * * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
47 6 * * 7 root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
52 6 1 * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )
*/1 * * * * root /usr/share/test/script.sh
# don't forget the newline at the end (https://askubuntu.com/a/23337/12057):
$ cat /usr/share/test/script.sh
#!/bin/bash
echo "got here" > /tmp/test.txt
$ ls -l /usr/share/test/script.sh
-rwxr-xr-x 1 root root 951 May 8 08:59 /usr/share/test/script.sh
$ uname -a
Linux mypcname 3.2.0-4-amd64 #1 SMP Debian 3.2.51-1 x86_64 GNU/Linux
$ ps aux | grep cron
root 1111 0.0 0.0 22222 3333 ? Ss 08:27 0:00 /usr/sbin/cron
me 4444 0.0 0.0 5555 666 pts/0 S+ 09:06 0:00 grep --color=auto cron
as you can see the crontab should run script.sh once a minute and write to file /tmp/test.txt, however this file never appears. i have been reading through these possible reasons for cron not running, but so far none of them are applicable. i thought a fresh set of eyes might shed some light.
Every minute is
* * * * *
not
*/1 * * * *
Related question: Using crontab to execute script every minute and another every 24 hours
/var/log/syslog gave the clue to the answer:
May 8 08:50:01 mypcname /usr/sbin/cron[2222]: (*system*) WRONG FILE OWNER (/etc/crontab)
May 8 08:51:01 mypcname /usr/sbin/cron[2222]: (*system*) WRONG FILE OWNER (/etc/crontab)
$ ls -l /etc/crontab
lrwxrwxrwx 1 root root 24 Oct 12 2013 /etc/crontab -> /home/me/.crontab
$ ls -l ~/.crontab
-rw-r--r-- 1 root root 24 Oct 12 2013 /home/me/.crontab
i remember i did this when i installed the debian os since everything in my home dir is checked out from a subversion repository. i fixed up the issue like so:
$ sudo cp ~/.crontab /etc/crontab
and now it all works fine :)

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