Autostart Terminal Command on Every Boot

All Gurus once were Newbies
Forum rules
There are no such things as "stupid" questions. However if you think your question is a bit stupid, then this is the right place for you to post it. Please stick to easy to-the-point questions that you feel people can answer fast. For long and complicated questions prefer the other forums within the support section.
Before you post please read how to get help
Post Reply
chinless_wanderer
Level 1
Level 1
Posts: 3
Joined: Thu Mar 22, 2018 11:20 am

Autostart Terminal Command on Every Boot

Post by chinless_wanderer » Thu Mar 22, 2018 12:05 pm

Greetings,

I'm aware that this topic is rather well-documented and similar questions to mine have been asked many times in many forms. I also did my homework and attempted to find a solution to my issue before posting here. Alas, to no avail. There are countless similar forum entries I know, but none of them could help me, sorry. But I hope you'll be able to help me here.

My issue: I have an ExpressVPN subscription and miraculously, I even managed to install the service on my system. What's more, I am also able to control its functioning through the terminal. Lovely up to this point. HOWEVER, I have to tell the service to start and connect every time my system boots. Instead of this, I would like the VPN connection to be established automatically so that I don't have to enter terminal commands at start-ups.

Could not do it with the GUI (Network Manager opened, OpenVPN keys and configuration files entered, WiFi set to connect to VPN done... nothing) so if I'm not mistaken now I have roughly three options left: a bash script placed in rc.local, an upstart job or a cron job. Out of these three, I would go with the bash script if possible. I know that it has to start with this line...
#!/bin/bash
...and that I have to save it as whatever.sh and place it in the rc.local folder.
But what else should be in it and how do I make it executable? Again, the command is would like to automate is "expressvpn connect".

Thank you guys! I appreciate any help.
Cheers.

Mattyboy
Level 6
Level 6
Posts: 1196
Joined: Thu Mar 26, 2015 2:17 pm

Re: Autostart Terminal Command on Every Boot

Post by Mattyboy » Thu Mar 22, 2018 2:05 pm

Name the file as the command name.

Just type in your command below

Code: Select all

#!/bin/bash
Make your script executable by right clicking on the file select properties/Permissions and check Allow executing file as program box.

Place your bash script in
/bin
Or
/usr/bin
You may have to use

Code: Select all

sudo mv
to get it there.

Add the name of the file ( command name ) to your start up programs.

Basically.

User avatar
Termy
Level 5
Level 5
Posts: 762
Joined: Mon Sep 04, 2017 8:49 pm
Location: UK
Contact:

Re: Autostart Terminal Command on Every Boot

Post by Termy » Thu Mar 22, 2018 2:27 pm

You needn't create a script. You can enter the command needed to start the service, into the /etc/rc.local file. Alternatively, you should be able to do something like sudo systemctl enable S, where "S" is the name of the service you wish to enable, which I believe would cause that service you need to be started at boot time; this has been my experience, at least. Another place you can put the command is in ~/.profile which is sourced (think "processed") during user login.
Here to help.

I'm LearnLinux (LL) on YouTube: https://www.youtube.com/channel/UCfp-lN ... naEE6NtDSg
I'm also terminalforlife (TFL) on GitHub: https://github.com/terminalforlife

User avatar
administrollaattori
Level 14
Level 14
Posts: 5460
Joined: Tue Sep 03, 2013 4:51 am
Location: Finland
Contact:

Re: Autostart Terminal Command on Every Boot

Post by administrollaattori » Thu Mar 22, 2018 4:23 pm

Install xterm and make a script, and add that script to the StartUp Applications. An example about a script.

Code: Select all

#!/bin/bash
zenity --question --text "Do you want to run this script?"
if [ $? = 0 ];
then
zenity --info --text "Close the script?"
else exit 0
fi
exit 1
Example2:

Code: Select all

#!/bin/sh
zenity --question --text "Would you like check running processes?"
if [ $? = 0 ];
then
xterm -e 'ps aux && sleep 100'
else exit 0
fi
exit 1

chinless_wanderer
Level 1
Level 1
Posts: 3
Joined: Thu Mar 22, 2018 11:20 am

Re: Autostart Terminal Command on Every Boot

Post by chinless_wanderer » Fri Mar 23, 2018 12:49 pm

Thank you guys for your immediate replies.

I don't want to infuriate anyone but these solutions don't appear to be working for me. Maybe I'm doing something wrong. Please, be patient with me. :D I mean it cannot be this hard. Could it be that ExpressVPN does not want the users to be able to connect automatically? Because I remember that when I used this service on Windows, the option to automatically connect was not given there either. You had to push a huge button in the software, manually, every time.

Sitwrap:
- I opened up a text editor, placed in it the commands (first line: "#!/bin/bash", second line "expressvpn connect"), saved it as "expressvpn connect" and placed it in both /bin and usr/bin folders. But no results.
- As to the second proposition. I don't seem to have an rc.local folder.
- And for the third, I installed xterm but could not figure out how my script should look like based on the examples and where I should put it. Sorry.

The magical command I'd like to automatically start is "expressvpn connect". (I dont't understand though, why there's no need for sudo in front of it...)

Thanks again!
Cheers.

User avatar
Termy
Level 5
Level 5
Posts: 762
Joined: Mon Sep 04, 2017 8:49 pm
Location: UK
Contact:

Re: Autostart Terminal Command on Every Boot

Post by Termy » Fri Mar 23, 2018 1:48 pm

Remove what you've created, then run this one-liner to do it for you:

Code: Select all

[ -f /etc/rc.local ] && printf "\nexpressvpn connect\n" | sudo tee -a /etc/rc.local 2>&1 > /dev/null || printf "You don't have an /etc/rc.local file, which is by default present."
You should see no output, aside from it asking your password (because we're changing a system file). If it doesn't work at this point, either you've disabled (manually or by some other process) the SystemD service which is responsible for the /etc/rc.local file, or you have incorrect permissions set for the /etc/rc.local file (should be at least 700; -rwx------). The file and thus anything within it, should be executed before a user logs in.

When you run that command normally in a terminal, does it ask for something? Like a password? You have to take a different route if you want to automate user input.
Here to help.

I'm LearnLinux (LL) on YouTube: https://www.youtube.com/channel/UCfp-lN ... naEE6NtDSg
I'm also terminalforlife (TFL) on GitHub: https://github.com/terminalforlife

chinless_wanderer
Level 1
Level 1
Posts: 3
Joined: Thu Mar 22, 2018 11:20 am

Re: Autostart Terminal Command on Every Boot

Post by chinless_wanderer » Sat Mar 24, 2018 2:59 pm

Thanks, Termy.

I did what you suggested. (Yes, I know that rc.local has been replaced by upstart.) Still, I cannot fathom why all methods fail here. I swear I'm not doing this on purpose. I mean it's just a simple command...

In other news: I found a very similar post to his one where the case was solved by a method I also tried previously (i. e.: using the Network Manager OpenVPN configuration downloaded from ExpressVPN's website), so I tried that method again (4th attempt). Did _everything_ again as it was described on the site. Results: when I enabled my WiFi connection to automatically connect to my VPN, it failed to connect to the WiFi on start-ups. Also, the VPN only worked for the first start-up. During the next boot, even though a small lock was there to indicate a VPN connection, my IP switched back to the original one.

And no, strangely enough, the command does not require a sudo prefix.

OK. I don't want to drag this for too long. If I can't solve it, I guess I will have to enter the command every time.

Still, thanks in advance.
Cheers.

Post Reply

Return to “Newbie Questions”