How to run command at startup?

10,949

Solution 1

Although you asked for a solution on startup, this might do what you want as well: to run a command on login, on user level, here is a simple one:

For example, to set the PageDown key as Return key on login:

Create a .desktop file as below, save it in ~/.config/autostart

[Desktop Entry]
Name=Set Keyboard
Exec=xmodmap -e 'keycode 117 = Return'
Terminal=false
Type=Application

Copy the text above, paste it into an empty textfile (gedit), save it as set_keyboard.desktop (or anything_else.desktop) in ~/.config/autostart

Solution 2

The easiest way to run commands at on user login (which is the best way to accomplish what you want) is to add the command to the .profile file (located in /home/[user]/). You want to add the command to the end of the file. For example:

[user@host ~]# nano .profile
# ~/.profile: executed by the command interpreter for login shells.
# This file is not read by bash(1), if ~/.bash_profile or ~/.bash_login
# exists.
# see /usr/share/doc/bash/examples/startup-files for examples.
# the files are located in the bash-doc package.

# the default umask is set in /etc/profile; for setting the umask
# for ssh logins, install and configure the libpam-umask package.
#umask 022

# if running bash
if [ -n "$BASH_VERSION" ]; then
    # include .bashrc if it exists
    if [ -f "$HOME/.bashrc" ]; then
        . "$HOME/.bashrc"
    fi
fi

# set PATH so it includes user's private bin if it exists
if [ -d "$HOME/bin" ] ; then
    PATH="$HOME/bin:$PATH"
fi

# Remap Return key to Page Down on login
xmodmap -e 'keycode 117 = Return'

You should be able to logout and log back in to make this change take effect. However, it may be best just to reboot (to fully test it out).

Share:
10,949

Related videos on Youtube

johngreen
Author by

johngreen

Updated on September 18, 2022

Comments

  • johngreen
    johngreen over 1 year

    The Return key in my keyboard is broken, so I have chosen to make my PgDn key to act like the Return key. Now I learned how to do this using 'xmodmap'. The command I have to run is:

    xmodmap -e 'keycode 117 = Return'
    

    But I have to run this command everytime I reboot. I googled around and found that placing this command in the /etc/rc.local file should run it automatically at start-up. However, it does not work.

    Can anyone tell me how can I execute this command when I boot up? I am using Ubuntu 14.04.

    • jobin
      jobin about 10 years
      Where exactly did you place your command in the file? After or before exit 0?
    • web.learner
      web.learner about 10 years
      @JacobVlijm I don't see anything to do with the GUI in this question, and your answer isn't a GUI answer..
    • Jacob Vlijm
      Jacob Vlijm about 10 years
      @Seth my answer is a GUI answer, and does not work when logging in via command line, while the supposed duplicate Karel mentioned explicitely asked for a solution that also works for non-GUI.
  • johngreen
    johngreen over 9 years
    No. I want to run the command on startup, even before login. Is this even possible?