Im trying to execute a program with xterm and then close xterm with program running but it seems this doesn't work like I would expect it.
In bash there is the option bash -c [command] is there something in xterm too?
Currently im using xterm -e [command] but this keeps the xterm window open till' my program has finished. But I want to open it, run the program, close it and keep the program running.
Is there a way to do it?
I do it (from a bash file) like:
my#self:~$ cat /usr/bin/rdesk.sh
#!/bin/bash
xterm -e 'perl /home/my/script.pl'
when i run the rdesk.sh file it starts xterm with a perl script, giving me some options. after choosing one, I can close the window, running the chosen program.
So you might just have to put your xterm -e ... command into a bash script.
I am using mapfile -t to obtain content of a text file and assign it to array.
In Jenkins it works fine where it will prompt steps and what command executed in console output .When I try to run in local console for example putty it prompts.
mapfile: not found [No such file or directory]
I know that mapfile is a bash command is and I am able to run the shell program after typing bash and executing the script.Is there anyway that I don't need to type bash in order to run the program ?I include #!/bin/bash -x on top of the script it still display the same error .The reason I don't want to type bash and execute the script is due to that it did not show what are the errors when the script dies.It did not display the error handling process that was in the script and it did not display output when it runs the command.
Please open a new file called script in a text editor. Type your program in:
#!/bin/bash -x
set -e
item=$1
if [ $item = '-database' ] then
mapfile -t DATA < $DATA_FILES
fi
save the file, execute chmod u+x and then
./script "-database"
to run it.
That's it.
However, that script will print nothing.
I have a one liner that I run from the run box in the start menu:
"C:\Program Files (x86)\casper\PuTTY\putty.exe" -ssh "192.168.1.2" -l casper -pw "<password>" -m \\PROD.MSAD.casp.NET\UserData\CASPER\Home\Documents\pbauth_list.txt -t
It sssh to a linux box, and opens up a terminal and echos these statements intoa terminal. when someone needs elevated access at the linux terminal i just type in user=user than cut and paste the line into the command line - and viola that user has access to a super user account for what ever time period i designate. It works awesome.
The
\\PROD.MSAD.casp.NET\UserData\CASPER\Home\Documents\pbauth_list.txt text
file looks like this
echo "pbrun pbauthcl grant PBAUTH_P_A4_PROD \$user 2 \"\$user needs access\" now all
pbrun pbauthcl grant PBAUTH_P_A4_UTIL \$user 2 \"\$user needs access\" now all
printf '\e[8;50;100t'
"
Notice that the
"printf '\e[8;50;100t'"
is echoed out along with the lines into the terminal .
What this command does is automagically logs into a linux box at 192.168.1.2 and then opens up a terminal and prints the lines to the terminal.
i cut and paste the print statement "printf '\e[8;50;100t'" to resize the terminal.
When a user wants elevated access at the linux command prompt
What I do is type in user= and then cut and paste one of the pbun command and it gets executed at the linux command line - which works fine.
however it would be much cooler if the "printf '\e[8;50;100t'" would just execute and make the terminal larger instead of me having to echo it into the terminal and then cut and paste it into the same terminal to resize it.
I have tried a bunch of different permutations in the test file to get the printf statement to just run, instead of echoing it out.
bash -v echo "printf '\e[8;50;100t'"
bash ;
I get thse kinds of errors though
/usr/bin/printf: /usr/bin/printf: cannot execute binary file
/bin/echo: /bin/echo: cannot execute binary file
how do I execute the "printf '\e[8;50;100t'"
command within the terminal instead of echoing it out ? There has to be a way .
Two possibile solutions that might work for you are the following:
Try -x
Try -o verbose
I have a zsh script that I want to run such that it also loads up my .zshrc file.
I believe I have to run my script in interactive mode?
Thus, my script begins like:
#!/bin/zsh -i
if [ $# = 0 ]
then
echo "need command line paramter..."
exit
fi
However, when I try to run this script in the background, my script becomes suspended (even if I pass in the correct number of parameters):
[1] + suspended (tty output)
My question is: How can I make a script that can run in the background that also loads my startup .zshrc file? If I have to put it into interactive mode, how can I avoid the suspension on tty output problem?
Thanks
Don't use interactive mode as a hash-bang!
Instead, source your zshrc file in the script if you want it:
#!/bin/zsh
source ~/.zshrc
...
For future reference, you can use the disown bultin to detach a previously backgrounded job from the shell so it can't be suspended or anything else. The parent shell can then be closed with no affect on the process:
$ disown %1
You can do this directly from the command line when you start the program by using the &! operator instead of just &:
$ ./my_command &!
I created a bash script that opens several gnome-terminals, connect to classroom computers via ssh and run a script.
How can I avoid that the gnome-terminal closes after the script is finished? Note that I also want to be able to enter further commands in the terminal.
Here is an example of my code:
gnome-terminal -e "ssh root#<ip> cd /tmp && ls"
As I understand you want gnome-terminal to open, have it execute some commands, and then drop to the prompt so you can enter some more commands. Gnome-terminal is not designed for this use case, but there are workarounds:
Let gnome-terminal run bash and tell bash to run your commands and then start a new bash
$ gnome-terminal -- bash -c "echo foo; echo bar; exec bash"
or if the commands are in a script
$ gnome-terminal -- bash -c "./scripttorun; exec bash"
The first bash will terminate once all the commands are done. But the last command is a new bash which will then just keep running. And since something is still running gnome-terminal will not close.
Let gnome-terminal run bash with a prepared rcfile which runs your commands
Prepare somercfile:
source ~/.bashrc
echo foo
echo bar
Then run:
$ gnome-terminal -- bash --rcfile somercfile
bash will stay open after running somercfile.
i must admit i do not understand completely why --rcfile has this behaviour but it does.
Let gnome-terminal run a script which runs your commands and then drops to bash
Prepare scripttobash:
#!/bin/sh
echo foo
echo bar
exec bash
Set this file as executable.
Then run:
$ gnome-terminal -- ./scripttobash
for completeness
if you just want to be able read the output of the command and need no interactivity
go to preferences (hamburger button -> preferences)
go to profiles (standard or create a new one)
go to command tab
when command exits -> hold the terminal open
i recommend to create a new profile for just for this use case.
use the profile like this:
gnome-terminal --profile=holdopen -- ./scripttorun
Every method has it's quirks. You must choose, but choose wisely.
I like the first solution. it does not need extra files or profiles. and the command says what it does: run commands then run bash again.
All that said, since you used ssh in your example, you might want to take a look at pssh (parallel ssh). here an article: https://www.cyberciti.biz/cloud-computing/how-to-use-pssh-parallel-ssh-program-on-linux-unix/
Finally this one works for me:
gnome-terminal --working-directory=WORK_DIR -x bash -c "COMMAND; bash"
Stack Overflow answer: the terminal closes when the command run inside it has finished, so you need to write a command that doesn't terminate immediately. For example, to leave the terminal window open until you press Enter in it:
gnome-terminal -e "ssh host 'cd /tmp && ls'; read line"
Super User answer: Create a profile in which the preference “Title and Command/When command exits” is set to “Hold the terminal open”. Invoke gnome-terminal with the --window-with-profile or --tab-with-profile option to specify the terminal name.
Run with -ic instead -i to make terminal close bash proccess when you close your terminal gui:
gnome-terminal -e "bash -ic \"echo foo; echo bar; exec bash\""
As of January 2020, the -e option in gnome-terminal still runs properly but throws out the following warning:
For -e:
# Option “-e” is deprecated and might be removed in a later version
of gnome-terminal.
# Use “-- ” to terminate the options and put the command line to
execute after it.
Based on that information above, I confirmed that you can run the following two commands without receiving any warning messages:
$ gnome-terminal -- "./scripttobash"
$ gnome-terminal -- "./genericscripttobash \"echo foo\" \"echo bar\""
I hope this helps anyone else presently having this issue :)
The ideal solution would be to ask for a user input with echo "Press any key".
But if double-click in Nautis or Nemo and select run in a terminal, it doesn't seem to work.
In case of Ubuntu a shell designed for fast start-up and execution with only standard features is used, named dash I believe.
Because of this the shebang is the very first line to start with to enable proper use of bash features.
Normally this would be: #!/bin/bash or similar.
In Ubuntu I learned this should be: #!/usr/bin/env bash.
Many workarounds exist to keep hold of the screen before the interpreter sees a syntax error in a bash command.
The solution in Ubuntu that worked for me:
#!/usr/bin/env bash
your code
echo Press a key...
read -n1
For a solution applicable to any terminal, there is a script that opens a terminal, runs the command specified and gives you back the prompt in that new terminal:
https://stackoverflow.com/a/60732147/1272994
I really like the bash --rcfile method
I just source ~/.bashrc then add the commands I want to the new startrc.sh
now my automated start.sh work environment is complete... for now 😼
If running a bash script just add gedit afile to the end of the script and that will hold gnome-terminal open. "afile" could be a build log which it was in my case.
Did not try just using gedit alone but, that would properly work too.
Use nohup command.
nohup gnome-terminal -e "ssh root# cd /tmp && ls"
Hope this will help you.