PDA

View Full Version : Autostart script in kde - but it keeps opening it as a text file?



wintermute000
31st October 2007, 01:53 PM
I have a script I have placed in my ~/.kde/Autostart called tune-desktop, which renices my X processes for a snappier desktop :)

sudo renice -10 `pidof X`
sudo renice -10 `pidof Xorg`

The script works well when I run it manually, but when I log into KDE, instead of running the script, it opens it up in a text editor :(

I have given the file executable permissions (chmod 777 to be precise!!!) but its still not running, its opening it in a text editor. What can I do to get it to actually RUN automatically?

note: I have placed /usr/bin/renice in my sudoers file so I don't need to input root password :)

BostonWatcher
31st October 2007, 04:31 PM
You have to create a desktop entry with the executable instruction under the "Exec"field. A simple shell script won't work.

For instance for your example you could create a file called my_script.desktop containing:


[Desktop Entry]
Name=My Script Name
Exec=sudo renice -10 `pidof X` && sudo renice -10 `pidof Xorg`
Type=Application


Of course you could also simply redirect execution to your script by replacind above "Exec=/path/to/my/script.sh".

More about desktop entries here (http://standards.freedesktop.org/desktop-entry-spec/desktop-entry-spec-0.9.3.html).

FriedChips
31st October 2007, 06:18 PM
chmod 777 will not give the file execution privelegages it changes the rw privelages, you need to do this:

chmod u+x filename

BostonWatcher
31st October 2007, 06:30 PM
chmod 777 will not give the file execution privelegages it changes the rw privelages

No. chmod 777 sets permissions to "rwx" to user, group and other, i.e. executable by everyone.

FriedChips
31st October 2007, 08:16 PM
only if it was executable to begin with.

wintermute000
31st October 2007, 11:06 PM
Well I just SSHed back home (at work :) ) and chucked the commands recommened by BostonWatcher, will test tonight.

Do you have a link to some documentation about those 'desktop entry' files? Are they basically KDE symlinks / shortcuts?

I'll also try chmod +x if that doesn't work

BostonWatcher
31st October 2007, 11:49 PM
only if it was executable to begin with.


I don't understand what you're saying. If it was executable to begin with (executable meaning here a file with executable permissions) I don't need to chmod it ;-)

"chmod 777" is the octal way of chmoding that sets explicitly the mode bits of a file to -rwxrwxrwx. Therefore it is equivalent to "chmod a+rwx", which contains chmod u+x since "u+x" only adds executable permission to the owner, to the already existing permissions of that file.

BostonWatcher
31st October 2007, 11:57 PM
Do you have a link to some documentation about those 'desktop entry' files? Are they basically KDE symlinks / shortcuts?


They are files that specify how a program should be launched, and can have many other additional metainformation. Take a look at some examples in
http://standards.freedesktop.org/desktop-entry-spec/desktop-entry-spec-0.9.3.html
(http://standards.freedesktop.org/desktop-entry-spec/desktop-entry-spec-0.9.3.html)

You will find lots of them in your system too. Basically every shortcut or start menu entry is a .desktop file. Just search for *.desktop files in you system.

BostonWatcher
1st November 2007, 01:44 AM
I have a script
sudo renice -10 `pidof X`
sudo renice -10 `pidof Xorg`


johannlo, did you include a shebang on your script? I ask this because now I see that a .desktop file is not strictly necessary (although it is perhaps the correct/standard way to do it anyway... and I always do it like that).

So, I correct my first reply and say that a bash script should be enough (with executable permissions) but I guess you have to specify who is going to interpret that script, and thus you need the shebang on the first line. For bash it is " #!/bin/bash", something like



#!/bin/bash
code here
more code
...


(incidentaly, simply naming the script with an .sh extension and without the shebang also seems to work as intended, but the shebang is the correct way to specify which program should interpret the script).

wintermute000
1st November 2007, 02:05 AM
OK I'll change it to #!/bin/bash and test when I get home

I thought # was like REM i.e. comment? or does #! have special meanining.

Yes I thought chmod 777 would suffice as it would give rwx permissions to all users, but maybe friedchips knows something I don't :)

BostonWatcher
1st November 2007, 02:29 PM
I thought # was like REM i.e. comment? or does #! have special meanining.


That is true. "#" is the comment character for bash, sh, and other scripting languages. Except when it is combined with "!" as "#!" and appears in the first line of a script. In that case it has a very special meaning: it tells your shell what program should interpret the script.

You see, if you are using bash and write a script without a shebang, then turn it into an executable file by changing its permissions it will execute nicely from your bash shell when you do


$ ./myscript

simply because bash will interpret it as a bash script.

However, if you use another shell and do the same it is not guaranteed to work. Also, you could have written a python script, a perl script a ruby script, etc. How would the shell know?

This has to do with the fact that, strictly speaking, a script needs to be interpreted by the appropriate program. So to run your bash script you should do


$ bash myscript.sh

but you don't need to if you are inside a bash shell. Likewise, if you have a python script you need


$ python myscript.py


But this is a bit boring, because most of the time you don't want to care about knowing who is going to interpret the script. For instance you probably use the yum update program in fedora. But to run yum you simply call "$ yum", not "$ python /usr/bin/yum", although yum is written in python.

That's where the shebang comes handy. You put it on the first line with the path to the interpreter and you're all set. Just make the file have executable permissions and you can run it as "$ myscript".

So the bottom line is that the shebang is always a good thing even when you know that you will only run the script from the shell. If nothing else, it immediately identifies the scripting language being used to someone else that needs to use it. All scripts in your system (and there are many) have the corresponding shebangs. Look at /usr/bin/yum or /usr/bin/firefox (yes, when you call "$ firefox" you are calling a bash script first).

FriedChips
1st November 2007, 08:17 PM
I don't understand what you're saying. If it was executable to begin with (executable meaning here a file with executable permissions) I don't need to chmod it ;-)

"chmod 777" is the octal way of chmoding that sets explicitly the mode bits of a file to -rwxrwxrwx. Therefore it is equivalent to "chmod a+rwx", which contains chmod u+x since "u+x" only adds executable permission to the owner, to the already existing permissions of that file.

You're right man, I always thought the permission levels were just for rw, I stand corrected :eek:

wintermute000
1st November 2007, 10:46 PM
Thanks heaps for that info. It worked and I learnt something new!!!