Saturday 1 September 2018

ubuntu - cron runs but nothing happens


I have created a script called "forward_email.sh" and set permissions to 777. It contains the following line:


echo "It worked: $(date)" >> /home/noc/email.log

I created a cron job with the following line:


*/5 * * * * /home/noc/forward_email.sh

The problem is that the "email.log" file never gets changed. I even set the permissions to 666. When I run the script manually, it works perfectly.


The cron job is running according to the "/var/log/cron.log" file.


Can someone point me into the right direction as to what might be going wrong? This is a minimal virtual machine install of Ubuntu Server 9.04 with cron installed via apt-get.


UPDATE: I made a very stupid mistake. I named my script "forward_mail.sh". I wasn't until I installed some email services and got the following error email from Cron that I discover my mistake.


/bin/sh: /home/noc/forward_email.sh: not found

I changed the name of the file, and it works now.



Answer



Lots of potential things off the top of my head.


Are you sure the crontab entry is really pointing at your script? For example, if you want to run a script such as ~noc/bin/script.sh, but have an entry like /home/noc/bin/script (ie a subtle typo) then of course it won't work. I usually check my crontab entries by copying the command out of crontab with my mouse, then pasting it into a command prompt, just to be 100% sure that it is going to run what I want it to.


Does your script contain only the indicated line? Usually you have to include something like:


#!/bin/bash

...at the top of your script to get it to work.


If it is more complicated than the single line, are you sure the programming logic will permit it to run that particular line?


Try reducing your script to


#!/bin/bash
echo "Bing!" >> /tmp/cronjob

...to see what happens. If that works, you have a programming issue in your cron script. If it fails, you have a cron issue.


Is the 'noc' user the one running the script? If not, are you sure that the user running the cron script has read/write access through /home/noc to the email.log file?


Where is the error email from the user running the cronjob going? Is an error email being generated, and then getting sent somewhere you are not expecting or maybe getting dumped altogether? Try a cronjob of


#!/bin/bash
echo "Bing!"

...and then try to figure out where the email that is generated goes.


Are cron.deny and/or cron.allow in play? If /etc/cron.allow does exist, then the user noc must be listed in it; if /etc/cron.deny exists. the user noc must not be listed in it. On RedHat, if neither cron.allow nor cron.deny exist, then ONLY the root user will be permitted to use cron.


No comments:

Post a Comment

Where does Skype save my contact's avatars in Linux?

I'm using Skype on Linux. Where can I find images cached by skype of my contact's avatars? Answer I wanted to get those Skype avat...