Expressvpn auto shutdown command - Is this possible?

About writing shell scripts and making the most of your shell
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Locked
kanichiro
Level 3
Level 3
Posts: 179
Joined: Thu Nov 07, 2013 11:23 am
Location: Frostbite Falls, MN

Expressvpn auto shutdown command - Is this possible?

Post by kanichiro »

Not sure if this can be done. So far I've been unable to make something up that works.

At the moment I have an Expressvpn auto logon command. What I would like to add to this is an Expressvpn auto shutdown command that executes either by a keyboard command that disconnects from Expressvpn then shuts down the computer, or executes when I click the Linux Mint Shutdown button.

This is my Expressvpn auto logon command that I managed to cobble together. This commands executes automatically at startup. It may not be the cleanest, but it works!

Code: Select all

#!/bin/sh
expressvpn connect USCH
@reboot ( sleep 15 ; sh /home/mederia/expressvpnstart.sh
Right now I have to manually type "expressvpn disconnect" into the Terminal then press Enter before each time I shutdown my lappy. An auto disconnect script would be a lot easier!

Update: I have contacted Expressvpn about this but they showed no interest in it at all.

Note: I am in no way a programmer!!!!

Thanks for any and all help or suggestions!

kanichiro
Last edited by LockBot on Sun May 28, 2023 10:00 pm, edited 1 time in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
User avatar
Termy
Level 12
Level 12
Posts: 4248
Joined: Mon Sep 04, 2017 8:49 pm
Location: UK
Contact:

Re: Expressvpn auto shutdown command - Is this possible?

Post by Termy »

The following will disconnect the VPN then power the machine off if the previous command succeeds, using the logical AND operator (&&). You can assign it directly to a keyboard shortcut, without needing to create a script.

Actually, you can put this in a script, then add it to — erm, I think it's '/lib/systemd/system/poweroff.target.wants', although that might not be the best place for it. Something like that should tell SystemD to execute the script (containing your disconnect command) prior to shutting down the system. Look online (or wait for someone) for how to run a command with SystemD before shutting down the system, to use the correct method.

Code: Select all

expressvpn disconnect && systemctl poweroff
Surely though, when you power off the machine, the VPN has no choice but to disconnect?

I don't understand how the below one-liner does anything, because it's certainly not valid shell syntax, let alone DASH. A command cannot start with '@', and there's an unpaired subshell parenthesis.

Code: Select all

@reboot ( sleep 15 ; sh /home/mederia/expressvpnstart.sh
I'm also Terminalforlife on GitHub.
kanichiro
Level 3
Level 3
Posts: 179
Joined: Thu Nov 07, 2013 11:23 am
Location: Frostbite Falls, MN

Re: Expressvpn auto shutdown command - Is this possible?

Post by kanichiro »

Termy wrote: Mon Nov 28, 2022 12:12 pm Surely though, when you power off the machine, the VPN has no choice but to disconnect?

I don't understand how the below one-liner does anything, because it's certainly not valid shell syntax, let alone DASH. A command cannot start with '@', and there's an unpaired subshell parenthesis.

Code: Select all

@reboot ( sleep 15 ; sh /home/mederia/expressvpnstart.sh
Hi Termy,
Thanks for the help!

Here are my answers to your statements:

* Surely though, when you power off the machine, the VPN has no choice but to disconnect?

Somewhere in my email correspondence with Expressvpn they requested that I enter the "Expressvpn disconnect" code into the Terminal and disconnect softly instead of a hard shutdown by just shutting down my lappy. Why? They know, I don't, but I've tried to follow their suggestion since then.

* I don't understand how the below one-liner does anything, because it's certainly not valid shell syntax, let alone DASH. A command cannot start with '@', and there's an unpaired subshell parenthesis.

Code: Select all

@reboot ( sleep 15 ; sh /home/mederia/expressvpnstart.sh

I have never claimed to be a programmer, I'm not by any stretch of the imagination. I cobbled that script together from several scripts I found, created an expressvpnstart.sh file and attached that to the Startup Applications app. It works, although I cannot tell you why. If there is better code for this I would appreciate knowing it! Without that script I have to launch Expressvppn manually each time after my lappy boots up
User avatar
Termy
Level 12
Level 12
Posts: 4248
Joined: Mon Sep 04, 2017 8:49 pm
Location: UK
Contact:

Re: Expressvpn auto shutdown command - Is this possible?

Post by Termy »

Ah, I'm guessing you're logged into a service when you use the VPN, so by not manually logging out, the service will still think you're logged in. I would have expected the service to be intelligent enough to simply log the user out after some time of inactivity, but that could be problematic if the user has just gone AFK. It seems a bit strange, but I guess I can see how it's that way. Sorry for the unrelated questions in my previous post — I was just curious and tired. :roll:

Did you try the command I posted? It should disconnect the VPN, then, if successful, shut the machine down. You can usually create a keyboard shortcut for this by using the 'Control Panel'-like settings in Linux Mint.
I'm also Terminalforlife on GitHub.
kanichiro
Level 3
Level 3
Posts: 179
Joined: Thu Nov 07, 2013 11:23 am
Location: Frostbite Falls, MN

Re: Expressvpn auto shutdown command - Is this possible?

Post by kanichiro »

Termy wrote: Tue Nov 29, 2022 7:19 am Ah, I'm guessing you're logged into a service when you use the VPN, so by not manually logging out, the service will still think you're logged in. I would have expected the service to be intelligent enough to simply log the user out after some time of inactivity, but that could be problematic if the user has just gone AFK. It seems a bit strange, but I guess I can see how it's that way. Sorry for the unrelated questions in my previous post — I was just curious and tired. :roll:

Did you try the command I posted? It should disconnect the VPN, then, if successful, shut the machine down. You can usually create a keyboard shortcut for this by using the 'Control Panel'-like settings in Linux Mint.
Termy,
Yes I did create a keyboard command with the script you supplied. It did disconnect me from Expressvpn, however, it did not shutdown my laptop afterwards.

As I sometimes change VPN servers multiple times during a session, I've added a keyboard shortcut to disconnect me. It does save me an additional step, and I thank you for that!

Update: I added a keyboard command for shutting down the laptop using only the systemctl poweroff part of your string. It Worked!

It's just the two ampersands (&&) between the strings that are stopping the entire code string from working. Any suggestions on what to replace the ampersands with?

kanichiro
User avatar
Termy
Level 12
Level 12
Posts: 4248
Joined: Mon Sep 04, 2017 8:49 pm
Location: UK
Contact:

Re: Expressvpn auto shutdown command - Is this possible?

Post by Termy »

kanichiro wrote: Tue Nov 29, 2022 9:50 am [...]
The logical AND operator (&&) will ensure the right-most command executes if the previous command is successful, so if the computer did not shut down, yet systemctl(1) successfully powers down the machine on its own, we can certainly conclude that the VPN is not correctly disconnecting or the program is otherwise not exiting successfully. So, let's find out what's going on.

Use the below one-liner with the keyboard shortcut instead.

Code: Select all

expressvpn disconnect 2>&1 | tee -a /home/mederia/VPN_Disconnect.log
Please share the contents of the '/home/mederia/VPN_Disconnect.log' log file, after entering the shortcut. Note that this may disconnect your VPN, obviously. :roll:

It's possible that the GUI letting you assign these keyboard shortcuts might not allow commands like the above — specifically, with the pipe — in which case, you may need to add it to a script, then have the shortcut execute the script (IE: sh /path/to/script.sh). You hopefully won't need to do that, though.
I'm also Terminalforlife on GitHub.
User avatar
JTJersey
Level 3
Level 3
Posts: 162
Joined: Sun Jul 07, 2019 8:32 am
Location: South Jersey

Re: Expressvpn auto shutdown command - Is this possible?

Post by JTJersey »

You could just create a simple text file like this one I use:

expressvpn disconnect
exit

Then create a launcher as an Application in a Terminal and direct the command over to your little script. Now you can just click/tap the disconnect icon on your desktop without needing to type anything.
I have a second icon to connect when I want.
User avatar
Termy
Level 12
Level 12
Posts: 4248
Joined: Mon Sep 04, 2017 8:49 pm
Location: UK
Contact:

Re: Expressvpn auto shutdown command - Is this possible?

Post by Termy »

JTJersey wrote: Tue Nov 29, 2022 6:20 pm [...]
BTW, the exit at the end is redundant, as the script already exits when there are no more commands. ;)
I'm also Terminalforlife on GitHub.
kanichiro
Level 3
Level 3
Posts: 179
Joined: Thu Nov 07, 2013 11:23 am
Location: Frostbite Falls, MN

Re: Expressvpn auto shutdown command - Is this possible?

Post by kanichiro »

Termy wrote: Tue Nov 29, 2022 5:07 pm
kanichiro wrote: Tue Nov 29, 2022 9:50 am [...]
The logical AND operator (&&) will ensure the right-most command executes if the previous command is successful, so if the computer did not shut down, yet systemctl(1) successfully powers down the machine on its own, we can certainly conclude that the VPN is not correctly disconnecting or the program is otherwise not exiting successfully. So, let's find out what's going on.

Use the below one-liner with the keyboard shortcut instead.

Code: Select all

expressvpn disconnect 2>&1 | tee -a /home/mederia/VPN_Disconnect.log
Please share the contents of the '/home/mederia/VPN_Disconnect.log' log file, after entering the shortcut. Note that this may disconnect your VPN, obviously. :roll:

It's possible that the GUI letting you assign these keyboard shortcuts might not allow commands like the above — specifically, with the pipe — in which case, you may need to add it to a script, then have the shortcut execute the script (IE: sh /path/to/script.sh). You hopefully won't need to do that, though.
Termy,
First of all I want to thank you for all your help. While none of the combined strings have worked, I was able to create two keyboard shortcuts to disconnect from Expressvon and to shutdown my laptop. Pressing these two keyboard combinations is much, much faster and easier than typing the Expressvpn sting in manually and then clicking on the various the Panel buttons to shutdown the lappy.

Now to answer your latest post:

* Just an off the wall guess on my part, but I wonder if the two ampersands don't work because the Expressvpn app is a 3rd party app and is not a part of Linux itself so Linux does not get notified that the Expressvpn server disconnected?

* As an experiment, I looked up combining Linux commands and used each variable I found. None of them worked.

* I tried your latest string of expressvpn disconnect 2>&1 | tee -a /home/mederia/VPN_Disconnect.log and it did nothing. It neither disconnected Expressvpn nor did it create a folder in my Home directory.

* I created a script as you suggested, it disconnected Expressvpn but did nothing else.

Thanks again for everything!

kanichiro
User avatar
JTJersey
Level 3
Level 3
Posts: 162
Joined: Sun Jul 07, 2019 8:32 am
Location: South Jersey

Re: Expressvpn auto shutdown command - Is this possible?

Post by JTJersey »

Termy wrote: Wed Nov 30, 2022 6:20 am BTW, the exit at the end is redundant, as the script already exits when there are no more commands. ;)
Cool. Never gave it a thought before. Works just fine without the "exit" included.
kanichiro
Level 3
Level 3
Posts: 179
Joined: Thu Nov 07, 2013 11:23 am
Location: Frostbite Falls, MN

Re: Expressvpn auto shutdown command - Is this possible?

Post by kanichiro »

As an experiment, I tried reversing Termy's original script to see if that worked. It didn't! I believe that combining a Linux script with a non-Linux breaks it for some reason.

Termy's original script: expressvpn disconnect && systemctl poweroff (this string disconnected Expressvpn, but did not power off my laptop.)

Termy's script reversed: systemctl poweroff && expressvpn disconnect (This string neither disconnected Expressvpn, nor did it power off the laptop)

Anyway, I am grateful to Termy for suggesting keyboard commands. I added one to disconnect from Expressvpn and a number of them to connect to various Expressvpn servers without my having to type in the string into the Terminal.

I am not sure why Termy's original string breaks, but it does. The code is correct and it would most likely work if both parts were Linux commands.

Should I list this issue as SOLVED or Cannot be performed?

kanichiro
User avatar
Termy
Level 12
Level 12
Posts: 4248
Joined: Mon Sep 04, 2017 8:49 pm
Location: UK
Contact:

Re: Expressvpn auto shutdown command - Is this possible?

Post by Termy »

kanichiro wrote: Fri Dec 02, 2022 7:16 am [...]
Then it'll be a limitation of the GUI. The GUI probably accepts one single command, and not things like logic or a pipeline. It's common, but inconvenient. This is why I suggested using a script (file), and having the shortcut point to that instead. It should then work correctly, and then, if it solves the problem, usually people prepend '[Solved] ' to the title. Let us know how you get on.
I'm also Terminalforlife on GitHub.
kanichiro
Level 3
Level 3
Posts: 179
Joined: Thu Nov 07, 2013 11:23 am
Location: Frostbite Falls, MN

Re: Expressvpn auto shutdown command - Is this possible?

Post by kanichiro »

Termy wrote: Fri Dec 02, 2022 3:51 pm
kanichiro wrote: Fri Dec 02, 2022 7:16 am [...]
Then it'll be a limitation of the GUI. The GUI probably accepts one single command, and not things like logic or a pipeline. It's common, but inconvenient. This is why I suggested using a script (file), and having the shortcut point to that instead. It should then work correctly, and then, if it solves the problem, usually people prepend '[Solved] ' to the title. Let us know how you get on.
Termy,
I tried it and it did not work besides disconnecting Expressvpn.
Termyscript2.png
Path to Termy shortcut script.png
Path to Termy shortcut script.png (7.47 KiB) Viewed 799 times
VPN_Disconnect.log - created.png
VPN_Disconnect.log - created.png (3.47 KiB) Viewed 799 times
The log file I created was empty after running the command
kanichiro
Level 3
Level 3
Posts: 179
Joined: Thu Nov 07, 2013 11:23 am
Location: Frostbite Falls, MN

Re: Expressvpn auto shutdown command - Is this possible?

Post by kanichiro »

Termy,
Using the keyboard shortcuts you suggested, I can now logoff Expressvpn and have my lappy shutdown in a fraction of the time it took me to do it manually. That's fast enough and easy enough for me.

Thanks for all your help, I really appreciate it!

kanichiro
User avatar
Termy
Level 12
Level 12
Posts: 4248
Joined: Mon Sep 04, 2017 8:49 pm
Location: UK
Contact:

Re: Expressvpn auto shutdown command - Is this possible?

Post by Termy »

kanichiro wrote: Sat Dec 03, 2022 7:07 am [...]
Okidoke. I'm glad a solution could be found. Oh, and you're more than welcome.
I'm also Terminalforlife on GitHub.
Locked

Return to “Scripts & Bash”