Link to home
Start Free TrialLog in
Avatar of Amin El-Zein
Amin El-Zein

asked on

crontab with space

Hello
I have this command:
sudo php /var/executer.php anyword
when I run it it's work fine
but when I added as cronjob it's not work:
*/1 * * * *  sudo php /var/executer.php anyword
what is the problem ? I think the problem is because the space between executer.php and anyword
thanks.
Avatar of dfke
dfke

Hi,

Probably that's because your cron doesn't have it's environment variables set, so you need to use the full path to the binary files or set a correct path first:

*/1 * * * *  /usr/bin/sudo /usr/bin/php /var/executer.php anyword

Open in new window


Cheers
ASKER CERTIFIED SOLUTION
Avatar of David Favor
David Favor
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I am not sure sudo works correctly in a batch file.  I run all my privileged tasks from cron without using sudo.  Have you tried it without the sudo?
When something is not working in cron I would want to know what is happening or what error you get. Do you get an error, what is it?

If you don’t know the error try adding a redirect to get the error in a file, like this:

*/1 * * * *  /usr/bin/sudo /usr/bin/php /var/executer.php anyword  >> /var/tmp/output.txt 2>&1

Change the output path if needed to a location where your cron user can write.

If your script runs long (around a minute) you may want to change to every 5 minutes (*/5)
Unless you set that /etc/sudoers to run that binary without a password, there's going to be a prompt for the account password if you're running it as some non-root user.  If you're putting it in root crontab, then sudo is entirely superfluous.  Get rid of it.  Using sudo or su in cron is unnecessary.  If you need privilege escalation to run that job, just put it in root's cron.
Hi,

Serialband you're absolutely right about that, my bad I was just focussed on the environment variables.  Run in root's cron without sudo.

Cheers
Avatar of Amin El-Zein

ASKER

Hello,
I notice that the problem that I change the crontab file only.
after I open it from console using sudo crontab -e and save it its work fine ..
so what I can do for that ?
thanks.