Run interactive script at KDE Plasma 5 startup and add keys to ssh agent

Estimated reading time of this article: 2 minutes

How to run an interactive script at KDE Plasma 5 startup?
How to add ssh keys to ssh agent?

KDE Plasma 5 allows to set startup scripts, see How to autostart programs on KDE Plasma 5.

I want to add my ssh keys at every startup to the ssh agent. However, the keys are encrypted and adding them to the agent requires a password. I need to type the password.

Screenshot "KDE Plasma 5 Autostart Script"KDE Plasma 5 Autostart Script

How to do it:

  1. Save the script below into your home directory as init_env.sh
  2. Set the executable bit of the script file, e.g. chmod 755 init_env.sh
  3. Install as startup script in KDE
    1. Open KDE System Settings
    2. Click Startup and Shutdown icon
    3. Change to planel Autostart
    4. Click on Add Script… button
    5. Choose init_env.sh
    6. Enable Create as symlink
  4. Reboot and test

init_env.sh

#!/bin/bash

# no parameters -> open konsole window
if test $# -eq 0; then
    # wait a little bit
    sleep 15
    # call itself with parameter in a konsole executing bash with command
    konsole --workdir "$HOME" -e bash -c "$HOME/init_env.sh -i"
# parameters -> execute init steps
else
    # add id_rsa key to ssh agent if not added yet
    if ! ssh-add -l | grep id_rsa; then
        ssh-add ~/.ssh/id_rsa
    fi
fi