I want to run a script as soon as my lightdm authentication succeed, and my Unity starts loading. and i want to run my scripts as a root user.

where are startup scripts located in Unity ?

  • I want to run the script after my X loaded. – Shnd May 7 '14 at 8:57
  • possible duplicate of How to run a script during boot as root – rubo77 Oct 14 '14 at 19:56
  • @Shnd X actually starts before lightdm, because lightdm also runs in X. So if you want to run something as your user you probably want it "after lightdm authentication succceeded" or in other words "when the desktop is starting". It is also possible to run a script when you computer just bootet, no matter if you have a logged in user or not (that is what starts X for example) than that is a completely different question. If you want to run something as user, you probably want that and the answer depends on your init system. – erikbwork Dec 4 '14 at 12:33

First put your script into /usr/bin and give execute permission.

Now create .desktop file in /home/[user-name]/.config/autostart/ which run your script which runs at startup.

Example:- Let your filename of script is "example" or "example.sh"

Create .desktop file with gedit with following lines and save as filename.desktop in /home/[user-name]/.config/autostart/

[Desktop Entry]
Exec=sudo example
Comment=Startup Script

Here Exec=sudo example or Exec=sudo example.sh runs your script as a root from /usr/bin Give execute permission to .desktop file.

Now, Script runs on startup.

  • 1
    for running sudo without password, see askubuntu.com/questions/39281/… – Pandya May 7 '14 at 13:31
  • Thanks that works. But with upstart you can make the same thing perhaps a little bit simpler (look at my answer). – TuKsn May 7 '14 at 16:53
  • This would be perfect with a link to the spec – erikbwork Dec 4 '14 at 12:38
  • I don't think the .desktop file needs to be flagged executable. – Julian Mar 16 '18 at 17:45

Another possibility:

Create a file in $HOME/.config/upstart/my-upstart-script.conf

start on desktop-start
stop on desktop-end

  sudo fdisk -l > /home/[user-name]/upstart-test.txt        
end script

Further details to Upstart:



Infos to run sudo without password:

How to run an application using sudo without a password?

How do I run specific sudo commands without a password?

  • "$HOME/.config/upstart" no such directory on my machine. – Kalamalka Kid Jun 13 '14 at 21:10
  • 1
    @KalamalkaKid Which Ubuntu version do you use? The upstart directory should be at this location /home/yourusername/.config/upstart/. Open your home director in nautilus, press Ctrl+H and you should see the directory .config – TuKsn Jun 13 '14 at 21:39

To run a command as root, after login, there is another simple trick:

It takes two steps:

  • create a trigger file on login
  • create a cronjob, run by root (set in /etc/crontab), to run a tiny script (running your command) if and only if the trigger file exists. Since the trigger file is removed by the same script, your command only runs once.

The sequence then is:

USER LOGIN > trigger file is created > cronjob runs script (with your command) and removes trigger file, > next time the script passes, since the trigger file does not exist anymore

The setup

The two small scripts:

One to create the trigger file on login:

touch $HOME/.trigger

and one two run the command:


# don't use $HOME here, since you run it by root

if [ -f $FILE ]; then
   <your command here, run by root>
   rm -f $FILE
  • copy both scripts into two empty files, save them as create_trigger.sh and run_command.sh.
  • For convenience reasons, make them both executable.
  • Add the following command to your Startup Applications (Dash > Startup Applications > Add)

  • Add the following line to the /etc/crontab file (sudo nano /etc/crontab):

    * * * * * root /path/to/run_command.sh

Now the defined command runs a single time within one minute from login.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.