Difference of running scripts manually or with a cronjob

5,417

Solution 1

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)

Solution 2

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.

Solution 3

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:
5,417

Related videos on Youtube

Björn
Author by

Björn

Updated on September 17, 2022

Comments

  • Björn
    Björn almost 2 years

    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

    • Admin
      Admin over 14 years
      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.
    • Admin
      Admin over 14 years
      Sure, I could do that - but as you says it will not fix my problem.
  • Björn
    Björn over 14 years
    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 :)
  • Douglas Leeder
    Douglas Leeder over 14 years
    It might be an environment affecting MEncoder?
  • Björn
    Björn over 14 years
    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
    Björn over 14 years
    I don't have a webserver installed. But thanks for input.