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.
PHPLinuxSystem Programming

Avatar of undefined
Last Comment
Amin El-Zein
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

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Dr. Klahn
Dr. Klahn

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)
Avatar of serialband
serialband
Flag of Ukraine image

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.
Avatar of dfke
dfke

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
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.
PHP
PHP

PHP is a widely-used server-side scripting language especially suited for web development, powering tens of millions of sites from Facebook to personal WordPress blogs. PHP is often paired with the MySQL relational database, but includes support for most other mainstream databases. By utilizing different Server APIs, PHP can work on many different web servers as a server-side scripting language.

125K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo