Reputation: 37
So I THINK the prob is most likely path. But I am confused on how to correctly set this.
So the top of my script looks like:
#!/bin/sh
MYSQL="$(which mysql)"
if [ -z "$MYSQL" ]; then
echo "Error: MYSQL not found"
exit 1
fi
when I run the script in ssh, it works prefect! But when I setup the cron, using these commands in directadmin:
/home/username/script.sh
also tried:
/bin/sh /home/username/script.sh
Both of them give me the following error:
Error: MYSQL not found
So here is what I found online:
SHELL=/bin/sh
PATH=/etc:/bin:/sbin:/usr/bin:/usr/sbin
HOME=/var/log
I assume I need to add at least one of the above statements in my script. The server is a freebsd server and I assume everything is in the default location. I know when I do a whereis sh it returns /bin/sh
I have no idea how to check PATH or home. I'm new to freebsd so any help would be great.
Upvotes: 1
Views: 398
Reputation: 532
This is because of PATH issues. You can either specify path variables within your crontab file as per above answers, or modify your script.
In case you decide to decide on the latter, here's what's needed to be modified,
Now you can cron the script using - "/bin/sh /path/to/your/script", in the crontab. You do not need to add any extra path variables in the crontab file in this case.
Hope this helps.
Upvotes: 0
Reputation: 1357
You can read the env(1) manpage and use it both to diagnose & fix your problem, or just fix the environment from within the script if you prefer.
As you mentioned it's a freebsd server, my guess is that you have mysql installed under /usr/local - which isn't included in cron's default $PATH.
Upvotes: 0
Reputation: 9618
Crontab jobs are submitted by the system and do not execute the normal system startup script (which sets PATH and similar things).
I do all my work in Korn shell on Solaris and add lines similar to these at the beginning of all my scripts:
#!/bin/ksh
#-------------------------------------------------------------------------------
# /opt/app/batch/daily_dns.ksh Daily DNS Batch process via crontab
# 16 3 * * * /opt/app/batch/dns/bin/daily_dns.ksh > /opt/app/batch/daily_dns.log
#-------------------------------------------------------------------------------
. /etc/profile
You probably need to do something similar.
Upvotes: 1
Reputation: 2675
I'm only making an educated guess as I'm only familiar with Debian, but cron runs tasks as root and runs them non-interactively. This means that it gets the same path as your root user and usually non-interactive prompts exit from .bash_profile before paths are set.
TL;DR: Check that your root user has MYSQL variable and check that root user's .bash_profile does not exit on non-interactive prompts.
Upvotes: 0