How to use environment variable declared in /etc/profie into script running through monit?

StackOverflow https://stackoverflow.com/questions/21428879

  •  04-10-2022
  •  | 
  •  

Question

Environment variable declared in /etc/profile:

export MYNAME=rhel

Content of script which is running from monit is [/tmp/printmyname.sh]:

echo "My Name is: "$MYNAME >> /var/log/env_variablefile.out

Content of monit:

check file PrintVariable with path /var/log/something.out
   start program = "/bin/sh /tmp/printmyname.sh"
   if timestamp > 1 minutes then start

I want to print environment variable declared in /etc/profile to /var/log/env_variablefile.out when /var/log/something.out file is not updated since one minute.

So my problem is when i directly run /tmp/printmyname.sh it append My Name is: rhel into /var/log/env_variablefile.out but when it is running from monit it only prints My Name is:.

So I want to know the reason of such behavior and a way to solve problem.

Note: monit is running every 10Seconds and above code is just example of my actual code.

Was it helpful?

Solution

/etc/profile is only executed for interactive shells.

One way to fix this is to add this to the beginning of /tmp/printmyname.sh:

. /etc/profile

Note that this may cause problems because /etc/profile tries to set up an interactive environment, so a lot of setup scripts will be called that you may not want.

A better solution is probably to put this variable in a new global script and source this new script from both /etc/profile and /tmp/printmyname.sh

Related articles:

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top