Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I have a cron job that runs once a day on a Linux server and in the script it executes there is a test:

# Validate ffmpeg is installed
if [ $(which ffmpeg | grep -c "ffmpeg") -eq 0 ]; then
    echo "error: ffmpeg is not installed!" | tee -a "$log"
    exit 1
fi 

Every day when I check the logfile, the message ffmpeg is not installed! is in the log and as result the work has not been executed. If I run the which test in the shell everything works fine and when I execute the script everything works.

Is there something intrinsic to cron that prevents it from being able to properly use the which command?

share|improve this question
4  
Why not just use [ -x /path/to/ffmpeg ] ... to check it exists and is executable ? tldp.org/LDP/Bash-Beginners-Guide/html/sect_07_01.html –  Iain Apr 1 '13 at 21:11
    
What would be the point of the test if you already know that ffmpeg is installed and its installation path? This test ensures that users don't try to run the program on a server which doesn't have ffmpeg installed. –  Neal Bailey Apr 2 '13 at 21:24
add comment

2 Answers 2

up vote 7 down vote accepted

At the top of your crontab file put SHELL and PATH declaration like:

SHELL=/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

See this SE post for more details.

The default SHELL and PATH for cron are SHELL=/bin/sh, PATH=/usr/bin:/bin (From man 5 crontab man page).

share|improve this answer
    
Thanks! I'll give this a try. –  Neal Bailey Apr 2 '13 at 21:25
add comment

It probably does not pick up your path since it's running from cron. There are several ways to make it know the path the easiest I have found is just hard code the path to which.

Do a:

locate which

Mine returns this:

[user@server ~]$ locate which |grep bin
/usr/bin/which

Then change your script to do:

if [ $(/usr/bin/which ffmpeg | grep -c "ffmpeg") -eq 0 ]; then
    echo "error: ffmpeg is not installed!" | tee -a "$log"
    exit 1
fi 

Another option is to set the environment and path in the crontab:

SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
share|improve this answer
1  
You could also use the rather humorous which which. –  Christian Mann Apr 2 '13 at 0:46
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.