Page 1 of 1

[bypass found] cron job not executed

Posted: Thu Dec 01, 2011 3:55 pm
by framp
I want to run a backup task every week. So I created a shell script called backup in /etc/cron.weekly on Mint 11.

Code: Select all

framp@obelix ~ $ ls  -la /etc/cron.weekly/
total 40
drwxr-xr-x   2 root root  4096 2011-11-20 15:11 .
drwxr-xr-x 171 root root 12288 2011-12-01 19:13 ..
-rwxr-xr-x   1 root root   312 2010-06-20 10:11 0anacron
-rwxr-xr-x   1 root root   730 2011-04-18 12:02 apt-xapian-index
-rwxr-xr-x   1 root root    31 2011-11-20 15:11 backup
-rwxr-xr-x   1 root root  1413 2008-11-11 00:15 cvs
-rwxr-xr-x   1 root root   895 2011-02-23 04:34 man-db
-rw-r--r--   1 root root   102 2011-01-05 11:22 .placeholder
cron is active

Code: Select all

framp@obelix ~ $ ps -ef | grep cron
root      1314     1  0 19:03 ?        00:00:00 cron
framp    9503  9449  0 20:58 pts/1    00:00:00 grep --colour=auto cron
But for some reasons I have to kick off the weekly backup manually. Any idea why it doesn't work?

Re: cron job not executed

Posted: Fri Dec 02, 2011 11:44 am
by xenopeek
Please share the contents of your backup script.

Re: cron job not executed

Posted: Fri Dec 02, 2011 2:01 pm
by framp
In the meantime I created a subfolder /etc/cron.minutely to test the execution of a simple logger script (Don't want to wait one week every time ;-) ). I see that cron starts - but no script in /etc/cron.minutely is executed. The script hello writes a message into /var/log/syslog - but there is no message written every minute :-(

Code: Select all

tail -f /var/log/syslog
Dec  2 18:57:01 obelix CRON[4999]: (root) CMD (test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.minutely ))
Dec  2 18:58:01 obelix CRON[5035]: (root) CMD (test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.minutely ))
Dec  2 18:59:01 obelix CRON[5142]: (root) CMD (test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.minutely ))
Dec  2 19:00:01 obelix CRON[5179]: (root) CMD (test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.minutely ))

Code: Select all

obelix log # ls -la /etc/cron.minutely/
total 24
drwxr-xr-x   2 root root  4096 2011-12-02 18:55 .
drwxr-xr-x 172 root root 12288 2011-12-02 18:47 ..
-rwxr-xr-x   1 root root   314 2011-12-02 18:55 0anacron
-rwxr-xr-x   1 root root    27 2011-12-02 18:46 hello

Code: Select all

obelix log # cat /etc/cron.minutely/0anacron 
#!/bin/sh
#
# anacron's cron script
#
# This script updates anacron time stamps. It is called through run-parts
# either by anacron itself or by cron.
#
# The script is called "0anacron" to assure that it will be executed
# _before_ all other scripts.

test -x /usr/sbin/anacron || exit 0
anacron -u cron.minutely

Code: Select all

obelix log # cat /etc/cron.minutely/hello 
#!/bin/bash
logger "Hello"

Code: Select all

obelix log # 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 )
* *	* * *	root	test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.minutely )
#

Re: cron job not executed

Posted: Sat Dec 03, 2011 5:11 am
by xenopeek
Try full path to logger? And always include in your cron scripts a feature to capture stdout and stderr into a logfile, else you won't know what is going wrong. As in example below:

Code: Select all

#!/bin/bash

# redirect stdout and stderr to logfile
exec >>/var/log/hello.log 2>&1

# announce started
echo "$(date +'%b %d %X') $(uname -n) $(basename $0): starting"

# your code
/usr/bin/logger "Hello world!"
Now every minute it should write something to /var/log/hello.log as well.

Re: cron job not executed

Posted: Sat Dec 03, 2011 2:03 pm
by framp
xenopeek wrote:

Code: Select all

# redirect stdout and stderr to logfile
exec >>/var/log/hello.log 2>&1
# announce started
echo "$(date +'%b %d %X') $(uname -n) $(basename $0): starting"
That's a very nice way to trace the execution and sysout and syserr of a cron script !
I changed the script into

Code: Select all

#!/bin/bash -vx
exec >>/var/log/hello.log 2>&1
echo "$(date +'%b %d %X') $(uname -n) $(basename $0): starting"
/usr/bin/logger "Hello"
echo "Hello" >> /tmp/hello
When I call

Code: Select all

cd / && run-parts --report /etc/cron.minutely
as root everything works fine. If I watch the log file I see

Code: Select all

Dec  3 18:47:01 obelix CRON[7704]: (root) CMD (test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.minutely ))
every minute - but neither an update is written in /var/log/hello.log (note the -xv flags for bash) nor in /tmp/hello. For some reasons the script is not called by cron - even it has 755 as root.root :? Do I have to register the script somehow?

Re: cron job not executed

Posted: Sat Dec 03, 2011 3:52 pm
by xenopeek
framp wrote:For some reasons the script is not called by cron - even it has 755 as root.root :? Do I have to register the script somehow?
:? I'm using cron.hourly and cron.weekly, and just put a script in those folders, 755 it and is owned by root. No need to register it, so from all info you give it should work. You might try adding your script to cron.hourly folder, just to test if for whatever reason that does work...

Re: cron job not executed

Posted: Sat Dec 03, 2011 6:30 pm
by framp
That's what I'll do. I hope I can speed up the test just by modifying the time. Frankly I don't want to wait one hour for every test :roll: . I'll keep you posted :D

Re: cron job not executed

Posted: Sun Dec 04, 2011 2:43 pm
by framp
I just copied the script from /etc/cron.minutely into /etc/cron.hourly and then the script is called successfully. I now know how to debug and log the sysout and syserr of scripts invoked by cron and will continue further debugging. Thx for your help and hints.

Re: [closed] cron job not executed

Posted: Sat Dec 17, 2011 1:30 pm
by framp
Just an update on this issue: I detected cron is configured to invoke the weekly scripts at around 6 am in the morning. Because it's my desktop and I usually sleep at this time my system is down and cron will not invoke the weekly scripts. That's different on openSuSE where the check is done every 15 mins for every scheduled task (daily, weekly and monthly). I just migrated form openSuSE to Mint because I like Mint much more than openSuSE but that's an annoying feature of Mint/ubuntu :cry: . I changed the cron defs so the weekly scripts are called at 8 pm when I'm usually online and it works now :D