-1

On my FreeBSD server, I have setup a cron script for backups using Duplicity. This script is as follows:

#!/bin/sh

export PASSPHRASE=
export FTP_PASSWORD=

keyid=
remote=
excludelist=

/usr/local/bin/duplicity --volsize 1000 --max-blocksize=20480 --asynchronous-upload --full-if-older-than 1M --encrypt-key "$keyid" --exclude-filelist "$excludelist" /Data "$remote"

/usr/local/bin/duplicity --force remove-all-but-n-full 1 "$remote"

unset FTP_PASSWORD
unset PASSPHRASE

(sensitive data redacted, of course).

This script is located at /etc/periodic/daily/duplicity and works fine if I run it directly from the terminal. But it doesn't work when cron/periodic runs it, and that seems te be because env is not functioning correctly.

Initially, the hashbang of the script read #!/usr/bin/env sh, but that resulted in the error env: sh: No such file or directory. So I changed it to the direct path of sh.

The problem I'm currently having is that duplicity uses env internally. So currently, I get the error env: python2: No such file or directory when cron tries to run it.

Why doesn't env work inside cronjobs, and how can I fix this?

Compizfox
  • 375
  • 1
  • 6
  • 17
  • It may not be a matter of problems with `env` but rather the environment variables used when running the script. Try to add something like `env > /tmp/env.txt` inside the script run from crontab to debug what variables are available when running the script. `which python2` gives me `/usr/local/bin/python2` and `/usr/local/bin/` is specifically added to the `PATH` environment variable: `PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/games:/usr/local/sbin:/usr/local/bin` - notice `/usr/local/bin` in the last part. This may not be set up when you run the script from the crontab. – Greg Mar 19 '16 at 15:00

1 Answers1

0

As Amiramix hinted, the problem was with the environment variables. More specifically, the value of PATH.

/etc/crontab contained the following line:

PATH=/etc:/bin:/sbin:/usr/bin:/usr/sbin

After I added /usr/local/bin to that, my script worked as expected.

Compizfox
  • 375
  • 1
  • 6
  • 17