Where are the startup scripts for Unity Desktop?
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 ?
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.
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?
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
andrun_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.