Systemd service configuration using environment variable - linux

How do i set the MemoryLimit using environment variable
I have tried set MemoryLimit field by using the variable MY_LIMIT like this,
The Service configuration:
[Unit]
Description=Blabla
[Service]
Environment="MY_LIMIT=1024"
MemoryLimit=$MY_LIMIT
ExecStart=script.sh
But this doesnt seems to work
As we see that
sudo systemctl show myservice
Show that the MemoryLimit is a assigned with this value instead
MemoryLimit=18446744073709551615

systemd has an Environment directive which sets environment variables for executed processes. [source][1]
So your MY_LIMIT won't get interpreted when set in MemoryLimit.
Also MemoryLimit is deprecated, use MemoryMax= instead. [source][2]
So what you should set is: MemoryMax=1024M

Related

supervisor child process not reading environment variable

In my go program, I'm using os.Getenv("DB_PASSWORD") to grab the environment variable DB_PASSWORD.
While in development, I use godotenv and GNU Make to build and start the program. In production, I'm using supervisor to manage the process (basically for its daemonizing and auto-restart capabilities).
But with supervisor, my go program is unable to access any environment variables (set using export key='value').
I've checked out supervisor with environment variables section and it's still not working.
I've done supervisorctl reread and supervisorctl update, still no luck.
How can I get supervisor child process (my program) to read environment variables
[program:program_name]
command=/var/www/program_name/bin/program_name -environment production -port 4041 --DB_PASSWORD=%(ENV_DB_PASSWORD)s
directory=/var/www/program_name
environment=DB_PASSWORD=value-here
autorestart=true
autostart=true
stdout_logfile=/var/www/program_name/logs/supervisord.log
stderr_logfile=/var/www/program_name/logs/supervisord_err.log
stdout_logfile_maxbytes=5MB
stderr_logfile_maxbytes=5MB
logfile_backups=3
loglevel=info

How can I get the environment variable from docker file

How can I get the environment variable from docker file for example I am adding a
ENV URL_PATH="google.com"
in my dockerfile, so can I get the this URL_PATH in my Jmeter.jmx file with the help of User Defined Variable.
On window its working fine with proper {__env(URL_PATH)}
but on docker its not working. How can I solve this problem?
You can use the -e option to pass environment variables into the container when running it.
docker run -e URL_PATH=google.com ...
Docs: https://docs.docker.com/engine/reference/run/#env-environment-variables
As far as I can see __env() is a Custom JMeter Function therefore it is not available in vanilla JMeter so the options are in:
Amend your Dockerfile to include downloading of http://repo1.maven.org/maven2/kg/apc/jmeter-plugins-functions/2.0/jmeter-plugins-functions-2.0.jar to "lib/ext". This way you will be able to use __env() function in Docker environment normally. See Make Use of Docker with JMeter - Learn How for example Docker configuration assuming using JMeter with Plugins.
Switch to __groovy() function. Replace all the occurrences of {__env(URL_PATH)} with the following expression:
${__groovy(System.getenv('URL_PATH'),)}

JAVA_HOME for Logstash

I am trying to setup ELK stack for my Web Services Log Monitoring.
So I have setup all the parts for ELK Stack.
I am facing one issue in Log-stash. When I am running Log-stash, I am facing error, could not load Java binary
Although the simple fix it set the JAVA_HOME in environment variable.
But I don't want to set an environment variable, but what I want to set JAVA_HOME just for Log-stash. I have tried adding in startup.options, but to enable I must run system-install. When I am running system-install, I am facing the same error again.
I have added
export JAVA_HOME=/opt/jre8
then system-install file runs, but still on starting log-stash, I am getting the same error. What should I do to resolve this error?
You can config in startup.options (logstash5.4 version):
Ex:
JAVA_HOME=/.../jdk1.8.0_121
JAVACMD=/.../jdk1.8.0_121/bin/java
Then use root role to start: system-install.
(You can use update-java-alternatives --list to list installed java versions with paths)
You can add this configuration to the file- /etc/sysconfig/logstash, this file is read during startup by logstash.
This is what you should add:
export JAVA_HOME=/opt/jre8

Linux environment variable reset after restart

So I have this really nasty problem.
I once set up a tomcat Server on my raspberry pi. The version of it was 8.0.24. I've created a bash script which sets the variable $CATALINA_HOME=/home/pi/apache-tomcat-8.0.24 on each start.
Meanwhile the directory is /home/pi/tomcat - i removed the useless information.
I've changed the export in /etc/init.d/tomcat also, but it didnt help.
After every restart, CATALINA_HOME is set back to /home/pi/apache-tomcat-8.0.24 again.
Is there a way to see, which script sets the environmental variable?
Somewhere I told linux to change the path at startup to /home/pi/apache.. , but i cant find where.
You can add a line in a few of the startup scripts to print the value of $CATALINA_HOME. Try adding:
echo "In $0, \$CATALINA_HOME is $CATALINA_HOME"
to your .bashrc before and after the call to /etc/bashrc
There's also a script called setenv.bash inside Tomcat that sets these types of variables. Take a look in there too.

Environment variable with period in using supervisor started node process

I have a node process that uses an environment variable in the form SECRET_KEY=1234.5678.910112.
This works fine if set using export in my bash_profile and the process is run directly in the shell.
But when running it using supervisor the script only picks up the part before the first period. This is the case either when reading env vars set in bash_profile or set using environment= in the conf file.
Turns out all I needed to do was to add single quotes around my variable. I did do this before but didn't run supervisorctl reread to get the new config.

Resources