script in crontab not being run - cron

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 :)

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.

Not able to open crontab -l

I am running crontab -l to view the crons on a RHEL server(v5.10) and getting the below error:
crontab: error while loading shared libraries: libaudit.so.1: cannot open shared object file: No such file or directory
I am running the command via root user. If I go to /etc and open the crontab file using the cat command I am getting the following output:
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/
# run-parts
01 * * * * root run-parts /etc/cron.hourly
02 4 * * * root run-parts /etc/cron.daily
22 4 * * 0 root run-parts /etc/cron.weekly
42 4 1 * * root run-parts /etc/cron.monthly

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

cron job not started daily

I have a script in /etc/cron.daily/backup.sh file is allowed to execute and run but do not start happening, I read the manual and used the search but not mastered decision.
ls -l /etc/cron.daily/
total 52
-rwxr-xr-x 1 root root 8686 2009-04-17 10:27 apt
-rwxr-xr-x 1 root root 314 2009-02-10 19:45 aptitude
-rwxr-xr-x 1 root root 103 2011-05-22 19:08 backup.sh
-rwxr-xr-x 1 root root 502 2008-11-05 03:43 bsdmainutils
-rwxr-xr-x 1 root root 89 2009-01-27 00:55 logrotate
-rwxr-xr-x 1 root root 954 2009-03-19 16:17 man-db
-rwxr-xr-x 1 root root 646 2008-11-05 03:37 mlocate
The cron job filename can't have a period in it on certain ubuntus. See this. Particularly, this quote within:
Although the directories contain periods in their names, run-parts
will not accept a file name containing a period and will fail silently
when encountering them
Properly, this is a problem with run-parts, which the ubuntu cron runs, and not with cron itself. Still, it's what bit me.
pls check:
1a) is the script executable and has correct owner/group settings?
1b) does it start with the correct #! line? and do you specify the full path to the shell you're using,
e.g. #!/bin/bash ?
2) does the script generate an error while being executed?
e.g. can you write to a log file from it, and do you see the log messages?
also: check in the email inbox of the user who owns the crontab -- errors are emailed to the user / e.g. root
what does the output of ls -l /etc/cron.daily/ look like? can you post that?
NOTE:
you can always create a crontab entry for this yourself, outside of those cron.xxx directory structure ;-)
See: man 5 crontab
10 1 * * * /somewhere/backup.sh >> /somewhere/backup.log 2>&1
this has the advantage that you get to pick the exact time when it runs (e.g. 1:10am here), and you can redirect STRERR and STDOUT to append to a log file for that particular script
For testing purposes you could run it ever 10 minutes, like this:
0,10,20,30,40,50 * * * * /somewhere/backup.sh >> /somewhere/backup.log 2>&1
do touch /somewhere/backup.log to make sure it exists

Resources