PDA

View Full Version : [SOLVED] How to run a script after userlogon



sora03
14th December 2014, 06:09 AM
Hi, I am new to Fedora I just installed it yesterday. I am trying to run a specific command line program with root access on user startup but I can't find any solution to accomplish it, I've tried the 'rc.local', rc.d, init.d.. and it still doesn't work. Please help me.

marko
14th December 2014, 07:57 AM
rc.local will run it at boot up time

Fedora 21 would be running it via the rc-local.service by systemd

Just putting the file at /etc/rc.d/rc.local and making it executable (and it has to be a valid shell script) should be all you need.

If you use KDE you can add the script to the Autostart, then it would run when the desktop is loaded (System Settings-> Startup and Shutdown -> Autostart ) and in there add it to the 'Script File' list. I don't use Gnome but it probably has a similar autostart feature

PabloTwo
14th December 2014, 01:39 PM
Since all scripts in /etc/rc.d/rc.local run as root before any user login, you will need to run the script from the users autostart function in whichever desktop environment that user is using. If the script requires "sudo", you can just prefix the command in the script with sudo. But for that to work, 1) the user needs to be setup to use sudo in the /etc/sudoers file, and, 2) the user must have the 'NOPASSWD' option enabled, either globally, or for the specific command involved in the script.

Without the NOPASSWD function set, the script cannot complete automatically because it would require a password entry.

Also, see the suggestion by stevea in this thread (http://forums.fedoraforum.org/showthread.php?t=301959)

Dutchy
14th December 2014, 02:37 PM
Just use 'su -l USER -c "cmd/script.sh"' in rc.local.
-wrong topic-

dobbi
14th December 2014, 03:11 PM
$ mkdir $HOME/.config/systemd
$ mkdir $HOME/.config/systemd/user
$ echo "[Unit]
Description=VLC player

[Service]
ExecStart=/usr/bin/vlc https://www.youtube.com/watch?v=7HGC7JkC1v4
Environment=DISPLAY=:0

[Install]
WantedBy=default.target" > $HOME/.config/systemd/user/vlctest.service

$ systemctl --user start vlctest
$ systemctl --user stop vlctest

That works for when the user if I was to use it for system wide it would be on the.

/usr/lib/systemd/system/
/usr/lib/systemd/user/

or maybe use the key "User=root" in the [Service] part.

Debbuging:

systemd logs everything in journal so you can use to see why it is not starting this.


journalctl -u <name of the unit>

journalctl -u vlctest

Resources:
https://wiki.archlinux.org/index.php/Systemd
http://www.freedesktop.org/software/systemd/man/systemd.unit.html
https://stackoverflow.com/questions/15146049/linux-start-up-script-in-systemd