Code-server: Ship a templated system systemd unit

Created on 5 Jun 2020  ·  8Comments  ·  Source: cdr/code-server

Older versions of systemd have problems with the user unit.

See https://github.com/cdr/code-server/issues/1747#issuecomment-639150485

feature

Most helpful comment

Should also switch from exec service for this file as older systemd's don't recognize it.

For anyone who just wants to use the bundled service now, you can easily fix by running:

sudo cp /usr/lib/systemd/user/code-server.service /etc/systemd/system
sudo sed -i "s/\(Restart=always\)/\1\nUser=$USER/" /etc/systemd/system/code-server.service
sudo systemctl daemon-reload
sudo systemctl enable --now code-server

It'll make code-server a system service and add your current user into the service file and start it for you.

All 8 comments

Should also switch from exec service for this file as older systemd's don't recognize it.

For anyone who just wants to use the bundled service now, you can easily fix by running:

sudo cp /usr/lib/systemd/user/code-server.service /etc/systemd/system
sudo sed -i "s/\(Restart=always\)/\1\nUser=$USER/" /etc/systemd/system/code-server.service
sudo systemctl daemon-reload
sudo systemctl enable --now code-server

It'll make code-server a system service and add your current user into the service file and start it for you.

To be clear, the above will fix errors similar to "Failed to get D-Bus connection" which is what happens when using a user unit on an old version of systemd.

@nhooyr Thanks for your solution. After run above cmd, I successed created symlink in default.target, but I can't start code-server, until I change the line 'User=$USER' in code-server.service to 'User=admin' with a real username , and its failed status below:

$systemctl status code-server

● code-server.service - code-server
   Loaded: loaded (/etc/systemd/system/code-server.service; enabled; vendor preset: disabled)
   Active: failed (Result: start-limit) since Tue 2020-06-09 21:25:00 CST; 39min ago
  Process: 8866 ExecStart=/usr/bin/code-server (code=exited, status=217/USER)
 Main PID: 8866 (code=exited, status=217/USER)

Jun 09 21:25:00 instance-1 systemd[1]: code-server.service: main process exited, code=exited, status=217/USER
Jun 09 21:25:00 instance-1 systemd[1]: Unit code-server.service entered failed state.
Jun 09 21:25:00 instance-1 systemd[1]: code-server.service failed.
Jun 09 21:25:00 instance-1 systemd[1]: code-server.service holdoff time over, scheduling restart.
Jun 09 21:25:00 instance-1 systemd[1]: Stopped code-server.
Jun 09 21:25:00 instance-1 systemd[1]: start request repeated too quickly for code-server.service
Jun 09 21:25:00 instance-1 systemd[1]: Failed to start code-server.
Jun 09 21:25:00 instance-1 systemd[1]: Unit code-server.service entered failed state.
Jun 09 21:25:00 instance-1 systemd[1]: code-server.service failed.

So I want to know the cmd

sudo sed -i 's/\(Restart=always\)/\1\nUser=$USER/' /etc/systemd/system/code-server.service

should replace single quote with double quote to add the current real username.

sudo sed -i "s/\(Restart=always\)/\1\nUser=$USER/" /etc/systemd/system/code-server.service

@yaerda Great catch!

@nhooyr Thank you so much! This works great.

@yaerda And thank you for the input.

You're answer got my instance up and running.

Also see the linger issues #1673 #1882 #1861

So the reason I couldn't reproduce before is I use ssh connection sharing which kept my ssh connection open even after I exited. So definitely need this to fix that.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

tecosaur picture tecosaur  ·  3Comments

justmao945 picture justmao945  ·  3Comments

avelino picture avelino  ·  3Comments

KSXGitHub picture KSXGitHub  ·  3Comments

pchecinski picture pchecinski  ·  3Comments