PDA

View Full Version : Autostart Python Script during Booting



Eagleeye777
9th August 2013, 05:50 PM
Hey there,

I have recently switched to Fedora 19 and still try to find my way back into it.

What I currently want to do is run a python script I have written to initialize my Razer Keyboards additional keys.

The script is working fine when started manually in a terminal.
What I would no like to achieve is run this script whenever Fedora boots up, so the additional Keys are usable after initial booting.

What do I actually have to do in Fedora 19 to autostart this script properly? I am quite confused as to where to drop it, so it just autoruns whenever I boot.

Help would be greatly appreciated.

Thanks

Eagleeye

marko
9th August 2013, 09:39 PM
You can run the script from the /etc/rc.local file

It should work as long as this service (bolded) is enabled:

$> systemctl -a list-units | grep local
rc-local.service loaded active exited /etc/rc.d/rc.local Compatibility

Eagleeye777
10th August 2013, 08:52 AM
Thanks for the suggestion. As it seems the service you mentioned is not running. It's listed as loaded inactive and dead. I tried to start it using

systemctl start rc-local but that just does not work either.

systemctl status rc-local returns:


rc-local.service - /etc/rc.d/rc.local Compatibility
Loaded: loaded (/usr/lib/systemd/system/rc-local.service; static)
Active: inactive (dead)
start condition failed at Sa 2013-08-10 09:41:20 CEST; 11s ago

So as far as I understand it, the service refuses to run. So far so bad. What do I have to do now?

marko
10th August 2013, 05:01 PM
Some services will not just start, you have to enable them first, then start them:


systemctl enable rc-local
systemctl start rc-local


I think this might be because rc-local is very general and so it could be dangerous to the system, that is it's running anything that's in the /etc/rc.local. In comparison something like "NetworkManager.service" just starts and stops a network interface and is pretty limited in scope and is encapsulated by selinux pretty well.

Eagleeye777
10th August 2013, 07:50 PM
doing what you suggested returns this:



systemctl enable rc-local
The unit files have no [Install] section. They are not meant to be enabled
using systemctl.
Possible reasons for having this kind of units are:
1) A unit may be statically enabled by being symlinked from another unit's
.wants/ or .requires/ directory.
2) A unit's purpose may be to act as a helper for some other unit which has
a requirement dependency on it.
3) A unit may be started when needed via activation (socket, path, timer,
D-Bus, udev, scripted systemctl call, ...).



So how to go from here?
Somehow it must be possible to get that service up wouldn't it

vallimar
10th August 2013, 08:01 PM
Did you make the rc.local file executable? Do a chmod 755 on it.

Eagleeye777
10th August 2013, 08:04 PM
i do have an executable python script, as I said above.
All I want to do is find a way to run this while Fedora boots up.
Unfortunately, there seems to be no simple way to just run an easy peasy script.
In Dos I used to have batch files. In windows I had ways to do this.
In Fedora ??????????????????

pinkfloyd
11th August 2013, 12:12 AM
vallimar meant that you have to make the file rc.local executable, locatet in /etc

marko
11th August 2013, 01:07 AM
@Eagleeye777

I'm 100% sure the rc-local systemctl thing works, I use it to load my power profiles, the /etc/rc.local has to be chmoded to executable like mentioned before

Also there should be a link from /etc/rc.d/rc.local to /etc/rc.local because if you look in /usr/lib/systemd/system/rc-local.service
you'll see that the ExecStart is actually running "/etc/rc.d/rc.local start"
(I suppose you can also just put the real script at /etc/rc.d/rc.local and skip the link and file at /etc/rc.local too) and it's a requirement that /etc/rc.d/rc.local is executable (or the link there points to /etc/rc.local and it is executable and a good script)

/usr/lib/systemd/system/rc-local.service:


# This unit gets pulled automatically into multi-user.target by
# systemd-rc-local-generator if /etc/rc.d/rc.local is executable.
[Unit]
Description=/etc/rc.d/rc.local Compatibility
ConditionFileIsExecutable=/etc/rc.d/rc.local
After=network.target

