I tried running Startup Applications as root using sudo gnome-session-properties but my autoclicker script doesn't seem to run at all. I can however get it to run in the terminal using sudo python3 ./autoclicker.py and everything works as intended.
The program won't run without root as the uinput kernel module is restricted to privileged users, would require messing with permissions, and probably would get messy quick. Here's the script if anyone wants to test it out. I use piper to set the macro ctrl+alt+1 on one of my mouse buttons and it works like a charm.
Auto Clicker Script
#!/usr/bin/python3
import time
import uinput
import keyboard
# Install these pip packages as root as you cannot run this program in user mode.
# sudo pip install keyboard && sudo pip install python-uinput
keys = [uinput.KEY_LEFTSHIFT,
uinput.KEY_SPACE,
uinput.BTN_LEFT,
uinput.BTN_MIDDLE,
uinput.BTN_RIGHT,
uinput.REL_X,
uinput.REL_Y,
uinput.REL_WHEEL,
uinput.REL_HWHEEL,
uinput.KEY_TAB,
uinput.KEY_ENTER]
device = uinput.Device(keys)
time.sleep(0.5)
print("Waiting for Ctrl + Alt + 1...")
while True:
# Wait until Ctrl + Alt + 1 is pressed
keyboard.wait('ctrl+alt+1')
print("Key combination detected, executing button clicks...")
device.emit(uinput.BTN_LEFT, 1)
device.emit(uinput.BTN_LEFT, 0)
time.sleep(0.1)
print("Returning to wait for Ctrl + Alt + 1...")
There are two parts to this question: using Startup Applications to select what runs on session startup and how to avoid having to enter your password when running the script non-interactively.
For the first part, you need to create a new file with a .desktop extension in /home/zenzen/.config/autostart (change your username accordingly). For a simple script, the contents can look something like this:
Obviously, adjust the Exec line to point to the right location as per your needs. Notice that in order to invoke sudo we need to pass in the command to bash, as it would normally be done directly on the terminal.
For the second part, create a new exception rule that will allow your user to run this specific command without entering your password. To do that, create a new file in /etc/sudoers.d. The name doesn't matter, and the contents should be a single line:
Thanks for the tip on suppressing password input when calling a script. Does this only work with a Python script? Or should it also work with a bash shell script?
Unfortunately, I tried it with a bash shell script without success. I did the following:
Create file /etc/sudoers.d/IgnorePassword with the following content:
The sudo utility is used to change the permissions of the running process, so you could use it for any kind of program.
This is where you need to specify that you're running sudo. The exec line should read: Exec=/bin/bash -c "sudo /home/urs/mount_NAS_urs.sh". I would also recommend adding Terminal=true in the desktop file.