Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I have a PHP script that I want to run every 10 minutes, going through database records and creating/editing movies through MEncoder. I've set up cron job to accomplish this, but it doesn't work very well.

I have my PHP script, call it document.php. I also have a bash script (document.sh) to call this PHP script;

#!/bin/bash
php document.php

Now comes the weird part: When I manually run the bash script everything works great - the movies are created or edited just the way I want them - but when I let the cron job run the bash script every 10th minute the movies are corrupt. They have some frames in them, though. I have no clue how this can happen. I've checked the permissions and file paths and everything looks fine. Does the cron job kill my process? Does it have a time limit (it takes roughly 1-2 minutes to run MEncoder through the files)?

Hope someone can provide an answer - this really bugs me and deadline is near.

I'm running Ubuntu Server with the latest updates and the latest MEncoder from the repo.

Best regards, Björn

share|improve this question
    
Not that this will fix your problem, but can't you just put php document.php in your crontab and eliminate the Bash script? I agree with Christopher Karel that it's probably an issue with environment variables, however. –  Dennis Williamson Dec 18 '09 at 17:54
    
Sure, I could do that - but as you says it will not fix my problem. –  Björn Dec 18 '09 at 18:00

5 Answers 5

up vote 8 down vote accepted

Running from cron doesn't pull in your shell configuration files (~/.bashrc or ~/.profile or /etc/profile) Is it possible there are environment variables defined there that are affecting your job? Try sourcing your profile files manually at the start of your script. (. /path/to/profile)

share|improve this answer
    
I will try this tomorrow, I can't access the servers from where I am now. But I doubt this will solve my problem, since the files are created from MEncoder and they contain data. I use absolute paths for my files and the environment variables should not affect the PHP script. But thank you for you input, I will try this tomorrow :) –  Björn Dec 18 '09 at 18:06
1  
It might be an environment affecting MEncoder? –  Douglas Leeder Dec 18 '09 at 21:14
1  
LD_LIBRARYPATH is a good place to start looking. –  Chris Nava Dec 19 '09 at 4:26
    
You guys rock! This was the problem -- probably MEncoder needed some environment variable. Excellent. Thanks! Too bad I cannot upvote -- I need more rep first ;) –  Björn Dec 19 '09 at 8:32

As others have stated, the number one most likely issue is the lack of some environment variables. If that's not it, the other possiblity I can think of is: Are you sure the job only takes 1-2 minutes? if it takes > 10 (your interval for respawning it), you could end up with multiple runs trying to modify the same files, which could cause some of the corruption you mentioned.

Debugging cron is always... fun. first off, check root's email on that system; if the cron job put anything on stdout, it should be there. If nothing useful showed up there, you can make something show up there by putting some debug output into your script.

share|improve this answer

My simple solution is to call the php file with LYNX Assuming you have an http server running

lynx http://127.0.0.1/document.php

share|improve this answer
    
I don't have a webserver installed. But thanks for input. –  Björn Dec 19 '09 at 8:33

There shouldn't be any time limit - at least by default, not sure about this distro. Do you receive the cron email? If not, redirect stdin/stdout to a file and see what the process says.

your-cmd  > filename.txt 2>&1
share|improve this answer

That's a very common problem with cronjobs--you simply cannot assume your environment variables, paths, etc. What I like to do is to have a cronjob that does "set > /tmp/set" so you can see exactly what you're going to have from the point of view of the cronjob. This way you can compare the assumptions your script makes, and what adjustments need to be made.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.