4

Somehow new to Linux, have used it in the past, though not as extensively as now.

What I am trying to achieve here is to start a openvpn service as soon as the computer boots. I have partially achieved this. I have several .ovpn files that are configured to connect to different countries. I have first created a script that asks which country that I want to connect to, then makes the connection.

What now I want to do is make it automatic, so that everytime that I boot into my user, I want the service to prompt me for input and then run it. The issue here is that I can not manage to force the service to prompt me for input.

Bash code:

#!/bin/bash
echo Which server do you want to connect 

read -p 'Server(pt, se, nl, es, no, is, ch, de, ca, au, at): ' server

echo "You selected " $server

sudo openvpn /opt/ProtonVPN/$server.protonvpn.com.udp.ovpn

This code works great by itself.

To create a service I followed the following article: How do I create a service for a shell script so I can start and stop it like a daemon?

So my service file looks like this:

[Unit]
Description=Connect to ProtonVPN

[Service]
Type=simple
ExecStart=/home/{user}/bin/proton-run.sh

I then restarted the daemon with: systemctl daemon-reload

I tried to run the service with: sudo systemctl start proton-run.service, but I never get prompted for a input, so the script crashes, screenshot attached: enter image description here

Any help ? I have read about systemd-ask-password but I don't think that it is what I need.

  • Sorry, I can't give you a solution to your problem but I can at least tell you why what you have so far isn't working. Your script is being executed in the background by the systemd service, not by you in your shell, so you're never going to see the input prompt. – JShorthouse Aug 05 '19 at 15:59
  • 1
    Actually I've just thought up something that could work. This is somewhat hacky and I'm certain there's much better ways to do this that someone with more experience that myself could come up with, but here goes. Add an if statement to your bashrc that checks if the VPN service is running. If it isn't, prompt for the region and then write this value out to a file in <some_location>, then start the VPN service. Change your VPN service script so that instead of trying to prompt for the region, it reads the region from the file in <some_location>, and then starts up. – JShorthouse Aug 05 '19 at 16:02
  • @JShorthouse thanks for the reply. I am aware of that, that is why I am somehow trying to get a way to get the prompt. Have read about and tried with StandardInput=tty and tried with the systemd-ask-password, but nothing seems to work. Thought maybe someone else had already tried this. – calexandru Aug 05 '19 at 16:04

1 Answers1

1

What you want to do is not possible.

When you type `sudo systemctl start proton-run.service' your script is launched into its own shell using a separate process. The prompt will never appear in your shell.

On possible solution would be to give arguments to systemctl that would be passed to the underlying script.

Here is some ideas on how to do that : https://superuser.com/questions/728951/systemd-giving-my-service-multiple-arguments

Lucas Declercq
  • 246
  • 1
  • 3