0
votes

systemctl does not allow you to use environment variables. I have a script that installs a program, downloads its system [email protected] file (note I cannot use a user service, it needs to be a system service which requires a username to be passed). It needs to run after boot, regardless which user logs in).

In order to do that, the script needs to enable the service like this:

sudo systemctl enable [email protected]

Normally, people would simply enter this command in terminal manually and use the name they want. But I am executing this script on multiple computers and I want the script to take care of it.

"username" should be the user that is logged in when this installation script runs. It is not root.

Normally, I would use the $SUDO_USER env variable as this simply returns the currently logged in user. Unfortunately you cannot use an env variable with systemctl. I thought this would do the trick:

/bin/bash -c 'sudo systemctl start syncthing@$SUDO_USER.service'

But unfortunately that also does not work.

How can I solve this? Ubuntu 20.04.1

1

1 Answers

0
votes

For now I solve it like this, I first do systemctl enable with a pseudo, then I rename the file that it creates for the real currently logged in user. Note I have to use $LOGNAME instead of $SUDO_USER. Although $SUDO_USER is a default env variable, it returns empty. $LOGNAME does work.

sudo systemctl enable [email protected]

sudo mv /etc/systemd/system/multi-user.target.wants/[email protected] /etc/systemd/system/multi-user.target.wants/syncthing@$LOGNAME.service