1

I'm trying to start a script with systemd, when an user logs in. Nevertheless until now, it is not working the way I'd like. What I did:

I placed the following service in /etc/systemd/system/loginNordVPN.service

[Unit]
After=network.service
After=network-online.target

[Service]
ExecStart=/usr/local/bin/autologinNordVPN

[Install]
WantedBy=default.target

The script /usr/local/bin/autologinNordVPN has this content:

#!/usr/bin/expect
# Script for automatic login at nordvpn
set timeout 10
set username "myusername\r"
set credentialpass "mypassword\r"

spawn nordvpn login
expect "Email / Username: "
send "$username"
expect "Password: "
send "$credentialpass"

interact

The script /usr/local/bin/autologinNordVPN works, if I start it with ./autologinNordVPN (independent, whether I start it with a normal user or as user with sudoers right).

After booting the machine and logging in as user with sudoers right, I can connect to NordVPN - the service above executed the script. Fine until here.

If I log in as "normal" user without sudoer right, I cannot connect to NordVPN - so something went wrong. Therefore I tried to add the service above to the users systemd. I copied the loginNordVPN.service to ~/.local/share/systemd/user/ and I started the service with systemctl --user enable loginNordVPN.service. (I also removed the service from the systemwide systemd before.)

After reboot and login it seems, that the script wasn't executed totally.

If I look into the journal with journalctl --user | grep "nordv" I can see, that service was startet, but for what ever reason it doesn't work:

journalctl --user | grep "nordv"
Dez 13 15:04:03 MYSYSTEM systemd[3497]: Started Login current user to nordvpn.
Dez 13 15:04:03 MYSYSTEM autologinNordVPN[3503]: spawn nordvpn login

I tried to change the user service this way, which does not work:

[Unit]
Description=Login current user to nordvpn

[Service]
StandardOutput=journal
ExecStart=/usr/local/bin/autologinNordVPN

[Install]
WantedBy=default.target

And this try neigther works:

[Unit]
Description=Login current user to nordvpn

[Service]
Type=oneshot
RemainAfterExit=true
StandardOutput=journal
ExecStart=/usr/local/bin/autologinNordVPN

[Install]
WantedBy=default.target

What do I have to do, to get my service with the script working for any user?

Thanks!

0

Your unit file probably needs a dependency on nordvpnd, the daemon that seems to actually connect to the NordVPN network and configure the VPN interface.

A line such as Requires=nordvpn.service in the [Unit] section of your unit file should do it.

What happens if you define /etc/systemd/system/loginNordVPN.service as:

[Unit]
Description=NordVPN Auto Login
After=network.service
After=network-online.target
Requires=nordvpn.service

[Service]
ExecStart=/usr/local/bin/autologinNordVPN

[Install]
WantedBy=default.target

And systemctl enable nordvpnd?

1
  • Hi @renatoc, Thanks for your answer! Unfortunately your idea did not work. The nordvpnd was enabled before systemwide. Therefore I can use the nordvpn commands in the terminal and they are working. I tried to add Requires=nordvpn.service as well as Requires=nordvpnd.service in the way you suggested. If I do that and start the service in --user context I am getting an error like this: :~$ systemctl --user enable loginNordVPN.service :~$ systemctl --user start loginNordVPN.service Failed to start loginNordVPN.service: Unit nordvpn(d).service not found. Any other idea? Thx – AMHU Dec 28 '20 at 10:10
0

I don't know why, but now this works - ~/.local/share/systemd/user/loginNordVPN.service:

[Unit]
Description=Login current user to nordvpn
After=network.service
After=network-online.target
#Requires=nordvpnd.service

[Service]
#StandardOutput=journal
ExecStart=/usr/local/bin/autologinNordVPN
#TimeoutSec=10

[Install]
WantedBy=default.target

Of course I enabled and started the service.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.