dkron icon indicating copy to clipboard operation
dkron copied to clipboard

How to source an environment file?

Open ssenaria opened this issue 10 years ago • 2 comments

I have an existing cronjob as follows (keeping this simple for example purposes):

curl -n -X POST 127.0.0.1:8081/v1/jobs \
  -H "Content-Type: application/json" \
 \
  -d '{
  "name": "runmeasroot",
  "shell": true,
  "schedule": "0 * * * * *",
  "command": ". /root/env && /root/run-it.sh",
  "owner": "Me",
  "owner_email": "[email protected]",
  "run_as_user": "root",
  "tags": {
          "cluster_id": "odd:1"
  },
  "disabled": false
}'

run-it.sh

#!/bin/bash
mydate=$(date +%Y-%m-%d--%H:%M:%S)
echo $(whoami) ${mydate} $HOME >> $HOME/run-it.log

I'm not able to run it via Dkron correctly. While the job runs, it doesn't honor any environment variables. $HOME isn't parsed correctly and the log file is stored under / and not /root. The cronjob syntax works if I were to use the OS' cron.

ssenaria avatar Oct 25 '16 18:10 ssenaria

I have some more information. So I ran a simple cronjob

/usr/bin/env > /tmp/env.output

When I use the OS' cron I get a bunch of variables, $HOME being one of them. When I use the same command within Dkron, I get a lot less. How can I simulate a working environment w/ all the appropriate environment variables I'm used to seeing within my OS cronjobs?

PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin
PWD=/opt/dkron
LANG=en_US.UTF-8
SHLVL=1
_=/usr/bin/env

ssenaria avatar Oct 25 '16 19:10 ssenaria

@ssenaria yes, dkron environment comes from the method that dkron is started, upstart or any other process manager, so by default it's usually quite empty.

If you need a full user env I recommend to use su -l user -c "your commands"

You could source any env file before calling the actual command.

vcastellm avatar Oct 25 '16 20:10 vcastellm