2

I set environment variables in some file /etc/profile.d/mercure.sh like this :

export MERCURE_PUBLISHER_JWT_KEY=$(cat /home/stephane/mercure/publisher.key.pub)
export MERCURE_PUBLISHER_JWT_ALG=RS256
export MERCURE_SUBSCRIBER_JWT_KEY=$(cat /home/stephane/mercure/subscriber.key.pub)
export MERCURE_PUBLISHER_JWT_ALG=RS256
export SERVER_NAME=localhost:3000

I need them for running following command (for mercure hub):

MERCURE_PUBLISHER_JWT_KEY=$MERCURE_PUBLISHER_JWT_KEY MERCURE_SUBSCRIBER_JWT_KEY=$MERCURE_SUBSCRIBER_JWT_KEY SERVER_NAME=$SERVER_NAME /usr/bin/mercure run

I want to create a mercure.service file for running mercure as service as explained here

[Unit]
Description=Mercure.Rocks service
After=network.target
StartLimitBurst=5
StartLimitIntervalSec=33

[Service]
Type=simple
WorkingDirectory=/tmp
EnvironmentFile=/etc/profile.d/mercure.sh
ExecStart=MERCURE_PUBLISHER_JWT_KEY=$MERCURE_PUBLISHER_JWT_KEY MERCURE_SUBSCRIBER_JWT_KEY=$MERCURE_SUBSCRIBER_JWT_KEY SERVER_NAME=$SERVER_NAME /usr/bin/mercure run --config /home/stephane/mercure/Caddyfile
StandardOutput=file:/var/log/nginx/mercure.log
StandardError=file:/var/log/nginx/mercure.log
Restart=always
RestartSec=5

[Install]
WantedBy=multi-user.targe

At start, I got these errors :

$ sudo systemctl status mercure
● mercure.service - Mercure.Rocks service
     Loaded: loaded (/etc/systemd/system/mercure.service; enabled; vendor preset: enabled)
     Active: activating (auto-restart) (Result: exit-code) since Wed 2024-01-03 15:26:49 CET; 1s ago
    Process: 33752 ExecStart=MERCURE_PUBLISHER_JWT_KEY=$MERCURE_PUBLISHER_JWT_KEY MERCURE_SUBSCRIBER_JWT_KEY=$MERCURE_SUBSCRIBER_JWT_KEY SERVER_NAME=$SERVER_NAME /usr/bin/mercure run --config /home/stephane/mer>
   Main PID: 33752 (code=exited, status=203/EXEC)
        CPU: 824us

systemd[1]: Stopped Mercure.Rocks service.
systemd[1]: mercure.service: Ignoring invalid environment assignment 'export MERCURE_PUBLISHER_JWT_KEY=$(cat /home/stephane/mercure/publisher.key.pub)': /etc/profile.d/mercure.sh
systemd[1]: mercure.service: Ignoring invalid environment assignment 'export MERCURE_PUBLISHER_JWT_ALG=RS256': /etc/profile.d/mercure.sh
systemd[1]: mercure.service: Ignoring invalid environment assignment 'export MERCURE_SUBSCRIBER_JWT_KEY=$(cat /home/stephane/mercure/subscriber.key.pub)': /etc/profile.d/mercure.sh
systemd[1]: mercure.service: Ignoring invalid environment assignment 'export SERVER_NAME=localhost:3000 #mercure.astro-otter.space': /etc/profile.d/mercure.sh
systemd[1]: Started Mercure.Rocks service.
systemd[1]: mercure.service: Main process exited, code=exited, status=203/EXEC
systemd[1]: mercure.service: Failed with result 'exit-code'.

I don't really understand how I can use environment variables in my ExecStart line, can somebody explain or show me an example?

3
  • try using EnvironmentFile with static values (you'll have to rebuild file if key change of course), if dynamic are mandatory, use a wrapper, that will 1) compute keys 2) run mercure run --config ...
    – Archemar
    Jan 3 at 19:17
  • 1
    I think this might be what you want: unix.stackexchange.com/a/675539/272848
    – Stewart
    Jan 5 at 14:39
  • @Stewart thank you for link, i wrote an answer lower how i fix it :) Jan 5 at 15:40

2 Answers 2

4

Try a simpler environment file, without export. It is not required.

Try also ExecStart /bin/bash -c "...." if you want to use some variables like $MERCURE_PUBLISHER_JWT_KEY

2
  • Ho, i just need to add variables in /etc/environment ? Jan 3 at 15:40
  • 1
    @Astro-Otter: /etc/environment, just like the file given in the EnvironmentFile= directive, is not a shell script. It is a simple list of assignments of the form variable=value. No export, no process substitution, no nothing. Just simple variable=value statements. If you want to run a shell script, run it in ExecStart=.
    – AlexP
    Jan 3 at 23:11
0

Helped with answers of @frédéric-loyer and @AlexP, i set environment variables in /etc/environment and edit my mercure.service file :

[Unit]
Description=Mercure.Rocks service
After=network.target
StartLimitBurst=5
StartLimitIntervalSec=33

[Service]
Type=simple
WorkingDirectory=/tmp
EnvironmentFile=-/etc/environment
ExecStart=/usr/bin/bash -c "MERCURE_PUBLISHER_JWT_KEY=$MERCURE_PUBLISHER_JWT_KEY MERCURE_SUBSCRIBER_JWT_KEY=$MERCURE_SUBSCRIBER_JWT_KEY SERVER_NAME=$SERVER_NAME /usr/bin/mercure run --config /var/www/mercure/Cad
dyfile"
StandardOutput=file:/var/log/nginx/mercure.log
StandardError=file:/var/log/nginx/mercure.log
Restart=always
RestartSec=5

[Install]
WantedBy=multi-user.target

It seems to be OK :

$ sudo systemctl status mercure
● mercure.service - Mercure.Rocks service
     Loaded: loaded (/etc/systemd/system/mercure.service; enabled; vendor preset: enabled)
     Active: active (running) since Thu 2024-01-04 10:15:04 CET; 3min 52s ago
   Main PID: 57957 (mercure)
      Tasks: 6 (limit: 2163)
     Memory: 8.8M
        CPU: 80ms
     CGroup: /system.slice/mercure.service
             └─57957 /usr/bin/mercure run --config /var/www/mercure/Caddyfile

Jan 04 10:15:04 vps773218 systemd[1]: Started Mercure.Rocks service.

Thank you.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .