LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Crontab not working ... (https://www.linuxquestions.org/questions/linux-newbie-8/crontab-not-working-836370/)

DanStrong 10-05-2010 01:44 PM

Crontab not working ...
 
Hi there,
I created a very simple cron job for testing my first Linux system but It doesn't work. After open the crobtab file by typing crontab -e, I put a line like this to send email in every 2 minutes to report disk usage:

MAILTO=dan@gmail.com
*/2 * * * * du -s /home

I tried to restart cron several times but I still got nothing worked.
/etc/init.d/crond restart

Please help!
Thank you so much!

kernl 10-05-2010 02:00 PM

Quote:

Originally Posted by DanStrong (Post 4118581)
Hi there,
I created a very simple cron job for testing my first Linux system but It doesn't work. After open the crobtab file by typing crontab -e, I put a line like this to send email in every 2 minutes to report disk usage:

MAILTO=dan@gmail.com
*/2 * * * * du -s /home

I tried to restart cron several times but I still got nothing worked.
/etc/init.d/crond restart

Please help!
Thank you so much!

There could be a number of things wrong, including having a mail server to send the mail.

Try sending it to a local user first, to eliminate the mail server problem.

It is also a good security practice to use the full pathname of any command you use in a cron job, especially if it is run as root.

Check your processes, and see if it is running the command every two minutes. (ps -ef |grep "du -s")

Worst case, you can check your syslog or messages logs and see if there are any errors.

DanStrong 10-05-2010 02:14 PM

Wow, wow ...
It's a super-quick response. I love this forum. Man, It's great.
Kernl,
I just changed it to a simpler cron job like this:
*/2 * * * * echo "testing"
but it doesn't work ether. I think that there is something wrong with cron in my system.
By the way, how can I open the message logs in order to see errors?
Thank you!

DanStrong 10-05-2010 02:56 PM

Hi Kernl,
I used your command (ps -ef | grep "du -s"), I got only one line:
root 16475 3427 0 12:52 tty1 00:00:00 grep du -s

If I used command ps -ef, I got many lines but the times are all 00:00:00.

Is there something wrong in my system?
Regards,
Dan

kernl 10-05-2010 03:49 PM

actually, the time you are referring to is the time the process is using for the command it represents.

Here are some steps for you to see what is going on.

Check to see if anacron, or cron is running on your system:

ps -ef |grep -i cron

you should see something like this:

root 5091 1 0 15:40 ? 00:00:00 /usr/sbin/cron
root 10494 5091 0 16:30 ? 00:00:00 /USR/SBIN/CRON
root 10509 5091 0 16:39 ? 00:00:00 /USR/SBIN/CRON
root 10512 5091 0 16:40 ? 00:00:00 /USR/SBIN/CRON
kernl 10585 10516 0 16:43 pts/0 00:00:00 grep -i cron

If not, your cron is probably not running, and you need to start it.

sudo /etc/init.d/cron start

then check again to see if it is running.

To check your syslog files type: less /var/log/syslog |grep -i cron

that should let you know if you are capturing the cron error messages.


you can also type this in a separate console: tailf /var/log/syslog
or if you don't have tailf on your system: tail -f /var/log/syslog

that will scroll the error messages as they occur.

Hope that helps.

kernl 10-05-2010 03:55 PM

Quote:

Originally Posted by DanStrong (Post 4118601)
Wow, wow ...
It's a super-quick response. I love this forum. Man, It's great.
Kernl,
I just changed it to a simpler cron job like this:
*/2 * * * * echo "testing"
but it doesn't work ether. I think that there is something wrong with cron in my system.
By the way, how can I open the message logs in order to see errors?
Thank you!

By the way, you won't get anything with the echo command, because STOUT is discarded after the command is run. If you want to test cron, try this:
* * * * * echo "Testing">>/tmp/foo.$$

Then go to /tmp, and see if you start getting files with foo.somekindofnumber the $$ at the end will translate to the process number used to generate the command.
Don't let it go too long though, or you might fill up /tmp and that would not be good.

Cheers

repo 10-05-2010 03:57 PM

Quote:

If I used command ps -ef, I got many lines but the times are all 00:00:00.
Is there something wrong in my system?
No, this is normal

First lets see if the cron daemon is running
Code:

ps aux | grep crond
then, take a look in the logfiles to see if there are any errormessages from cron.
Cron will sent a mail to root, if there are problems, so check root's mail
Look in the logfiles to see if there is a problem with the mail.


Kind regards

joec@home 10-05-2010 04:00 PM

Quote:

Originally Posted by DanStrong (Post 4118581)
Hi there,
I created a very simple cron job for testing my first Linux system but It doesn't work. After open the crobtab file by typing crontab -e, I put a line like this to send email in every 2 minutes to report disk usage:

MAILTO=dan@gmail.com
*/2 * * * * du -s /home

I tried to restart cron several times but I still got nothing worked.
/etc/init.d/crond restart

Please help!
Thank you so much!

2 part answer. If cron is not responding then the password for the user may have expired. See if resetting the password for the user account fixes the cron issue. Also it is a common misconception that cron will run bash commands. Sometimes cron will operate in this fashion, but cron is in theory supposed to run scripts, not commands. You may try placing the commands as a script and run the script in cron.

Code:

echo "du -s /home" > /home/username/scriptname
chmod 700 /home/username/scriptname

Add */2 * * * * ./home/username/scriptname to cron


DanStrong 10-05-2010 04:51 PM

Thanks to Kernl, Repo;
Oh man, I just follow your instructions and don't understand nothing. :confused: But for now, that is too much for me. :)

I run the command: ps -ef | grep -e crond, I got 2 lines:
root 15558 1 0 11:49 ? 00:00:00 crond
root 18168 3427 0 14:29 tty1 00:00:00 grep -e crond
that means the crond thing is working, right?

I also changed the cron job as yours:
*/2 * * * * echo "testing" >>/tmp/foo.$$
Yes, I see somthing like:
foo.17928
foo.17944
foo.18004 and so on ...

I try looking for errors but there is no syslogs file in my system. Instead, I see a Cron file. I open it and see something like this:

Oct 5 14:06:01 Legacy crond[17782]: (root) CMD (du -h /home | mail -s "testing" dan@gmail.com)
Oct 5 14:06:01 Legacy crond[17780]: (root) MAIL (mailed 148 bytes of output but got status 0x004e )
Oct 5 14:06:01 Legacy crond[17779]: (root) MAIL (mailed 67 bytes of output but got status 0x004e )
Oct 5 14:08:01 Legacy crond[17835]: (root) CMD (du -h /home)
Oct 5 14:08:01 Legacy crond[17834]: (root) MAIL (mailed 148 bytes of output but got status 0x004e )
Oct 5 14:09:01 Legacy crond[17846]: (root) CMD (du -h /home | mail -s "testing" dan@gmail.com)
Oct 5 14:09:01 Legacy crond[17845]: (root) MAIL (mailed 67 bytes of output but got status 0x004e )
Oct 5 14:10:01 Legacy crond[17860]: (root) CMD (du -h /home)
Oct 5 14:10:01 Legacy crond[17859]: (root) MAIL (mailed 148 bytes of output but got status 0x004e )
Oct 5 14:12:01 Legacy crond[17875]: (root) CMD (du -h /home | mail -s "testing" dan@gmail.com)
Oct 5 14:12:01 Legacy crond[17876]: (root) CMD (du -h /home)
Oct 5 14:12:01 Legacy crond[17874]: (root) MAIL (mailed 148 bytes of output but got status 0x004e )
Oct 5 14:12:01 Legacy crond[17873]: (root) MAIL (mailed 67 bytes of output but got status 0x004e )

I'm using CentOS distro in AsteriskNow. there is no GUI at all but a black sreen for command lines.

Thank you very much for your help,
Best Regards,

repo 10-05-2010 05:03 PM

Your cron is working, your mail isn't.
Make sure sendmail is setup correctly.
What happens if you type
Code:

du -h /home | mail -s "testing" dan@gmail.com
What happens if you send a mail to your username ?
What happens if you sent a mail to your gmail account?
Look in the logfiles
Do you have root access?

Kind regards

DanStrong 10-05-2010 05:27 PM

Thanks Repo,

I check my gmail account but nothing received.
Quote:

What happens if you send a mail to your username ?
Can I sent mail to root? Because I login as root account.
Quote:

Look in the logfiles
Do you have root access?
Could you show me how to do this?

Best Regards,

repo 10-05-2010 05:34 PM

Quote:

Can I sent mail to root? Because I login as root account.
Code:

du -h /home | mail -s "testing" root
To read roots mail
type
Code:

mail
or use pine or mutt

Quote:

Could you show me how to do this?
look in /var/log/maillog

Kind regards

DanStrong 10-05-2010 05:47 PM

Hi Repo,
Here are some msg in Maillog file:
Oct 5 15:32:01 Legacy sendmail[19058]: NOQUEUE: SYSERR(root): can not chdir(/var/spool/clientmqueue/): No such file or directory
Oct 5 15:32:01 Legacy sendmail[19059]: NOQUEUE: SYSERR(root): can not chdir(/var/spool/clientmqueue/): No such file or directory
Oct 5 15:34:01 Legacy sendmail[19075]: NOQUEUE: SYSERR(root): can not chdir(/var/spool/clientmqueue/): No such file or directory
Oct 5 15:34:01 Legacy sendmail[19076]: NOQUEUE: SYSERR(root): can not chdir(/var/spool/clientmqueue/): No such file or directory
Oct 5 15:36:01 Legacy sendmail[19123]: NOQUEUE: SYSERR(root): can not chdir(/var/spool/clientmqueue/): No such file or directory
Oct 5 15:36:01 Legacy sendmail[19124]: NOQUEUE: SYSERR(root): can not chdir(/var/spool/clientmqueue/): No such file or directory
Oct 5 15:38:02 Legacy sendmail[19141]: NOQUEUE: SYSERR(root): can not chdir(/var/spool/clientmqueue/): No such file or directory
Oct 5 15:38:02 Legacy sendmail[19142]: NOQUEUE: SYSERR(root): can not chdir(/var/spool/clientmqueue/): No such file or directory
Oct 5 15:40:01 Legacy sendmail[19157]: NOQUEUE: SYSERR(root): can not chdir(/var/spool/clientmqueue/): No such file or directory
Oct 5 15:40:01 Legacy sendmail[19158]: NOQUEUE: SYSERR(root): can not chdir(/var/spool/clientmqueue/): No such file or directory
Oct 5 15:42:01 Legacy sendmail[19207]: NOQUEUE: SYSERR(root): can not chdir(/var/spool/clientmqueue/): No such file or directory
Oct 5 15:42:01 Legacy sendmail[19208]: NOQUEUE: SYSERR(root): can not chdir(/var/spool/clientmqueue/): No such file or directory

so, what's wrong with the email? How can I get it worked?
Thank a million,
Regards,

DanStrong 10-05-2010 06:05 PM

Hi Repo,
I just figure out that there is no root mail in /var/spool. that is why I could not send email and run command <mail>, right? So, how can I setup a mail thing for send email?
Thanks a lot,
Best Regards,

repo 10-06-2010 01:44 AM

Try this:
Code:

mkdir /var/spool/clientmqueue
chown smmsp:smmsp /var/spool/clientmqueue
chmod 770 /var/spool/clientmqueue
chmod 700 /var/spool/mqueue
chown root.root /var/spool/mqueue
chmod 777      /var/spool/mail
chown root.root /var/spool/mail
chmod 4555      /usr/sbin/sendmail
chown root.smmsp /usr/sbin/sendmail

Or reinstall sendmail
Code:

yum reinstall sendmail sendmail-devel

Kind regards


All times are GMT -5. The time now is 01:19 PM.