Execute script on every login, not just boot

I want to execute some commands every time I log in into Gnome.

  • I tried putting it in .bashrc, but then the commands only execute on boot.
  • I tried putting it in .profile, but then the commands never executed.

I have a weird keyboard layout (Belgian Azerty MacBook), so I created a script that uses xmodmap to remap some key. But when I suspend/logout and log back in, the keys get reset.

When you're logged in, you have the correct keymap selected, but only on the login screen is the layout wrong? If that's the case, changing the layout on login isn't going to help, because the login screen itself has its own layout set that you'll probably need to change to make it functional.

I'll include a link here to some information on that, as this post reply has a lot of information on that sort of change:

How to set GDM keyboard layout in Debian 12? - Unix & Linux Stack Exchange

If you were to make a script and have it run at a user login, it wouldn't help the issue of GDM (the login screen) having the wrong layout. Let me know if I'm wrong, but that sounds like more what the issue is rather than your keyboard layout when you're successfully logged in.

1 Like

The layout wrong in the login screen, and after I log in (only a few keys).

The script I created works when I put it in the startup applications, but when I suspend or logout the keys revert back the the original mapping.
That is why I'm trying to run the script every time I log in.

Is Your Layout not available in the Keyboard Layout Choices?

1 Like

You might consider skipping the script part entirely and using Input Remapper. It can be installed via the software store or apt install input-remapper-gtk. It provides a UI for remapping and runs as a daemon, so the changes are in place even at login. (I just tested this.) There's also input-remapper, which has the same version number. I assume it's the same thing, but a non GTK version. The software store installed the GTK version and I haven't felt a need to change that.

2 Likes

No, it's not I'm afraid

I can't get Input Remapper to work. I had to input my password before starting the application and I pressed Apply, but nothing happens?

Any clues on what I could be doing wrong?

You could invoke your script using a systemd hook. All you need is to create a small script in /lib/systemd/system-sleep/, that looks something like this:

#!/bin/sh

case $1 in
    pre)
        # Do something before suspend
    ;;
    post)
        # Do something after suspend
    ;;
esac
exit 0

The name of the script doesn't matter, but it needs to be made executable.

In your case, you'd simply call the script that you wrote to update the keyboard layout, which you would still have to run on autostartup, since those are separate actions.

For reference:

https://wiki.archlinux.org/title/Power_management

https://man.archlinux.org/man/systemd-sleep.8

I see what you're trying to do. I'm not sure how you got your trigger key to say "at." When I try getting an at sign, the trigger goes on Shift R + 2, tracking the key combination. If you're using a keyboard layout other than US English, it might make more sense. The thing that I CAN see as a problem is your mapping. It looks like you're mapping to <, but Input remapper is extremely specific, which can make it difficult to find your intended target key. Try getting rid of the < and entering less. After le you should see it autocomplete, which is your hint that you're on the right track. Once I had less there, my remapping worked as expected. When I tried with <, it acted like there was no remapping at all.

As a sidenote, the reason you have to authenticate when running Input Remapper is because it uses a daemon to keep your remapping from boot, even prior to login.