Solved [Solved] env returns "wrong" path when used in cronjob

Hello forum,

I'll break down my problem into sort of a Q&A style monologue, so please bear with me.

Q: What am I trying to do?
A: I want to set up duply to run every hour. (http://www.freshports.org/sysutils/duply/)

Q: What is the problem?
A: When running duply from shell as root, everything seems working just fine. But as soon as I try to automate this by using cron, nothing happens.

Q: Do you get any error messages? What does the log say?
A: Thanks, now I've got "What does the log say?" stuck in my brain. Anyway, I'm receiving an error message claiming that bash is not in the path of root. (Or so I understand.) Error message:
Code:
Subject: Cron Daemon
Cron <root@hostname> /usr/local/bin/duply hailmarrybackup 
env: bash: No such file or directory

Q: Are you sure that you set the path correctly?
A: I'm trying my hands on this for pretty much the first time. So I'll guess the answer is no. This is what I'm getting when I run env | grep -e PATH from my shell.
Code:
PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/games:/usr/local/sbin:/usr/local/bin:/root/bin
This is what I'm getting when I set a cronjob to run env | grep -e PATH > /tmp/cronpath.log.
Code:
PATH=/usr/bin:/bin

Q: Please examine the username stated in the respective env output. Do they match?
A: No, but I've got no idea why. But does it matter, since I configured the cronjob to be run as root by editing the crontab using this command:
Code:
crontab -e -u root

Thank you for your patience,

-frozen


05-01-2013 15:00 UPDATE
I tried setting the PATH variable at the top of the script just to see if this would help - it did not.

05-01-2013 15:24 UPDATE
I added the cronjob line to /etc/crontab and ran service cron restart. IT WORKS.
To clarify here is what I cut from crontab -e -u root:
Code:
25      *       *       *       *       /usr/local/bin/duply vollbackup backup_verify_purge --force

I added the username and put it into /etc/crontab:
Code:
25      *       *       *       *       root    /usr/local/bin/duply vollbackup backup_verify_purge --force
Is there any downside to this, besides the fact that this cronjob won't show up as conveniently when using crontab -e ?

05-01-2013 15:58 UPDATE / SOLVED
Dear helpful people on the internet, Today I learned that the magic of cron, specifically the environment variables, can be set either by editing /etc/crontab or simply declaring them in the user specific crontab using crontab -e -u root. This was a valuable lesson!

Your faithful student,
Neck-beard Sparkle
 
Re: env returns "wrong" path when used in cronjob

There should be a PATH setting in /etc/crontab, does that help?
 
Re: env returns "wrong" path when used in cronjob

I took a look at that file, but unfortunately it already mirrors the exact line I get when running env | grep -e PATH from my shell.

Output of grep PATH /etc/crontab:
Code:
PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/games:/usr/local/sbin:/usr/local/bin:/root/bin

Thanks anyway,

-frozen
 
Re: env returns "wrong" path when used in cronjob

if you installed this cron job using crontab -e -u root, then you need to re-run that command and set PATH in that file; the /etc/crontab file is treated separately from those created using the crontab command.
 
Re: env returns "wrong" path when used in cronjob

Shouldn't you be specifying the full path to hailmarrybackup?

I'm not so sure you are understanding what the error message is really telling you. Try something stupid in your cron command, like bash --version.
 
Re: [SOLVED]env returns "wrong" path when used in cronjob

Thank you.

That shouldn't be necessary because this is just a parameter determining the backup profile handled by the script that is executed.

-frozen
 
Re: env returns "wrong" path when used in cronjob

The only downside I can see from adding this to /etc/crontab is that it appears that you are touching a "base" system file that you would have to deal with merging if/when you were to ever update/upgrade your system. The crontabs created with the crontab command are kept in a separate place and you wouldn't have to worry about them.
 
Re: [SOLVED]env returns "wrong" path when used in cronjob

It felt like a quick and dirty kind of fix, so I kept looking. I put the solution in my original post.

Thank your input, although the post is marked as solved.

-frozen
 
Back
Top