0

I have a command-line program that is periodically run by services (either as a cronjob or by systemd) and it heavily depends on environment variables which are not static (that is, they might change) which makes it impossible to create a file and source it in the service script (as stated in this question).

Is there a way to access environment variables from a UNIX service (crontab or systemd) dynamically?

2
  • 3
    Each process on the system has its own individual set of environment variables, parent inherited from child, that it can modify at whim. Not understanding that leads to confusion such as this, and wrong ideas that there are somehow "static" and "dynamic" environment varibles. You have a program that uses and expects certain environment variables inherited from its parent. You want to have cron and systemd, the parents of the process that is running your program, set those environment variables from some source. You need to explain what source that is.
    – JdeBP
    Jan 8, 2017 at 10:58
  • What @JdeBP says above is correct. I'd like to expand on it a bit: a process inherits its parent process' environment, but once it is inherited, only the process itself can change it. That is, changes to the parent's environment are not inherited. A process can also no change the environment of its parent. Jan 8, 2017 at 13:18

1 Answer 1

1

As I learnt from a previous answer in StackExchange, crontab knows nothing about your environment. So you need to explicitly source it before launching your script; in such a way the following script will have its own environment:

*/20 * * * * . $HOME/.profile; <your_script>
4
  • 1
    It would be better if the script itself sourced a file that contained the appropriate environment variables and their values. The .profile of a user should only be read by interactive or login shells.
    – Kusalananda
    Jan 8, 2017 at 11:23
  • Thanks @Kusalananda ... you mean: looking for variables needed by the script (from ENV) and saving only them into a file. Than sourcing just that file? I undertood well?
    – Echoes_86
    Jan 8, 2017 at 11:37
  • A good setup would be, I think, to have a separate myscript.env file that is sourced by the script myscript. Yes, the author of myscript would know what variables need to be set, and set them in the myscript.env file. I still don't know what a "dynamic environment variable" is, but if it's something that is manipulated by an interactive environment, then this would obviously fail, as would any approach that uses cron, unless there's another script running inside that environment that shares it's variables to myscript.env, or unless myscript is started from that environment.
    – Kusalananda
    Jan 8, 2017 at 12:06
  • Ok, many thanks. By the way, for portability issues, I think the best approach would be source ENV from user's one. I mean, if you need some script-related variables set, you can set them into a separated file or embed them into the script itself. But if you need "default" ENV variabkes (USER, SHELL, ...) I think it would be better sourcing them from ENV each time, because they change on each machine the script is executed. Thank again anyway!!!
    – Echoes_86
    Jan 8, 2017 at 13:44

You must log in to answer this question.

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