Now Place:2uzhan.com » cron script not properly executing

cron script not properly executing

Ubuntu @ June 4, 2010   Views:0

First of all, so sorry about this seemingly silly cron question. I know a lot are asked, and are usually just environment related.

Anyway, I wrote a very simple PHP script to mount my external drive to a specific location as soon as it's switched on.
The script works great when run.
What's strange, is that it runs in cron and generates a log file, but for someone reason doesn't mount the drive when cron runs it.

I added in the full path to EVERYTHING in the script, and I'm calling it with the full path from crontab, but it doesn't mount my drive when run from cron.

I've tried running the individual commands on the cmd, and it all works. Starting to annoy me now... anyone have an idea?


Last I checked, cron (or rather run-parts) had problems with anything with a dot in the filename, like files ending in ".sh".

edit: related bug report https://bugs.launchpad.net/ubuntu/+s...ils/+bug/38022

never had a problem with that, but I removed the .php from my script and still nothing.
Thanks for the quick reply btw.

Could this be because cron isn't allowed to mount drives? I am using sudo in the script, but would that try to do sudo as the cron user? Obviously that would fail.
Perhaps a more correct question would be: is cron running as my user or it's own user?

Cron runs scripts as root. You don't need sudo in it. And I've had no trouble mounting drives from cron scripts.

Do you have any logging to determine whether the php script is running at all?

Did you add the full php command line to run it to your cron tab, or did you put the script in one of the existing cron folders (like cron.daily)? If the latter, the script would have to be marked as executable, and the first line of the script would have to specify the php command to run it, "#!/usr/bin/php -q".

Cron scripts should run as the user that the cron entry is for. NOT always as root as has been suggested.

EG as user run


crontab -l

then do


sudo crontab -l

You should see they are different.

EG here is mine


[email protected]:~$ crontab -l
no crontab for callum
[email protected]:~$ sudo crontab -l
[sudo] password for callum:
no crontab for root
[email protected]:~$

If you want to be sure you are running it as root (like your script requires) be sure to put your script in roots crontab and not your users.

There are often subtle differences between running things with cron versus the command line. For instance, cron generally runs with a very limited PATH, so sometimes a command that otherwise runs fine is invisible to cron.

Have you tried sending the output of cron to an output file? Example:


0 20 * * *  /home/user/script_to_be_run.sh > /home/user/OUT_FILE 2>&1

Then after cron runs the next time, you can check the output and hopefully get a better idea of what went wrong. BTW, the "2>&1" means, "send both regular script output and error messages to the same file", which in this case is OUT_FILE.

I've added absolute paths in cron as well as in the script (e.g. /usr/bin/cut). So that's covered.
I also tried outputting to a file, which gets updated at the cron run time, so the script is being called for sure.

I also just added it to roots crontab, yet nothing! I never had this problem before. Is ubuntu 10 to blame???

Could you post the output of "crontab -l" please? Also, are you sure the cron command is not working? In other words, is the mount working but then something is later unmounting the drive? You could add something to your script to check to see of the mount worked, such as "ls -l /mnt/mydrive/something".

I don't think 10.04 is to blame. All of my cron commands are running fine, although I don't have one which mounts drives.

* * * * * /usr/bin/php /home/sia/check_ext
That's crontab.
Nothing is unmounting the drive. If I mount it or rn the script manually, it remains mounted.

I added another command to ls -l /media/External RIGHT after the actual mount command, and told that command to output to mnt.log. mnt.log was created, and said "total 0".

This proves that the script run by cron can't mount... which is strange.

© 2018 2uzhan.com Contact