How to run commands automatically on gnome-terminal after log-in?

After each login, there’s certain commands that I run on specific tabs of gnome-terminal. This is a tedious process, so can this be done automatically?

Asked By: tshepang

||

Yes, there is a way. You need to tell gnome-terminal to launch tabs with certain profiles; these profiles must be setup to start a shell with the commands you want.

First, you need to make a script (or a launcher icon) that will start gnome-terminal --tab-with-profile=Dev. “Dev” is the name of a profile you will create, so replace that with whatever you want it to be. Also, you can specify as many --tab-with-profiles as you want: it will open a tab for each.

Now, you need the profile you just referenced. This is created by opening gnome-terminal, and finding Edit->Profiles... in the menu. Make a new profile and give it the name you specified in the previous step. Next, you need to set its preferences. Highlight the newly created profile and click the Edit button. When the Profile Preferences dialog is up, activate the “Title and Command” tab, check “Run a custom command…” and in the associated textbox, put sh -c "ENV=$HOME/.dev_profile sh". Of course, you can set ENV to any path you want, as long as you are consistent in the next step. This starts sh, and sh will execute whatever commands are in $HOME/.dev_profile

Next, you need to create that shell profile file. So edit $HOME/.dev_profile (or whatever file you specified in the previous step). Place whatever commands you want in there; they will be executed when the shell is started. Treat this like you would a .bashrc – this will replace it. Depending on how your .bashrc is setup, you may want to source $HOME/.bashrc in the profile to copy all the functionality over from your normal sh profile.

Answered By: Shawn J. Goff

You can start multiple commands on the same gnome-terminal command line by specifying the --tab-with-profile option multiple times, followed each time by a single -e specifying what command to run in that tab. You can also use --window-with-profile to have multiple windows. For example, the following command starts two windows with two tabs each; the first window runs bash in each tab, setting the environment variable TAB to 1 or 2; the second window runs htop in one tab and iotop in the other tab. The explicit sh invocation, with correct quoting, is necessary for some reason.

gnome-terminal --tab-with-profile="Default" -e 'sh -c '''export TAB=1; exec bash'' 
               --tab-with-profile="Default" -e 'sh -c '''export TAB=2; exec bash'' 
               --window-with-profile="Default" -e 'htop' 
               --tab-with-profile="Default" -e 'iotop'

If you want a command to run when you log in, put it in a shell script (for example ~/bin/my_gnome_login_commands, and register it in “System / Preferences / Startup Applications” in the Gnome menu. Alternatively, create a file ~/.config/autostart/my_commands.desktop containing

[Desktop Entry]
Type=Application
Exec=/home/tshepang/bin/my_gnome_login_commands
Hidden=false
NoDisplay=false
X-GNOME-Autostart-enabled=true

(You must use the full path to your home directory on the Exec= line, you can’t use ~.)

(This answer has been tested with Gnome 2.30 on Ubuntu 10.04. As Gnome sometimes breaks compatibility, it may or may not apply to other versions.)

I am seeing here a lot of stuffs. Probably you do not need anything of this.

These are the steps that I did in my Oracle 5.9 Linux:

  1. Create a gnome-terminal icon in your Desktop.
  2. Open your gnome-terminal.
  3. Go to Edit -> Profiles.
  4. Select your Default profile and click on Edit.
  5. Go into “Title and Command” tab.
  6. Select “Run command as login shell” option.
  7. Click on Close button.

It worked for me. Of course, in my case, I just want to run .profile to setup my terminal environment.

Answered By: CesarDBA
Categories: Answers Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.