How can I run a python script at startup as root using Startup Applications?

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:

[Desktop Entry]
Type=Application
Exec=/bin/bash -c "sudo /home/zenzen/autoclicker.py"
Terminal=true
Name=Autoclicker
Comment=A descriptive comment

This will make a new entry available when you launch the Startup Applications:

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:

zenzen ALL=(ALL:ALL) NOPASSWD:/home/zenzen/autoclicker.py

Again, change the user and path to the script file with your own. You can logout from your account and log back in, and test if it's already working.

Note that there are other ways to do this but it all depends on your particular needs. I think this is a simple enough way to go about doing this.

6 Likes

Hello @littlekun

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:

  1. Create file /etc/sudoers.d/IgnorePassword with the following content:

  2. Create Autostart (.desktop) in /home/urs/.config/autostart with the following content:


  3. Logout and login for testing purposes

After logging in again, my script /home/urs/mount_NAS_urs.sh is unfortunately still not called automatically. :sweat_smile:

Can you please tell me what I'm doing wrong if a shell script can also be called automatically without a password in this way?

Thank you in advance for your support.

Best regards,
Kobi

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.