Cron wont run Command

txcowboy99

Verified User
Joined
Dec 12, 2005
Messages
21
I have a command that I can run perfectly from the command line, but when I set it up to be run via cron it just does not run (as far as I can tell).

I enter exactly the same command I run from the command line into cron via DA and just doesn't run.

I am not a *nix person and I am reading the cron log by going 'cat cron' and I can see other cron jobs that have run.

All help greatly appreciated.
- Matt.
 
Can your provide the command you are trying to run?

Do you have root access to this server?
 
Yeah I have root access. the command is more like a sequence of commands, here it is:

cd /home/mydomain/public_html/idx/admin/ && php {php script} > /home/mydomain/public_html/cronjobs/logs/script.log

I have a reseller account with root access. I have set up another acount that has shell access. I run the command within this account from the command line and the command runs perfectly, however if it set it up to run via cron through DA it doesn't run.

All help greatly appreciated, I'm really lost with this.

Matt.
 
For one, {php script} is not valid, unless you intentionally changed it ;)

You might also need to specify the path to the php binary.
For example
Code:
/usr/local/bin/php /home/admin/myscript.php
 
I've tried adding the path the php binaries but I always end up with the same problem. Runs from the command line, wont run when scheduled via cron.

When running things via cron, do they run in a different context/environmnet/under different priveldges than when run from the command line?

I come from a Windows background, but this problem almost looks like cron doesn't have sufficient rights/access to run the script and commands.

Does this thing happen in *nix environ's?

Thanks for all help,
Matt.
 
Yes permission problems are quite common in linux.

Try piping the email notification to your email address so you can get more information on the failure of the job.
 
Cron jobs created in a user interface are run with the same rights as the user that created it.

The only way you can test that is to (perhaps temporarily) create an ssh login for that user, and try it from the command line while logged in as that user.

The best way to run a cronjob that includes multiple commands is always to create a shell script with those commands in it. Have you done that?

As Jon points out, you must have full paths in the cronjob, and also in the script.

Jeff
 
Yes I have also created a hash script to run the commands and have come across the same problem. Runs from the command line, fails when run via cron.

I log in as the user and run the command from the command line and look for the desired results, all's well. I then run the command via cron (using the DA cron interface I should mention, not sure I put that bit in) and the script fails.

Here's the command I enter, both into cron and the command line:

/bin/bash /home/mydomain/public_html/cronjobs/script.sh > /home/mydomain/public_html/cronjobs/logs/script.log

Here's the script:

#!/usr/local/bin/php
cd /home/mydomain/public_html/idx/admin/ && php script.php

Thanks for your help with this, I really appreciate it.
Matt.
 
Just a few things that may help?

I need that cd... command (cd /home/mydomain/public_html/idx/admin/) as the php script I need to run lives in the admin folder.

I put the && in because I had the <php script.php> command on a different line, but it would just not run when there, but it does after the &&'s.

Matt.
 
You've brought up another point, Matt.

You write that your script is in the admin directory, but you don't write whether it can run as the user executing the cron job.

Jeff
 
Here's the path to the script:

/home/mydomain/public_html/cronjobs/script.sh

as listed in the command I run to kick things off. Here's the contents of the script:

#!/usr/local/bin/php
cd /home/mydomain/public_html/idx/admin/ && php script.php

the script is called 'script.sh' as listed in the command above.

Is that what you mean Jeff? I'm flying a bit blind here. I hope this is what you mean?

Thanks again,
Matt.:confused:
 
> You've brought up another point, Matt.

You write that your script is in the admin directory, but you don't write whether it can run as the user executing the cron job.

This is starting to sound like we're getting somewhere, this sounds like exactly what I've been thinking.

How do I do this?

Thanks,
Matt.
 
I'm a little curious on your script.
Why are you calling the php interpreter in a file that does not contain any php commands.

If it really is a shell script it should look something like
Code:
#!/bin/sh
cd /home/user/example.com/public_html/
/usr/local/bin/php script.php
 
That's fine. The reason it's like that is because I really have no idea what I'm doing.

I'm fumbling around in the dark hoping I stumble across an answer.

The script originally had #!bin/bash as its' first line. I guess I changed it in an attempt to find and answer.

I'm still wondering what Jeff means by:
You write that your script is in the admin directory, but you don't write whether it can run as the user executing the cron job.

All help greatly appreciated.
Matt.
 
I think he's referring to the admin user directory (/home/admin)

A cronjob can only execute scripts of which it owns. (i.e. Not in another user's directory)

You should create the shell script as suggested before and have the cronjob execute that. Then add checks to the script to notify you of it's progress in some ways. If the php script is executed then move onto that and see where the problem is.

Debugging is so much fun isn't it! :p
 
Sorry everyone, but I'm getting lost. Are you able to expand on what you mean here?

> A cronjob can only execute scripts of which it owns. (i.e. Not in another user's directory)

Sorry, but I'm doing my best to understand, it's just not my area of expertise I guess.

Many thanks,
Matt.
 
I just lookd at var/log/cron and this is the last line:

<date> <time> <host> crontab[3936] (root) REPLACE <userid>

I've changd the things in <>'s for privacy. But does that mean anything to anyone? It obviously tells me that crontab has run job id 3936 on the date time listed but I don't know what root, replace and userid indicate?
 
> A cronjob can only execute scripts of which it owns. (i.e. Not in another user's directory)
i guess that simply means the user that want to run the script has to own the script. if you want to run the script as Mr.ABC then the script must be own by Mr.ABC and it has to be in a folder that MR.ABC has access rights to.

if you login as the user and can run the command, cron should be able to run it as a script. you probably need to tweak your script.

maybe you can try this:

login as the user who wish to run the script. make sure the php file is owned by the user.
create a script with content below:
======================
#!/bin/sh
/usr/local/bin/php /home/mydomain/public_html/idx/admin/yourphpscript.php
======================
save it as script.sh and chmod 755 script.sh

create a cron job to launch the script.
crontab -e
*/5 * * * * /full/path/to/your/script.sh

wait for 5 minutes and check whether your script runs. if it runs, change the cron schedule to something else as the above will run every 5 mins

lets see what we get
 
Last edited:
Back
Top