Using a User's .Bashrc in a Systemd Service

How to load environment variables for the process of a systemd service?

Environment can be set in systemd service file as below under Exec options

Environment=LD_LIBRARY_PATH=/usr/lib

Below is the official documentation of systemd Environment/EnvironmentFile usage

Environment=

Sets environment variables for executed processes. Takes a space-separated list of variable assignments. This option may be specified more than once, in which case all listed variables will be set. If the same variable is set twice, the later setting will override the earlier setting. If the empty string is assigned to this option, the list of environment variables is reset, all prior assignments have no effect. Variable expansion is not performed inside the strings, however, specifier expansion is possible. The $ character has no special meaning. If you need to assign a value containing spaces or the equals sign to a variable, use double quotes (") for the assignment.

Example:

Environment="VAR1=word1 word2" VAR2=word3 "VAR3=$word 5 6"
gives three variables "VAR1", "VAR2", "VAR3" with the values "word1 word2", "word3", "$word 5 6".

See environ(7) for details about environment variables.

EnvironmentFile=

Similar to Environment= but reads the environment variables from a text file. The text file should contain new-line-separated variable assignments. Empty lines, lines without an "=" separator, or lines starting with ; or # will be ignored, which may be used for commenting. A line ending with a backslash will be concatenated with the following one, allowing multiline variable definitions. The parser strips leading and trailing whitespace from the values of assignments, unless you use double quotes (").

The argument passed should be an absolute filename or wildcard expression, optionally prefixed with "-", which indicates that if the file does not exist, it will not be read and no error or warning message is logged. This option may be specified more than once in which case all specified files are read. If the empty string is assigned to this option, the list of file to read is reset, all prior assignments have no effect.

The files listed with this directive will be read shortly before the process is executed (more specifically, after all processes from a previous unit state terminated. This means you can generate these files in one unit state, and read it with this option in the next).

Settings from these files override settings made with Environment=. If the same variable is set twice from these files, the files will be read in the order they are specified and the later setting will override the earlier setting.

Read more here

How to login to bash with current user, and the user's .bashrc file, in jupyterlab?

You could try creating a file

$HOME/.bash_login

and source .bashrc from there

source $HOME/.bashrc

and see if it does what you wanted.

systemd doesn't run an application from bash script

Systemd will need to know how to run the script. Therefore either add:

#!/bin/bash

to the top line of the startup.sh script or change the ExecStart line in the systemd service file to:

ExecStart=/bin/bash -c /opt/somedir/startup.sh

Also, to ensure that the processes spawned remain persistent after being spawned, change:

Type=forking


Related Topics



Leave a reply



Submit