Open gnome terminal programmatically and execute commands after bashrc was executed

可紊 提交于 2019-12-12 07:12:15

问题


I try to build a little script to start my development environment. For that task I try to open a gnome terminal with several tabs where automatically the rails server and autotest is started. But

gnome-terminal --tab -e "rails server" --tab --tab

does not work ("error creating the child process"). Also

gnome-terminal --tab -e "bash -c \"rails server\"" --tab --tab` 

does not work. Any suggestions how to solve that problem?


回答1:


Here is a nice trick we worked out at Superuser

  1. Add a eval "$BASH_POST_RC" to the end of your .bashrc

  2. Set the BASH_POST_RC environment variable for each tab to that command you like to execute, e.g.: gnome-terminal --working-directory="/home/zardoz/projects/my_rails_app" --tab -e 'bash -c "export BASH_POST_RC=\"rails server\"; exec bash"' --tab -e 'bash -c "export BASH_POST_RC=\"autotest\"; exec bash"'

@Gilles: Thanks for that solution!




回答2:


Stab in the dark: create shell scripts for each command you want to run in a tab, make them executable, and invoke them by absolute path, e.g. put this in /home/zardoz/bin/railsstart

#! /bin/sh
exec rails server

chmod +x it, and then do

gnome-terminal --tab -e /home/zardoz/bin/railsstart --tab --tab ...

If that doesn't work, the next thing I would try is sticking strace -f -o /tmp/trace.log on the beginning of the command, letting it fail, and then digging through trace.log to find out which system call actually failed and why (there'll be a tremendous amount of junk in there - read from the end backward and look for all-capitalized code phrases starting with E, like "ENOEXEC", "ENOENT", "EPERM", sort of thing.)

EDIT: Here's how you pull in all the .bashrc settings in one of these scripts:

#! /bin/bash
. ~/.bashrc
exec rails server

Caution: you may need to adjust your .bashrc so that it doesn't do certain things that only work in a "real" interactive shell session. Don't worry about this unless you get strange errors before rails starts.




回答3:


I'm assuming the error arises because PATH is not set at the time gnome-terminal tries to run rails.

Why not use the full path to the rails server, or create a script that sets the PATH variable?




回答4:


Already replied, but just in case, check out this gem that automates the terminal on KDE, OSX and Gnome desktops.




回答5:


for ubuntu 16.04

press Ctr+Shift+T

this will open a new tab in the same window. additionally a button for adding more tabs will appear next to the right most tab.



来源:https://stackoverflow.com/questions/3896882/open-gnome-terminal-programmatically-and-execute-commands-after-bashrc-was-execu

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!