0

I found that setting my crontab PATH=/home/toothrot/bin worked, but $HOME/bin did not. And I checked that $HOME is set as expected. Does cron not expand environment variables? I tried using it by calling a script in the path.

* * * * *   fetchmail

This script writes a log file when PATH is set to the full explicit path, but not when using $HOME, presumably because in the latter case it is not run.

5
  • 1
    How did you use it?
    – Jeff Schaller
    Sep 4, 2019 at 12:23
  • @JeffSchaller, see edit.
    – Toothrot
    Sep 4, 2019 at 12:26
  • What's output of tr '\0' '\n' < /proc/cron-pid/environ | grep HOME? Sep 4, 2019 at 12:28
  • @ArkadiuszDrabczyk, zsh: no such file or directory: /proc/cron-pid/environ
    – Toothrot
    Sep 4, 2019 at 12:30
  • You didn't get it, you need to replace cron-pid with the correct PID you can find using ps aux | grep '[c]ron' Sep 4, 2019 at 12:31

1 Answer 1

3

You didn't specify what cron implementation you are using, however at least in Debian based systems (where it is derived from Vixie cron IIRC) it does not. From man 5 crontab:

       An  active line in a crontab will be either an environment setting or a
       cron command.  The crontab file is parsed from top to  bottom,  so  any
       environment  settings  will affect only the cron commands below them in
       the file.  An environment setting is of the form,

           name = value

       where the spaces around the equal-sign (=) are optional, and any subse‐
       quent non-leading spaces in value will be part of the value assigned to
       name.  The value string may be placed in quotes (single or double,  but
       matching)  to  preserve  leading or trailing blanks. To define an empty
       variable, quotes must be used. The value string is not parsed for envi‐
       ronmental substitutions or replacement of variables, thus lines like

           PATH = $HOME/bin:$PATH

       will not work as you might expect. And neither will this work

           A=1
           B=2
           C=$A $B

       There will not be any subsitution for the defined variables in the last
       value.

       An alternative for setting up the commands path is using the fact  that
       many shells will treat the tilde(~) as substitution of $HOME, so if you
       use bash for your tasks you can use this:

            SHELL=/bin/bash
            PATH=~/bin:/usr/bin/:/bin

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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