Home > command line > How to run a command at login?

How to run a command at login?

March 19Hits:1
Advertisement

This question already has an answer here:

  • How to run scripts on start up? 8 answers
  • How do I start applications automatically on login? 14 answers

I need to run the following command (with real parameters) after login:

ssh -f [email protected] -L 54321:example.com:54321 -N 

I need it only for my user, not all users on the machine. Also, I need it to run whether I login via the GUI or via SSH to a plain shell terminal. I would even like to know how to do this on machines without a GUI installed.

Where would I put the above line to be run as explained?

Answers

Adding the command to the end of ~/.profile should do the trick (where ~ represents your home directory).

Not ~/.bashrc, which would make it run again every time a bash shell is started. In contrast, .profile is only run by interactive login shells. See man bash and man sh for more information.

Please note that this will run not just for GUI and SSH logins, but for all logins (or at least all interactive logins), including when you log on in a virtual console.

Related Articles

  • How can I run a command at login?December 12

    I would like to run a command at login on my Fedora 19 GNU/Linux system. What is the best way to do this? Note: i am using the GUI. Thus, I could not simply place the commands in my .zshrc file. They are already in this file, but this file is not exe

  • Ssh, run a command on login, and then Stay Logged In?October 29

    I tried this with expect, but it didn't work: it closed the connection at the end. Can we run a script via ssh which will log into remote machines, run a command, and not disconnect? So ssh in a machine, cd to such and such a directory, and then run

  • How to Automatically run two commands after login?

    How to Automatically run two commands after login? February 26

    This question already has an answer here: How do I start applications automatically on login? 14 answers I have these two commands that I need to manually run every time after login: autossh -M 2000 -N -f -q -D 127.0.0.1:7070 [email protected] and sudo

  • run xmodmap commands after loginApril 19

    I created a file ~/.Xmodmap with my mapping commands. I thought that it will be automatically launched after login (and restart) but it's not. So I have to always run after login the command $ xmodmap .Xmodmap Is it possible to automatize this proces

  • How to run a command at login? March 19

    This question already has an answer here: How to run scripts on start up? 8 answers How do I start applications automatically on login? 14 answers I need to run the following command (with real parameters) after login: ssh -f [email protected] -L 5432

  • putty run remote commandFebruary 12

    I config putty to run remote command after login. cd /workDirectory/; /bin/bash; However, after doing this, I cannot use git anymore, complaints: git: Command not found. If I just log on without executing the command, git works just fine. (I am conne

  • Run a command on Windows startup (not login!)May 4

    How do you tell Windows to run a command on each system startup? I have a large number of servers on which I will need to run a command on system startup that makes a non-persistent configuration change. I can run the command either: as an executable

  • Running terminal commands on startup/loginDecember 20

    How can I run terminal commands on startup or login? On FreeBSD I believe you can do this using rc.d - is there an equivalent for the Mac? I know how to set up Login Items, but these only work for GUI applications, and there must be an elegant way to

  • How to run a command file in PuTTY using automatic login in a command prompt?December 6

    I am using the following to login automatically to a remote server and then run commands listed in a commands.txt, like this: C:\path\to\putty.exe -load "[Sessionname]" -l [user] -pw [password] -m C:\path\to\commands.txt commands.txt contains th

  • How to run a command as a user whose login is disabled?January 15

    I am trying to use the su command to run an application as another user In this case I am trying to run irssi [email protected]: su - [username] irssi (enter password) [email protected]: (nothing happens) [email protected]: su - [username] -c irssi (nothing) I run gks

  • PuTTY: Run a remote command after login & keep the shell runningApril 25

    What I'm trying to do: start a PuTTY session from the command line, login to remote machine and cd to provided directory. putty.exe -agent -ssh some.host That will open a session & login with my default login name & private key. echo cd /some/remo

  • A robust way to execute a command/run a script at login?

    A robust way to execute a command/run a script at login?August 8

    I have two commands that change keyboard behaviour: xset r rate 120 66 /usr/bin/setxkbmap -option "ctrl:nocaps" When I open a terminal and execute them, they change auto-repeat settings and remap CAPS LOCK to control, not only in the terminal wi

  • Single command to login to SSH and run program?March 16

    Is there a way to structure a single command to login via SSH to a remote server and run a program on the remote login shell? In the OpenSSH manual, it reads "If command is specified, it is executed on the remote host instead of a login shell."

  • Run command at login - Ubuntu 15.04May 17

    This has been asked to death, and yet none of the solutions I've found seem to work. I want to run a command, say setxkbmap -layout us -option ctrl:nocaps , at login. It should only run for me, and not anybody who logs in. ~/.profile doesn't work. ~/

  • Login to multiple servers and run a command having passphraseOctober 6

    Already passphrase has been setup and now I want to login to multiple servers and run a command against all of the servers. I tried this #! /bin/bash for HOST in $(cat servers.txt ) ; do ssh -o $HOST "uname -a" PreferredAuthentications=keyboard-

  • How can I run a shell command at login time without showing a Dock entry? February 9

    Possible Duplicate: How to run something automatically when the system boots or a use logs on? I need to automatically run a command when I log in but I don't want to add an item to the Dock. How can I do this? --------------Solutions------------- If

  • How can I use ssh to run a command on a remote Unix machine and exit before the command completes?July 19

    How can I use ssh to run a command on a Unix machine and exit before the command completes? For instance, if I type ssh localhost 'sleep 60 &' ssh hangs on my terminal until sleep completes, even though sleep is run in the background. --------------S

  • Updating the $PATH for running an command through SSH with LDAP user accountMay 7

    I am setting up a Mac OSX 1.6 server to host Git repositories. As such we need to push commits to the server through SSH. The server has only an admin account and uses a user list from a LDAP server. Now, since it is accessing the server through a no

  • How can I automatically run a command when I log out?October 5

    I'd like to make a command run every time I log out. Is there a way to do this? --------------Solutions------------- If you are speaking of logging out of a bash login shell, there's a solution with ~/.bash_logout - Run man bash in the terminal for m

  • How do I run a command as the system administrator (root)October 13

    I need to run a command with administrative privileges. Someone said I should run a command as root. How do I do this? --------------Solutions------------- The main two commandline possibilities are: Use su and enter the root password when prompted.

Copyright (C) 2017 ceus-now.com, All Rights Reserved. webmaster#ceus-now.com 14 q. 0.593 s.