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:

  1. 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?

  2. 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 [email protected] < 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?

5 Answers 5


Add cd /home/xxxx/Documents/Scripts/ if you want your job to run in that directory. There's no reason why cron would change to that particular directory. Cron runs your commands in your home directory.

As for ssmtp, it might not be in your default PATH. Cron's default path is implementation-dependent, so check your man page, but in all likelihood ssmtp is in /usr/sbin which is not in your default PATH, only root's.

15 7 * * * cd /home/xxxx/Documents/Scripts && ./email_ip_script.sh
  • @Giles - Thanks, would cron have it's own PATH or can I check my user's PATH? I set ssmtp up to have it's own user and wheel permission thinking it would allow anyone to use it (including cron). If it helps Im on CENTOS 6.2 Commented May 18, 2012 at 15:51
  • 5
    @ProfessionalAmateur Your problem isn't that you aren't allowed to use ssmtp, but that your cron job doesn't find any executable called ssmtp because it's not in your PATH. There's no such thing as “your user's PATH”; this is a per-process setting, not a per-user setting. You can set the path for all your cron jobs by putting a PATH=… line in your crontab. Commented May 19, 2012 at 8:45
  • I had to add MAILTO='[email protected]' as well to get it working along with PATH setup. Wierd but it worked for me.
    – mac
    Commented Jun 8, 2014 at 9:56
  • For ssmtp one can use; ´which ssmtp´ ... Commented Nov 9, 2017 at 10:40
  • @FredrickGauss Make that type ssmtp Commented Nov 9, 2017 at 21:54

If your cronjob is a bash script, the following will CD to the location of your script (assuming that you're using absolute path in your cron definition):

cd "$(dirname "$0")";
  • The question was where is the default working directory. I'm now investigating the results of a command that run in the default working directory, and this answer doesn't help me investigate that.
    – Flimm
    Commented Mar 7, 2022 at 9:14
  • @Flimm it sorts of answer the question by implying the current directory is not the directory the script runs in
    – vdegenne
    Commented Nov 2, 2022 at 11:38

To answer question 1: if you run crontab -e as your own user the jobs will be scheduled in that user's crontab and will thus run with the permissions of that user.

But you need to consider that the jobs will run in a non-interactive shell meaning that the $PATH might be different from the one you have when running the script from the command line.

It is best to always use full paths in scripts, especially if you plan to schedule them via at/cron etc.

I would also recommend using full paths to all files to avoid exactly the problems you see.

To prevent race conditions and other security issues you should also use mktemp to make sure the file you read is not modified by anything outside your script.

So I'd change the script to something like:

vIP_ADDR="`curl automation.whatismyip.com/n09230945.asp`"
echo "$vIP_ADDR"
/bin/sed "s/IPADDR/$vIP_ADDR/g" /home/xxxx/Documents/Scripts/template.txt > $mailmsg
/path/to/ssmtp [email protected] < $mailmsg
/bin/rm $mailmsg

cron runs each user's scheduled jobs as that user. This should be enough for us to work out that it runs your scripts relative to your home directory.

If you need it to run from a different location, simply use cd in your script to go to that location.

ssmtp is probably not in cron's default PATH (it is set to be very narrow by design on most platforms). You can either specify the full path to ssmtp in your script, or you can explicitly set PATH in a) your crontab file, which will be available to all your scripts, or b) in each script.


Some people have hinted or linked at it but the best way to find out since I can't find it in the man docs for my distro is just add this to a cron

* * * * * echo "$PWD" > /tmp/lolcronjobs

Then cat out lolcronjobs to see what your working directory is. The same works for finding $PATH and other environment variables.

  • 3
    You can also run pwd > /tmp/pwd.txt
    – Flimm
    Commented Mar 7, 2022 at 9:17

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .