I'm trying to create a script that runs in terminal is automatically started in a raspberry pi 'screen' output. The problem here is that I need sudo privileges inside the script, and that once the process is running I don't see the request for the password.

One example of the script is the following (if I get this to work I can adapt the script for other purposes):

1) go to a folder:

cd /etc/openvpn

2) execute the service (this requires sudo privileges):

sudo openvpn ./pia_netherlands.conf

When I enter the code manually the script does run so there is no problem there, I just have a problem executing this inside a 'screen'.

I've set up the following script using some googling:

if [ -z "$STY" ]; then exec screen -dm -S pia /bin/bash "$0"; fi
cd /etc/openvpn
sudo openvpn ./pia_netherlands.conf

This script should check if there is a screen called 'pia' and if not create a screen called pia and run the script by firstly going to the folder and then run the openvpn file.

I've tried a second script that is a bit more straightforward but this also doesn’t work:

sudo bash screen -S pia
cd /etc/openvpn
openvpn ./pia_netherlands.conf

This script opens a bash screen called 'pia', goes to the folder, and executes the openvpn file.

Both methods are not working, and I really have difficulties finding out how to get it to run. Can anyone help me please?

I've also consulted the raspberry pi forums but no one replied, I guess because this is more of a linux question than a raspberry question.


If I read the man page correctly, openvpn can act as a daemon (i.e. go to the background itself) with the --daemon switch.

So if you don't need screen specifically, you might be able to go with:

sudo openvpn --daemon --config /etc/openvpn/pia_netherlands.conf

Some other alternatives:

  • start screen to run the script, enter the password, then detach the screen.
  • run the whole screen under sudo, instead of just the openvpn? i.e. sudo screen -S pia openvpn /etc/openvpn/pia_netherlands.conf

  • add the script to /etc/sudoers with the NOPASSWD: flag, so you can run that particular command without entering the password every time.

  • Wow that was it! What an easy solution to run it as daemon!!! I haven't thought of that thank you! I'm also going to check the screen option tough, screen seems like a very handy program to use, I'd like to understand how to use it. Thank you! – Johan Antonissen Jan 15 '18 at 20:31

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.