That's a question I've seen several time for several Linux flavours, so let's try to be exhaustive.
What is the method to execute script/command/program before and after user login into its desktop session ?
To run a program in graphical environement before a user logged in a graphical environment depend on your display manager. A display manager is in charge to provide you a login interface and setup your graphical environment once logged in.
the most important are the following:
- GDM is the GNOME display manager.
- LightDM is a cross-desktop display manager, can use various front-ends written in any toolkit.
- LXDM is the LXDE display manager but independent of the LXDE desktop environment.
- SDDM is a modern display manager for X11 and Wayland aiming to be fast, simple and beautiful.
We will review how to setup the execution of command when the display manager popup before any user logged in and how to execute something when someone is finally logged in.
If you don't know which one you're running, you can refer to this question :
Is there a simple linux command that will tell me what my display manager is?
Before I start, you are going to edit file that except if mention execute command as
root. Do not remove existing stuff in those files except if you know what you're doing and be careful in what you put in those file. This could remove your ability to log in.
Be careful with GDM, it will run all script as
root, a different error code than 0 could limit your log in capability and GDM will wait for your script to finish making it irresponsive as long as your command run.
For complete explanation read the documentation.
If you need to run commands before a user logged-in you can edit the file:
This file is a shell script that will be executed before the display manager is displayed to the user.
If you need to execute things once a user has logged in but before its session has been initialize edit the file:
If you want to execute command after the session of session initialization (env, graphical environment, login...) edit the file:
I will talk about lightdm.conf and not about /etc/lightdm.conf.d/*.conf. You can do what you want what is important is to know the options you can use. Be careful with lightDM, you could already have several other script starting you should read precisely your config file before editing it. also the order in which you put those script might influence the way the session load.
LightDM works a bit differently from the others you will put options in the main configuration files to indicate script that will be execute.
Edit the main lightDM conf file:
add a line
This script will be executed when lightDM shows the login interface.
You can edit
/etc/lightdm/lightdm.conf once again and add a line:
This will run the script as
root after a user successfully logged in.
If you want to execute command before anyone logged in, you can edit the shell script:
If you want to execute command after someone logged in but as root, you can edit the shell script:
And if you want to run command as the logged in user, you can edit the script:
Couldn't find proper information about that... Feel free to edit this part.