Crontab is running command 3 times each run - linux

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.

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.

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

Why the crontab run in duplicated times?

I have added an crontab like this:
*/15 * * * * /home/test/demo.py
However, it runs in two times. And, we can see that two logs are logged in /var/log/cron:
Jun 30 20:00:01 demo1.ops.dev CROND[29181]: (root) CMD (/home/test/a.py)
Jun 30 20:00:01 demo1.ops.dev CROND[29180]: (root) CMD (/usr/lib64/sa/sa1 1 1)
Jun 30 20:00:01 demo1.ops.dev CROND[29189]: (root) CMD (/usr/lib64/sa/sa1 1 1)
Jun 30 20:00:01 demo1.ops.dev CROND[29190]: (root) CMD (/home/test/a.py)
Answered by myself,
I restart the crond daemon. It can not resolve the problem,
I restart the machine. It can not be scheduled properly.

Command not found even crontab PATH is set

Even though I set the PATH in /etc/crontab as
PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/lib/mailman/cron:/usr/lib/mailman/bin
commands found in /usr/lib/mailman/cron are still not found, Thus issuing a mail to root saying
/bin/sh: mailman: command not found
I've debugged the problem, setting up a cron entry
* * * * * /bin/echo "`/bin/date`: $PATH" >> /tmp/crontest.log 2>&1
using crontab -e which actually do write the PATH to /tmp/crontest.log, confirming that the path entered in /etc/crontab is not what cron think it should be.
Fri Feb 14 10:22:01 CET 2014: /usr/bin:/bin
I've also tried to solve it by re-start cron using (both) service crond restart and service crond stop;sleep 5;service crond start (which does the same, but to make absolutely certain that it has been restarted), but this doesn't change anything.
The /etc/crontab file is readable by everyone (permissions is 644 root root)
-rw-r--r-- 1 root root 500 10 feb 10:36 /etc/crontab
/var/log/cron does not show anything about the problem, just what's started and when I restarted the cron
grep -v CMD /var/log/cron
.
.
.
Feb 14 09:45:34 p1kitlst01l crond[12214]: (CRON) INFO (running with inotify support)
Feb 14 09:45:34 p1kitlst01l crond[12214]: (CRON) INFO (#reboot jobs will be run at computer's startup.)
Feb 14 09:48:07 p1kitlst01l crontab[12331]: (root) BEGIN EDIT (root)
Feb 14 09:48:45 p1kitlst01l crontab[12331]: (root) REPLACE (root)
Feb 14 09:48:45 p1kitlst01l crontab[12331]: (root) END EDIT (root)
Feb 14 09:49:01 p1kitlst01l crond[12214]: (root) RELOAD (/var/spool/cron/root)
Feb 14 10:01:01 p1kitlst01l run-parts(/etc/cron.hourly)[13010]: starting 0anacron
Feb 14 10:01:01 p1kitlst01l run-parts(/etc/cron.hourly)[13027]: finished 0anacron
Feb 14 10:01:01 p1kitlst01l run-parts(/etc/cron.hourly)[13010]: starting mcelog.cron
Feb 14 10:01:01 p1kitlst01l run-parts(/etc/cron.hourly)[13039]: finished mcelog.cron
Feb 14 10:19:16 p1kitlst01l crontab[13840]: (root) BEGIN EDIT (root)
Feb 14 10:19:23 p1kitlst01l crontab[13840]: (root) END EDIT (root)
Feb 14 10:27:17 p1kitlst01l crond[14170]: (CRON) STARTUP (1.4.4)
Feb 14 10:27:17 p1kitlst01l crond[14170]: (CRON) INFO (running with inotify support)
Feb 14 10:27:17 p1kitlst01l crond[14170]: (CRON) INFO (#reboot jobs will be run at computer's startup.)
Any suggestions to what I have to look in to?
Henrik
What I think is ,you have two ways to go (maybe others provide more).
1) set the absolute path for mailman in your script.
2) source the .profile, then run the script, such as:
0 1 * * * . ~/.profile; bash your_script.sh

Resources