2

After I start up, I open Terminal and run 2 sudo commands. I am trying to automate this step.

I issue these 2 commands (to activate a 3Dconnexion SpaceMouse 3D mouse):

sudo xhost + sudo /etc/init.d/spacenavd restart 

I have tried 3 ways to automate these 2 commands but none has worked:

  1. I put them both in a bash script spacenavd.sh and place it in my user home.
  2. In System Settings > Applications > Startup ( see attachment below )
  3. I create a script /etc/rc.local and give it permissions.

On start up I tail -n 100 /var/log/syslog but have not found a solution that works. Any tips appreciated.

Elementary rules. Big thanks to all the effort going into it.

enter image description here

2
  • Might I ask whether these are needed systemwide or if it's specific to when you log in? If it's systemwide, there's a method I can suggest that runs it during boot instead of login. Unfortunately you won't have much luck getting sudo commands running in desktop startup scripts. Commented Jan 27, 2019 at 20:05
  • @Schyken Thanks for the time. Running during boot would work. What's the method? Commented Jan 28, 2019 at 20:31

3 Answers 3

2

So I solved my problem.

It turned out I did not actually need to use sudo with these commands.

What I did was to save the 2 commands (without sudo) in a bash script in my home folder. Then I gave it permissions to be executed by me and my group. Then, in elementary juno, I went to System Settings > Applications > Startup and clicked the + in bottom left to add the script.

This action creates a file in ~/.config/autostart/ with basic info that results in something very similar to my original screenshot, named Custom Command and with a stock icon. I was able to clean up the look by editing basic info in this file, like name, and adding an icon in ~/.local/share/icons/hicolor/128x128/apps/ called spacenavd.png.

To test is, I include sudo in front of both commands in the bash script, and execute it from terminal, then in terminal, I am prompted for a password.

But when I do not include sudo in front of either command in the script, and execute it from terminal, then this dialogue box pops in and prompts me to enter my password:

spacenavd auth dialogue

When I boot up and login, I am not given this dialogue, and I believe that's because I have just logged in.

Here is how it looks now:

Spacenav startup item in elementary juno

Here is the spacenavd.sh file in my home folder

#! /bin/bash xhost + /etc/init.d/spacenavd restart 

The permissions on the script

spacenavd.sh permissions

The dekstop file at ~/.config/autostart/ which I renamed spacenavd.desktop

[Desktop Entry] Name[en_US]=Spacenav Comment[en_US]=3D Controller Exec=./spacenavd.sh Icon=spacenavd X-GNOME-Autostart-enabled=true Type=Application 

And the spacenavd icon ~/.local/share/icons/hicolor/128x128/apps/spacenavd.png

spacenavd icon

The 3Dconnexion SpaceMouse Wireless 3D Mouse 2.4GHz via Micro-USB (3DX-700043)

My setup running on a model A1398 macbook pro 2012:

elementary juno macbook pro 2012 model A1398

1

In the event that you'd like something to run during boot, you can utilize systemd services. I initially used this guide to assist me in setting up a startup run for a sleep inhibitor, but it can be modified for your uses as well.

The script file (.sh file) would instead have your sudo xhost + and sudo /etc/init.d/spacenavd restart commands in it.

An example, here:

#! /bin/bash sudo xhost + sudo /etc/init.d/spacenavd restart 

You may choose to name this script whatever you'd like, but I definitely recommend naming it something relevant to the commands inside it just for future reference. Placing it in a directory like /usr/local/bin/ where it can reside safely is also a good idea. If you have it resting in your home directory or something of the sort and it disappears, your system is going to be looking for a file that doesn't exist.

I hope this does what you're looking for! Feel free to comment with results, and I can edit my answer accordingly to accommodate for them.

0
0

They're probably not going to work because sudo is expecting you to enter a password to run them.

You can exempt those applications by creating special rules for them in sudoers, such that they can be executed without a password. Here's some documentation on how to do that.

3
  • thanks for the ideas! There is a way to let the system prompt the user for password with dialogue.. I only know because I did it. But then I couldn't recreate it. Commented Jan 30, 2019 at 17:30
  • There used to be a tool called gksu but it went away. I'm not sure what the current practice is for that, no. Commented Jan 30, 2019 at 18:40
  • revised answer as I was able to recreate the auth dialogue prompt. Commented Jan 31, 2019 at 17:04

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.