Linux start terminal with command

Open terminal on startup and run command [duplicate]

When I turn my laptop on the command runs in the background, and this is great, but I can’t issue a «Stop» command because the terminal never opens. So, my question is this; How do I force the terminal to open, run the script and then stay open? Do I need to add a line to the .sh file, or add arguments to the startup command?

Just commenting, that there’s a stackexchange community dedicated to Elementary OS here: elementaryos.stackexchange.com

1 Answer 1

If you are using older GNOME you can add to startup something like this:

Just edit your startup with gnome-session-properties GUI. It should be in System -> Properties menu or Applications –> Other –> Advanced Settings or press Alt + F2 and input it there. But apparently it was deprecated and it will not work on GNOME 3.

You can try adding into your folder ~/.config/autostart a file, let’s say myscript.desktop with following content:

[Desktop Entry] Type=Application Exec=gnome-terminal --command "/path/myscript.sh" Hidden=false X-GNOME-Autostart-enabled=true Name=Startup Script Comment= 

Above content is actually from my own system, created by mate-session-properties GUI and it works. Only updated it for GNOME. It actually might work on other environments, you’ll just need to play with some details. Adding Terminal=true with Exec=/path/myscript.sh also works and with it you don’t need to know the command to start your terminal.

On MATE it’s easier, because it’s still got that GUI. Commands are called mate-terminal and mate-session-properties . Here’s how it looks on my MATE:

enter image description here

I’m not familiar with Elementary OS and you didn’t say which desktop environment you are using (might be Pantheon which I’ve never worked on), but it should not be that different. You might need to figure out command to start your terminal and try running my-terminal —help or visit it’s manpages to find proper parameter (or just try that much simpler Terminal=true solution).

And if for some reason Terminal=true doesn’t work and you can’t determine your terminal’s commands, install XTerm:

Its command is xterm -e «/path/myscript.sh .

Источник

Open terminal on start and pass a command

I have a perl script that runs from the terminal on Linux. I would like to be able to boot up, and when my desktop environment starts it will automatically open my terminal, and pass in the arguments to star this program, which are ./ttytter.txt -ansi -mentions -vcheck . How can I go about doing this?

2 Answers 2

Sometimes we do need to run programs in a terminal, can do like this:

xterm -e "sh -c './ttytter.txt -ansi -mentions -vcheck ; read" 

The final «read» waits for you to press enter, to close the terminal.

Читайте также:  Linux http 500 internal server error

You can use a different terminal program, and instead of «read» at the end you can run a shell, e.g.:

gnome-terminal -e "sh -c './ttytter.txt -ansi -mentions -vcheck ; exec bash'" 

For some terminals (xterm) you might not need the sh -c » wrapper; for some (gnome-terminal) we apparently do need that.

You can put such a command in .xinitrc, .xsession, or type it into a «run at startup» dialog.

You can also I think do this with a freedesktop .desktop file / shortcut, if you select «run in terminal», not sure how off the top of my head. That would be a much user friendly way to do it! See if you can figure that out.

Now, if you want to do it in general for any shell command, need to worry about shell escaping — and it becomes more difficult! Or, can put your command into a script and run the script.

It might also be helpful to see the command’s exit status, like echo $? where 0 means success.

Sorry for over-complex answer, I hope it can help.

Источник

How to start a terminal with certain text already input on the command-line?

Rather than rephrasing my question, let me describe to you the desired user-case: I create a short shell-script to run command «gnome-terminal —someoptionflagname ‘my text to be posted'», and execute this script. Gnome-terminal pops up, with command-line prompt followed by my text. ie: fields@mycomputer:/$ my text to be posted Can this be done?

7 Answers 7

You can do this with expect (install). Create and make executable ~/bin/myprompt :

#!/usr/bin/expect -f # Get a Bash shell spawn -noecho bash # Wait for a prompt expect "$ " # Type something send "my text to be posted" # Hand over control to the user interact exit 

and run Gnome Terminal with:

gnome-terminal -e ~/bin/myprompt 

This is cool. I was hoping there would be some simple(ish) answer based on default gnome-terminal functionality, but looks like this answers the job.

This one has been bugging me so long, I almost couldn’t remember what the intended use-case was. So, for clarification: I would want to do something like «gnome-terminal -e ls», but the catch is to have the window remain open. So, basically shortcuts to opening a terminal and running shell commands. Does this clarify issues vs aendruk/msw’s answers?

Super, thanks. I’ve successfully integrated this into a shell script that call calls Pentaho data integration tool. Then I’ve added the call to this script to Unity launcher in 11.10, and it works like a charm.