[Service]
Type=forking
ExecStart=/etc/rc.d/rc.local start
TimeoutSec=0
RemainAfterExit=yes
SysVStartPriority=99

Eagleeye777
11th August 2013, 02:02 PM
I am totally lost now!!!!!!!!!
I am gonna give this one more shot otherwise I will just give up on this distro.

Could someone please provide a working guide for me on how to start a script during bootup. It can't be that hard.

Btw: I do not have a /etc/rc.local file. You guys keep talking about it, but it is not part of my installation.

I seriously do not understand that I am doing research for days now, trying out everything people tell me and none of this works. I just want to autostart a script. Period.

pinkfloyd
11th August 2013, 03:07 PM
sudo touch /etc/rc.local

xeonix
11th August 2013, 03:15 PM
Eagleeye777:
Why not just place your execuatle in your "~/.bashrc" as most probably you are using RL5 :]



username$echo "python /path/to/yourscript.py" >> /home/user/.bashrc

Eagleeye777
11th August 2013, 04:02 PM
that does not work either. Tried already

mekanopsis
11th August 2013, 04:57 PM
If you're using GNOME 3 (and presumably 2) you can specify commands to run on login with gnome-session-properties. That's how I do the equivalent of Windows' "Startup Programs" type stuff.

---------- Post added at 04:57 PM ---------- Previous post was at 04:54 PM ----------

By the way this is a problem with GNOME3 in my opinion. gnome-session-properties ought to be in the settings panel. Far too many kinds of configuration options are hidden by default!

marko
11th August 2013, 05:22 PM
Do you only care about those special keys being configured when using the GUI desktop. IF so you can usually run the script from Autostart (KDE) and I guess Gnome has something similar.

In the KDE case you'd go to System Settings, then 'Startup and Shutdown" in the "System Administration" section of System Settings, select "Autostart" and add the python script with the "Add Script.." button.

Any Autostart script is run when KDE desktop is logged into by the user.

marko
11th August 2013, 05:48 PM
I am totally lost now!!!!!!!!!
I am gonna give this one more shot otherwise I will just give up on this distro.

Could someone please provide a working guide for me on how to start a script during bootup. It can't be that hard.

Btw: I do not have a /etc/rc.local file. You guys keep talking about it, but it is not part of my installation.

I seriously do not understand that I am doing research for days now, trying out everything people tell me and none of this works. I just want to autostart a script. Period.

The /etc/rc.d/rc.local script doesn't not exist by default --- You create it yourself

The rc-local service is designed to run /etc/rc.d/rc.local (note that the rc-local.service runs the file /etc/rc.d/rc.local so make your file there, putting the file in /etc/rc.local is just a unix convention thing and is optional, some people make a link from /etc/rc.d/rc.local to point to /etc/rc.local so the file is more obvious by putting it in /etc/ directory where lots of other config stuff is stored )

The steps:

1) start a terminal, then switch user (su) to root user


su <root password>2) edit the /etc/rc.d/rc.local file to create it and to add the python script, this example uses the 'nano' editor which is more new user friendly than 'vi' editor, you should have nano already but install it if it's missing


nano /etc/rc.d/rc.local
I put the example python script in /usr/local/bin/keyboard.py, substitute yours of course, as a shell script you need to put #!/bin/bash
as the first line



#!/bin/bash

/usr/local/bin/keyboard.py

exit 03) save and exit nano editor.
Make the file read/write/executable to root and readable to all others with chmod (read/write/execute to root and readable only to group and others == 744)


chmod 744 /etc/rc.d/rc.localIf rc-local.service is enabled and running, it will run the script next boot up and future bootups

TerryMcCann
11th August 2013, 07:17 PM
Marko,

Thumbs up and kudos for understanding Eagleeye777's question and providing an excellent tutorial.

Terry

Eagleeye777
12th August 2013, 03:08 PM
thank you!