My cron doesn't work but 'sudo -u john foo.sh' does
Linux - SoftwareThis forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
When you execute things with cron, you don't get all the environment variables you get when you log inn normally. My guess is the PATH variable, so here's my suggestion:
log in as "john"
type "echo $PATH" and copy the entire value
type crontab -e
In the beginning of the crontab file, add setting the PATH, e.g.:
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/bin/X11:/usr/games
I have troubleshot many many many cronjob failures over the years. In the case where it runs when manually invoked from the commandline, but not when invoked from cron, it has ALWAYS turned out to be an environment problem or permissions problem, just like Guttorm has already said.
From a command prompt, as the userid that you want to run the cronjob from, run "env" and trap it's output to a file. Then create a cronjob using this same userid and have it run "/usr/bin/env > /tmp/my.cron.env". Notice that I put the fully qualified pathname to the "env" command in that cronjob. /usr/bin would probably(?) be in a normal cronjob's environment, but why take chances? It's good form to be 100% explicit when messing with cronjobs.
After the cronjob has executed, compare the two environment files. Look for something that is missing that your job might require.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.