I have a script that works when I run it from the command line, but when I schedule it with cron
I get errors that it cannot find files or commands. My question is twofold:
When I schedule a cron job using
crontab -e
, does it use my user ID as the basis for its permissions? Or does it use a cron user ID of some sort and its related permissions?When a cron job is launched, what is the working directory? Is it the directory where I specify the script to run, or a different directory?
Here is my cron job:
15 7 * * * /home/xxxx/Documents/Scripts/email_ip_script.sh
Here is the actual script:
vIP_ADDR="`curl automation.whatismyip.com/n09230945.asp`"
echo "$vIP_ADDR"
sed "s/IPADDR/$vIP_ADDR/g" template.txt > emailmsg.txt
ssmtp XXXXX@gmail.com < emailmsg.txt
Here are the errors I get when I view the mail
message produced by cron
:
sed: can't read template.txt: No such file or directory
/home/xxxx/Documents/Scripts/email_ip_script.sh: line 15: ssmtp: command not found
It cannot find the template.txt
but it resides in the same directory as the script. It also cannot run ssmtp
, but I can as my user. What am I missing to get this to work properly?
cron
have it's ownPATH
or can I check my user'sPATH
? I set ssmtp up to have it's ownuser
andwheel
permission thinking it would allow anyone to use it (including cron). If it helps Im on CENTOS 6.2 – ProfessionalAmateur May 18 '12 at 15:51ssmtp
, but that your cron job doesn't find any executable calledssmtp
because it's not in yourPATH
. There's no such thing as “your user'sPATH
”; this is a per-process setting, not a per-user setting. You can set the path for all your cron jobs by putting aPATH=…
line in your crontab. – Gilles 'SO- stop being evil' May 19 '12 at 08:45´which ssmtp´ ...
– Fredrick Gauss Nov 09 '17 at 10:40type ssmtp
– Gilles 'SO- stop being evil' Nov 09 '17 at 21:54Command 'cd' not found in /usr/bin, (...)
. – Philipp Ludwig Oct 29 '22 at 14:04