ændrük’s suggestion is very good and worked for me, however the command is hardcoded within the script, and if you resize the terminal window it doesn’t work well. Using his code as the base, I’ve added the ability to send the myprompt script the command as an argument, and this script correctly handles resizing the terminal window.

#!/usr/bin/expect #trap sigwinch and pass it to the child we spawned #this allows the gnome-terminal window to be resized trap < set rows [stty rows] set cols [stty columns] stty rows $rows columns $cols < $spawn_out(slave,name) >WINCH set arg1 [lindex $argv 0] # Get a Bash shell spawn -noecho bash # Wait for a prompt expect "$ " # Type something send $arg1 # Hand over control to the user interact exit 

and run Gnome Terminal with:

gnome-terminal -e "~/bin/myprompt \"my text to be posted\"" 

If I understand correctly, you want your first input line to be prefilled to contents that you pass on the gnome-terminal command line.

Читайте также:  Linux mint mate настройка

I don’t know how to do exactly this with bash, but here’s something that comes close. In your ~/.bashrc , add the following line at the very end:

history -s "$BASH_INITIAL_COMMAND" 

Run gnome-terminal -x env BASH_INITIAL_COMMAND=’my text to be posted’ bash , and press Up at the prompt to recall the text.

Note also that if you put set -o history followed by comments at the end of your .bashrc , they will be entered into the history as bash starts, so you can use them as a basis for editing by reaching them with the Up key and removing the initial # .

ændrük’s answer is fine, but perhaps a little heavyweight for the task.

Here is a script that writes a script based on its arguments

#!/bin/sh # terminal-plus-command: start a subordinate terminal which runs # the interactive shell after first running the command arguments tmpscript=/tmp/tmpscript.$$ echo "#!$SHELL" > $tmpscript echo "$@" >> $tmpscript echo exec "$SHELL" >> $tmpscript chmod +x $tmpscript gnome-terminal --command $tmpscript rm -f $tmpscript 

If you’ve not done much shell programming, there appears to be more magic here than there is. First, I name a temporary file for holding the script where $$ is the process ID of the shell running this script. The /tmp/something.$$ metaphor is used in case two instances of this script are run at the same time, they won’t try to use the same temporary file.

The variable $SHELL is set to the name of the shell running the script. If you use /usr/bin/bash, presumably you’d like the mini-script to use it also.

The «$@» is a shell idiom for «interpolate all my arguments, quoting them if needed». This peculiar syntax causes

script.sh 'my file' your\ file 

to interpolate the arguments as two elements

instead of the four that $@ would yield

Читайте также:  Kali linux язык программирования

The last lines of the script arrange for a gnome-terminal to start running the mini-script and then starting an interactive shell. When the gnome-terminal exits, the temporary script is removed because littering is uncool.

The last line is not a part of the mini-script, it demonstrates that the mini-script works. If the 11 line script above is in a file called rt.sh then the chmod makes it executable and then it is executed.

$ chmod +x rt.sh && ./rt.sh echo hello world 

The result of all of this will be a gnome terminal which starts up, displays

on its first line and then starts an interactive shell:

Источник

Shortcut to open Terminal and run some commands

I tried with Terminal profiles, but I didn’t find option for 2. and 4.

1 Answer 1

First you need a script like one from the following example:

#!/bin/bash commands () < cd / # change directory, in this case to `/` echo "your_password" | sudo -S mkdir -p "test" # execute a command as root for which the password is automatically filled $SHELL # keep the terminal open after the previous commands are executed > export -f commands gnome-terminal -e "bash -c 'commands'"

Then, if you are not familiar about how to execute a script using a desktop shortcut, see Execute sh script from *.desktop file?.

Thank you, but I think I need some more help. Do I type this script in terminal or make a document (with .sh ending)? Do I start with #!/bin/bash? Do I type password with or without «»? What about «test» in the echo line — it’s marked red, do i have to insert something else there? Same question with «bash -c ‘commands'»? Where do I type my actual command, that will be executed?

Use a text editor. Include ‘#/bin/bash’ — won’t work without! No password necessary — you created the file. Don’t worry about the red. You need to insert your own code where it says ‘sudo -S mkdir -p «test»‘. Read the links for how to make it executable and how to run from desktop.

I almost made it:). it executes the command, but I still have to type my root password. where in a script I put my password? I tried after echo (with or without «) and it doesn’t work.

@mo echo «your_password» — replace your_password with your password; keep quotes and your password shouldn’t contain another quotes. This command: mkdir -p «test» will create a directory called test in / if there doesn’t exist another one with the same name; it’s just an example and you can change the command as you wish.

I did that, but it doesn’t work. it starts Terminal, but it wants my sudo password. when I type it, then the command is executed.

Источник

Оцените статью
Adblock
detector