Thread: Linux: Run a Program on Login

  1. #1
    Registered /usr
    Join Date
    Aug 2001
    Location
    Newport, South Wales, UK
    Posts
    1,273

    Red face Linux: Run a Program on Login

    Hello,

    I'm at a point now where I can easily find my way around Linux systems, and can even remember vim commands(!), but still struggle with basic tasks sometimes.

    Specifically, I want to set up a user so that when they login, Midnight Commander is automatically launched. This from a console login, no X.

    I've tried specifying /usr/bin/mc in /etc/shells and then changing the user's shell in /etc/passwd, this almost works correctly but I have a feeling that when mc launches subshells, it actually launches copies of itself. mc should only be running once, and should not be exitable.

    I've also tried creating a .profile in the user's home directory and putting "/usr/bin/mc" in it, this seems to cause the console to freeze, logging in as root on another console and killing the mc process returns the user to the login prompt.

    It is possible to still have bash as the shell, but mc always runs on login and avoids returning (I guess using respawn)?

  2. #2
    Registered /usr
    Join Date
    Aug 2001
    Location
    Newport, South Wales, UK
    Posts
    1,273
    After reading round a few wikis, I found that the following is correct for the shell that I am using (ash), written into .profile in the user's home directory:-
    Code:
    if [ -n "$PS1" ] && [ -z "$STARTED_PROGRAM" ]; then
      export STARTED_PROGRAM=1
      /usr/bin/mc && exit 0
      echo "Launch failed! continuing with normal startup"
    fi
    This kind of shell scripting isn't massively friendly, but I guess that -n means "variable defined" and -z means "variable not defined".
    Prefixing the program path with respawn should prevent normal exiting.

    I now have a sense of accomplishment.

  3. #3
    Registered User
    Join Date
    Nov 2017
    Posts
    6
    When Bash starts, it executes the commands in a variety of different scripts.
    [COLOR=#242729][FONT=Arial]When Bash is invoked as an interactive login shell, it first reads and executes commands from the file /etc/profile, if that file exists. After reading that file, it looks for ~/.bash_profile, ~/.bash_login, and ~/.profile, in that order, and reads and executes commands from the first one that exists and is readable.
    Last edited by Salem; 4 Weeks Ago at 02:18 AM. Reason: spammy links deleted

  4. #4
    Registered User userxbw's Avatar
    Join Date
    Aug 2017
    Posts
    835
    what desktop / window manager are you using?
    fluxbox, windowmaker that goes in the autostart file, among a few other wm's.
    xfce4 and a few more have a place where you edit the conf and add your own custom start up commands.
    so everything you want started starts on login.

  5. #5
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Angola
    Posts
    8,445
    Quote Originally Posted by SMurf View Post
    Code:
    if [ -n "$PS1" ] && [ -z "$STARTED_PROGRAM" ]; then
      export STARTED_PROGRAM=1
      /usr/bin/mc && exit 0
      echo "Launch failed! continuing with normal startup"
    fi
    If I understood your requirements correctly, you can replace all of the above with just:

    Code:
    [ pgrep -x mc > /dev/null ] && /usr/bin/mc
    Shell scripting isn't complicated. To an untrained eye it can share Perl's reputation of being difficult to read. It's not however.
    But I do suggest you drop ash, unless you are using BSD. If you are using Linux you want bash or zsh. I'd suggest bash as a starter.
    The best place to start is Greg's Wiki, the most authoritative source for bash programming on the web IMHO. Especially because it also includes a whole lot of information on standardization and best practices. The #bash IRC channel is the perfect place to fill in the rest of the knowledge. It's very active.
    After a good shell understanding, you should complement your knowledge with Python and become a shell god.
    Last edited by Mario F.; 4 Weeks Ago at 10:00 AM.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  6. #6
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Angola
    Posts
    8,445
    Missed an obvious mistake above:

    Code:
    [ pgrep -x mc > /dev/null ] || /usr/bin/mc
    Otherwise it would never launch.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C login program help
    By unknown_959 in forum C Programming
    Replies: 11
    Last Post: 03-29-2013, 12:20 AM
  2. Replies: 8
    Last Post: 12-08-2009, 12:55 PM
  3. Login program
    By kollurisrinu in forum C Programming
    Replies: 26
    Last Post: 12-15-2007, 02:19 PM
  4. login directly in linux without username,passwor
    By nitinmhetre in forum Tech Board
    Replies: 12
    Last Post: 12-30-2006, 02:18 PM
  5. Login Program
    By tboy in forum C Programming
    Replies: 2
    Last Post: 12-03-2004, 11:56 AM

Tags for this Thread