What's wrong with my Cron?

software development


For security reasons, I am anonymizing the path to my file and my email. Trust that it is written correctly in the actual cron. That has been checked and rechecked as I try to solve this.

*/10 * * * * sh -c $’/usr/local/php5/bin/php /home/myuser/mysite/myfolder/myfile.php’

myfile.php has the following permissions: (chmod 744)
-rwxr–r-- myfile.php

I get nothing in my email from the cron.

If I run the script (sh -c $’/…) from an SSH session, I get nothing. Nothing that is, unless I am running it from the /home/myuser/mysite/myfolder directory.

If I run it from there, it works just fine. What can I do to fix this?


Why are you running sh -c? I’d leave that off so it’s just:
/usr/local/php5/bin/php /home/myuser/mysite/myfolder/myfile.php

And if it turns out you need to run it from myfolder, make the command:
cd /home/myuser/mysite/myfolder ; /usr/local/php5/bin/php /home/myuser/mysite/myfolder/myfile.php


Honestly, I set it up through Dreamhost’s make it for me panel. They added the sh -c bit.

I should have thought of the cd solution, although there is probably a real problem somewhere.[hr]

…and the cd solution worked. I rewrote the command outside of the dreamhost block, which may have helped.

I’m still curious why I needed to be in that directory, but as long as it works I am not going to complain.


The answer to your last question is rather obvious: It’s because your cronjob starts in some “random” directory, which is not the one your script is in. That’s just how life is :wink:

2 solutions:

  1. Change the directory in the cronjob line
  2. Write the script so that it knows how to run from a strange directory


Cronjobs are always run with the cwd set to your home directory.


Right, but if both paths in the script have universal paths, that is, they start with /, shouldn’t they run from anywhere on that server?


Some scripts expect that you’re running it from its own directory. For example, if your script calls for a config file, it wants it to be in ‘cwd’ which is your Current Working Directory. Scripts don’t always know a reliable path to to needed files, so it’s usually safest to run the script from its own directory.