11

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 ?

Shnd
  • 223
  • I want to run the script after my X loaded. – Shnd May 07 '14 at 08:57
  • @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. – erikbstack Dec 04 '14 at 12:33

3 Answers3

5

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]
Type=Application
Exec=sudo example
Hidden=false
NoDisplay=false
X-GNOME-Autostart-enabled=true
Name=myscript
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.

Pandya
  • 35,771
  • 44
  • 128
  • 188
4

Another possibility:

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

start on desktop-start
stop on desktop-end

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

Further details to Upstart:

http://ifdeflinux.blogspot.de/2013/04/upstart-user-sessions-in-ubuntu-raring.html

http://upstart.ubuntu.com/cookbook/

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?

TuKsn
  • 4,370
  • 2
  • 26
  • 43
  • "$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
2

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:

#!/bin/sh
touch $HOME/.trigger

and one two run the command:

#!/bin/bash

FILE="/path/to/your/homedirectory/.trigger"
# don't use $HOME here, since you run it by root

if [ -f $FILE ]; then
   <your command here, run by root>
   rm -f $FILE
fi
  • 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)

    /path/to/create_trigger.sh
    
  • 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.

Jacob Vlijm
  • 83,767