Running script on login

About writing shell scripts and making the most of your shell
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Locked
vorastrix

Running script on login

Post by vorastrix »

Hello all,

I am trying to run a script on login to solve the Caps Lock delay problem,
turning off the delay when Caps Lock is pressed quickly. To do so, I have created a script. The following works in the terminal:

Code: Select all

sleep 5s && sh /home/USERNAME/.capslockfix/caps_lock_fix
Where "caps_lock_fix" is a file containing the following:

Code: Select all

#!/bin/sh
xkbcomp /home/USERNAME/.capslockfix/myxkbmap $DISPLAY
My problem is the following: when I open "Startup Application Preferences" and add the command:

Code: Select all

sleep 5s && sh /home/USERNAME/.capslockfix/caps_lock_fix
It does not execute on startup. The same exact command words when I run it in the terminal myself.

How can I make this script run on login?

Any help or pointers greatly appreciated.
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 1 time in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
User avatar
austin.texas
Level 20
Level 20
Posts: 12003
Joined: Tue Nov 17, 2009 3:57 pm
Location: at /home

Re: Running script on login

Post by austin.texas »

Place the executable script in /home/yourusername/.config/autostart
If that folder does not exist - create it.
Mint 18.2 Cinnamon, Quad core AMD A8-3870 with Radeon HD Graphics 6550D, 8GB DDR3, Ralink RT2561/RT61 802.11g PCI
Linux Linx 2018
vorastrix

Re: Running script on login

Post by vorastrix »

austin.texas wrote:Place the executable script in /home/yourusername/.config/autostart
If that folder does not exist - create it.
I tried this just now. It did not work.

The following did work:
  1. Moving the script to /bin and renaming it to "caps".
  2. Adding the command "caps" to Startup Applications.
Although this fixes the problem, I still do not know why the other methods didn't work. Any help as to why would be appreciated.
LinuxJim

Re: Running script on login

Post by LinuxJim »

vorastrix wrote: Although this fixes the problem, I still do not know why the other methods didn't work. Any help as to why would be appreciated.
Because the command line you tried to enter makes the assumption that it will be executed by bash. The "&&" is a bash-ism.

You can check the box that says "Run in Terminal" - that will spawn a bash process to execute your command.

It is a very bad idea to add your own scripts or programs to /bin, /sbin, or /usr/bin. You can easily break the system that way. Better to use /usr/local/bin, even better to use /home/(username)/bin.
BlackVeils

Re: Running script on login

Post by BlackVeils »

if it were me, i would try this startup program command instead:

Code: Select all

bash -c "sleep 30s; xkbcomp /home/USERNAME/.capslockfix/myxkbmap $DISPLAY"
or

Code: Select all

bash -c "sleep 30s; /home/USERNAME/.capslockfix/caps_lock_fix"
User avatar
austin.texas
Level 20
Level 20
Posts: 12003
Joined: Tue Nov 17, 2009 3:57 pm
Location: at /home

Re: Running script on login

Post by austin.texas »

To combine the excellent advice from LinuxJim and BlackVeils:
Make a directory named bin

Code: Select all

mkdir /home/(yourusername)/bin
When that directory exists, it is automatically added to your path ($PATH) at login, so it functions exactly like /bin or /usr/sbin
If your script is in /home/(yourusername)/bin, the launcher can be:

Code: Select all

bash -c "sleep 30s  ;  .capslockfix/caps_lock_fix"
Mint 18.2 Cinnamon, Quad core AMD A8-3870 with Radeon HD Graphics 6550D, 8GB DDR3, Ralink RT2561/RT61 802.11g PCI
Linux Linx 2018
vorastrix

Re: Running script on login

Post by vorastrix »

Thank you LinuxJim, BlackVeils and austin.texas.

I ended up using austin.texas' solution, which worked very well.
Locked

Return to “Scripts & Bash”