LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   cron doesn't run the rule (https://www.linuxquestions.org/questions/linux-software-2/cron-doesnt-run-the-rule-4175531736/)

Iotar 01-21-2015 06:13 AM

cron doesn't run the rule
 
Hello. I have a mailsystem server based on Ubuntu 14.04. I have created rules in crontab to delete some mails automatically:

root@mail:~# 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 )
00 23 * * * root /bin/rm /var/vmail_user/some_user/Maildir/.INBOX.some1/cur/*
00 23 * * * root /usr/bin/find /var/vmail_user/some_user/Maildir/.INBOX.some2/cur -type f -mtime +4 -delete

Rule for find command is working, but for rm command is not. I tried to change "/bin/rm" to "rm" and to "rm -rf", but it's not working. I tried to make file:

root@mail:~# cat /etc/cron.daily/rm
#!/bin/bash

rm -rf /var/vmail/info/Maildir/.INBOX.autotests/cur/

wait

exit 0

It's not working too. If I run this commands (rm and /bin/rm or bash /etc/cron.daily/rm) from console, they are working fine. Permissions:

root@mail:~# ls -la /etc/ | grep crontab
-rw-r--r-- 1 root root 1002 Jan 20 12:29 crontab

root@mail:~# ls -la /etc/cron.daily/ | grep rm
-rwxr-xr-x 1 root root 78 Jan 20 12:26 rm

Permissions to .INBOX.some1 and to .INBOX.some2 are the same.
"service cron restart" isn't helping.

Can You help me and say, what I'm doing wrong?

pan64 01-21-2015 06:22 AM

I would try to write a shell script and execute it from cron and you can check anything you want in that script and also write a logfile (also use set -xv)

Iotar 01-21-2015 06:27 AM

Quote:

Originally Posted by pan64 (Post 5304479)
I would try to write a shell script and execute it from cron and you can check anything you want in that script and also write a logfile (also use set -xv)

I've tried to write a shell script:
Quote:

I tried to make file:

root@mail:~# cat /etc/cron.daily/rm
#!/bin/bash

rm -rf /var/vmail/info/Maildir/.INBOX.autotests/cur/

wait

exit 0

It's not working too.
Is you mean that or not?

If I run this script from console, it's working fine.

pan64 01-21-2015 06:34 AM

no, I mean to modify it, like:
Code:

#!/bin/bash
exec >/tmp/mylogfile 2>&1
set -xv

echo "my script blabla started"

rm -rf /var/vmail/info/Maildir/.INBOX.autotests/cur/

echo completed

wait # < this wait is not required. What is it good for?

exit 0

and you can check the content of that /tmp/mylogfile

Iotar 01-21-2015 06:38 AM

Thank you, I shall try it.

TenTenths 01-21-2015 09:40 AM

in your script for any commands you are using that aren't shell built-in then use the full path to the command.

For example:

Code:

/bin/rm
instead of
Code:

rm

haertig 01-21-2015 12:58 PM

If something runs from the command line, but not from cron, FIRST, check the environment. SECOND check the environment again. And FINALLY, re-check the environment.

We are talking about ownership, permissions, paths, and environment variables here.

Iotar 01-22-2015 12:07 AM

Quote:

Originally Posted by TenTenths (Post 5304555)
in your script for any commands you are using that aren't shell built-in then use the full path to the command.

For example:

Code:

/bin/rm
instead of
Code:

rm

I tried this. I wrote about it in the first post of this topic. But thanks.

Quote:

If something runs from the command line, but not from cron, FIRST, check the environment. SECOND check the environment again. And FINALLY, re-check the environment.
We are talking about ownership, permissions, paths, and environment variables here.
I can't understand what may be wrong...

pan64, I tried to run this script via cron. Script worked, there is output in /tmp/mylogfile, but nothing to remove in catalog in rm command :(

pan64 01-22-2015 12:25 AM

so script works, crontab works, just the directory was empty?

Iotar 01-22-2015 01:45 AM

Quote:

Originally Posted by pan64 (Post 5304873)
so script works, crontab works, just the directory was empty?

No.

pan64 01-22-2015 02:03 AM

But what? We cannot go further with a simple no....

Iotar 01-22-2015 02:15 AM

Quote:

Originally Posted by pan64 (Post 5304896)
But what? We cannot go further with a simple no....

I can't understand why. Maybe it connected with some permissions, but I don't know where.

pan64 01-22-2015 02:25 AM

so that is where you can add some check/test to the script. For example:
id - to print the identity of the actual user
ls - to show the content of some dir and permissions
type - to check if a command is available
and other things you can find useful.

Iotar 01-22-2015 02:29 AM

Quote:

Originally Posted by pan64 (Post 5304902)
so that is where you can add some check/test to the script. For example:
id - to print the identity of the actual user
ls - to show the content of some dir and permissions
type - to check if a command is available
and other things you can find useful.

Ok, thank you.

Iotar 02-13-2015 02:05 AM

Problem solved by reinstalling cron. Thanks.


All times are GMT -5. The time now is 10:41 PM.