Run script on login (script with sudo) or startup

59,262

1. Using /etc/profile.d

You can run the script on login by placing the script in /etc/profile.d/

These files are executed upon login.

To create a symbolic link to the file you want to execute, use

sudo ln -s /home/karl/.scripts/startup/sensei-raw-startup.sh /etc/profile.d/myscript.sh

2. Using upstart

Another possibility is to use upstart

start on desktop-session-start

and place your script there.

Share:
59,262

Related videos on Youtube

Karl Morrison
Author by

Karl Morrison

Updated on September 18, 2022

Comments

  • Karl Morrison
    Karl Morrison almost 2 years
    #!/bin/bash
    ids=$(xinput list | awk '/SteelSeries Sensei Raw Gaming Mouse .*pointer/ {print $8}' | sed 's/id=\(.*\)/\1/')
    
    if [ -z "$ids" ]; then
      exit 0;
    fi
    
    read -a ids_array <<< $ids
    
    echo fixing id ${ids_array[0]}
    xinput set-prop ${ids_array[0]} 'Device Accel Profile' -1
    xinput set-prop ${ids_array[0]} 'Device Accel Constant Deceleration' 2.5
    xinput set-prop ${ids_array[0]} 'Device Accel Adaptive Deceleration' 1
    xinput set-prop ${ids_array[0]} 'Device Accel Velocity Scaling' 1
    
    echo fixing id ${ids_array[1]}
    xinput set-prop ${ids_array[1]} 'Device Accel Profile' -1
    xinput set-prop ${ids_array[1]} 'Device Accel Constant Deceleration' 1.5
    xinput set-prop ${ids_array[1]} 'Device Accel Adaptive Deceleration' 1
    xinput set-prop ${ids_array[1]} 'Device Accel Velocity Scaling' 1
    
    sudo sensei-raw-ctl --show
    sudo sensei-raw-ctl --polling 500
    sudo sensei-raw-ctl --cpi-on 450
    sudo sensei-raw-ctl --cpi-off 5670
    
    unset ids
    unset ids_array
    

    I wish for the following script to run once when I login or when the computer starts up. The above script is located in /home/karl/.scripts/startup/sensei-raw-startup.sh.

    I DO NOT wish to use the GUI to add the script. I wish to learn a bit more about how to do it manually.

    What files do I need to create, what must be in them and where should they be located to be able to run my script which is located in the said directory.

    • Julen Larrucea
      Julen Larrucea over 8 years
      Here they explain how to run an script at boot as root (in /etc/init) as user (/home/user/.config/upstart) or even with a "@reboot" task in cron: askubuntu.com/questions/814/how-to-run-scripts-on-start-up
    • mchid
      mchid over 8 years
      Either rclocal or systemd on a newer system as seen in the posted link.
    • mcantsin
      mcantsin over 8 years
      @Julen creating a cronjob is not the right solution, as @reboot starts the script when the system is started, not when the user logs in. help.ubuntu.com/community/CronHowto
  • Karl Morrison
    Karl Morrison over 8 years
    How does this handle sudo commands?
  • mcantsin
    mcantsin over 8 years
    @KarlMorrison personally I think you should use the upstart solution, as /etc/profile.d is not really meant to be used like this. My suggestion is more a "hack". I you still want to try, just add the echo "password" in front of the | sudo -S part, whereafter sudo won't ask for the password any more.
  • Karl Morrison
    Karl Morrison over 8 years
    Indeed I've tried: askubuntu.com/questions/704815/upstart-not-running-sh-script perhaps you could see whats wrong? :/
  • mcantsin
    mcantsin over 8 years
    @KarlMorrison I think the error is with the part start on runlevel [2345]. Use the solution I suggest: start on desktop-session-start which I think is what you want: starting when the desktop session starts. - no? (Regarding upstart I will respond in your other ticket)
  • Karl Morrison
    Karl Morrison over 8 years
    Just tried with what you suggested, still no effect :( I need the script to be loaded when the login screen appears (as the mouse can be used there). So basically before logging into the user.
  • mcantsin
    mcantsin over 8 years