How to send UDP packet from Debian 9 CRON? - linux

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.

Related

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

crontab running twice but ps ax shows only one crond process

I am trying to run a crontab which executes a bash script which triggers mail command. I have done only one entry in crontab for my user. Also doing ps ax | grep cron results only one crond process. I don't know why i am getting mails twice
Cron logs
Aug 7 14:38:10 centos crond[29299]: (CRON) INFO (RANDOM_DELAY will be scaled with factor 85% if used.)
Aug 7 14:38:11 centos crond[29299]: (CRON) INFO (running with inotify support)
Aug 7 14:38:11 centos crond[29299]: (CRON) INFO (#reboot jobs will be run at computer's startup.)
Aug 7 14:40:01 centos CROND[29376]: (root) CMD (/usr/lib64/sa/sa1 1 1)
Aug 7 14:50:01 centos CROND[29940]: (root) CMD (/usr/lib64/sa/sa1 1 1)
Aug 7 14:59:01 centos CROND[30388]: (test_user) CMD (bash /home/test_user/dev/mail_test_user.sh)
Aug 7 15:00:01 centos CROND[30585]: (test_user) CMD (bash /home/test_user/dev/mail_test_user.sh)
Aug 7 15:00:01 centos CROND[30586]: (root) CMD (/usr/lib64/sa/sa1 1 1)
Aug 7 15:01:01 centos CROND[30775]: (root) CMD (run-parts /etc/cron.hourly)
Aug 7 15:01:01 centos run-parts(/etc/cron.hourly)[30775]: starting 0anacron
Aug 7 15:01:01 centos run-parts(/etc/cron.hourly)[30787]: finished 0anacron
Aug 7 15:01:01 centos run-parts(/etc/cron.hourly)[30775]: starting 0yum-hourly.cron
Aug 7 15:01:01 centos run-parts(/etc/cron.hourly)[30795]: finished 0yum-hourly.cron
Crontab list
#reboot autossh -M 8000 -f -N -R 9000:localhost:22 remote_user#192.168.0.3
#reboot autossh -M 8002 -f -N -R 9001:localhost:5901 remote_user#192.168.0.3
#reboot autossh -M 8004 -f -N -R 9002:localhost:80 remote_user#192.168.0.3
0,59 * * * * bash /home/vikas/dev/mail_test_user.sh
Contents of mail_test_user.sh
echo "I am up :)))" | mail -s "Notification : test_user#centos.localdomain [STAG]" foobaar22#gmail.com
ps aux | grep cron results
root 29299 0.0 0.0 126300 1692 ? Ss 14:38 0:00 /usr/sbin/crond -n
test_user 31650 0.0 0.0 112640 964 pts/0 S+ 15:13 0:00 grep --color=auto cron
Any luck why the hack this is happening ?
I also tried rebooting and restarting crond but not working.
The second line is result of your grep command. This is process of grep command not your cronjob process. Because it matches your search criteria.
The error is in setting up cronjob it should be */59 instead of 0/59.

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