LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   keeping xterm open? (https://www.linuxquestions.org/questions/linux-newbie-8/keeping-xterm-open-707053/)

bergman 02-24-2009 03:09 AM

keeping xterm open?
 
hello,

I have a commandline based program which I run in an xterm, called from a fluxbox menu (so I dont manually open the xterm myself).

Code:

xterm -e program
The problem is that the program needs to be run twice with different options, but the xterm closes after each program and option completes, and it has to open a new xterm to execute the next command.

Is there a way to keep an xterm open so that a program can run multiple commandlines in that one xterm instance?

like this:

Code:

1. call xterm
2. execute 'program -option 1'.
3. execute 'program -option 2' (in same xterm instance.)
4. keep xterm open


alan_ri 02-24-2009 07:17 AM

Is this a bash script or what? What is this "program" supposed to do and how? It would be nice if we could see the source.

bergman 02-24-2009 10:40 AM

Hello Alan,

The program is 'rkhunter'.

http://rkhunter.sourceforge.net/

It is a program for detecting rootkits, and run's in console or terminal. I would like to run it in an Xterm.

There two commands I need to run together:

Code:

rkhunter --update
secondly:

Code:

rkhunter -c

I would like to run these two commands in the same instance of Xterm.

the problem is that I call xterm from another program (fluxbox menu), and it opens xterm and executes the first of the commands, but then once that has finished, the Xterm closes, so I am unable to run the second command (in the same Xterm window).

catkin 02-24-2009 11:46 AM

Hello Bergman :)

I've been looking for a way to do what you want to do for some time; it may not be possible (why not? IIRC it was possible on HP-UX and Solaris).

You could write a script to do steps 2 and 3 and leave the terminal open while the user reads the output. The reader would unfortunatel not then ba able to do anything except close the terminal. You could call the script from the fluxbox menu with xterm -e myscript

The script could be something like (not tested -- just to give the idea)

Code:

#! /bin/bash
rkhunter --update
rkhunter -c
read

Best

Charles

alan_ri 02-24-2009 11:47 AM

This is just a quick answer since I don't use xterm,rkhunter and Fluxbox but you can see if it works.Why don't you make "one command" out of this two that you now have;
Code:

rkhunter --update && rkhunter -c

bergman 02-24-2009 01:49 PM

Quote:

Originally Posted by catkin (Post 3455829)
Hello Bergman :)

I've been looking for a way to do what you want to do for some time; it may not be possible (why not? IIRC it was possible on HP-UX and Solaris).

You could write a script to do steps 2 and 3 and leave the terminal open while the user reads the output. The reader would unfortunatel not then ba able to do anything except close the terminal. You could call the script from the fluxbox menu with xterm -e myscript

The script could be something like (not tested -- just to give the idea)

Code:

#! /bin/bash
rkhunter --update
rkhunter -c
read

Best

Charles

Thankyou Charles, the script worked.

I didnt think of passing a script to xterm :)

Code:

xterm -e rkhunter --update && rkhunter -c
unfortunately the above command doesnt work.

it runs 'rkhunter --update' in an xterm, but when the update has finished, xterm closes. I think that the 'rkhunter -c' part is being executed because my cpu is at 100%, but its in the background and Im unable to view it.

GazL 02-24-2009 04:56 PM

You could put your rkhunter commands in a rc file,
Code:

cat > ~/.rkhunter.bashrc <<_EOF
[ -f ~/.bashrc ] && source ~/.bashrc
rkhunter --update
rkhunter -c
_EOF

And then call it with something along the lines of this,
Code:

xterm  -e "/usr/bin/bash --rcfile ~/.rkhunter.bashrc"
Unlike catkin's example above which uses the read to delay the xterm closing, this leaves you with a fully operational bash session to work in.

Don't forget to source your existing ~/.bashrc into your rkhunter.bashrc file if you have one as the --rcfile option will mean it won't get run otherwise.

alan_ri 02-24-2009 07:02 PM

Actually,what I had in mind was something like;
Code:

xterm -e "bash /path/to_the/script"
and that's something close to what GazL said and he is right.

bergman 02-25-2009 05:14 AM

thanks :)

I didnt consider the possibility that I may need the prompt back at some point.

Could you explain what this part of the script does? I notice that when I look into the script after creation, this part is missing?

Code:

cat > ~/.rkhunter.bashrc <<_EOF

GazL 02-25-2009 06:35 AM

Quote:

Originally Posted by alan_ri (Post 3456256)
Actually,what I had in mind was something like;
Code:

xterm -e "bash /path/to_the/script"
and that's something close to what GazL said and he is right.

That will only work if the script doesn't end alan, the bash process will exit when it hits EOF on the script. it's actually little different than,
Code:

xterm -e "/path/to_the/script"
You can actually use multiple commands within the -e option as xterm just passes that string to the shell, so any of the following may do what bergman is looking for.

Code:


xterm -e " command1 ; command2 ; read -p 'Press enter to close window'"

xterm -e " command1 ; command2 ; sleep 3650d "

xterm -e " ( command1 ; command2 ) 2>&1 | tail -f "

xterm -e " command1 ; command2 ; bash"

I'm not sure off hand how to make the sleep in the second example be infinite so I just used 3650days, just use a value that's going to be long enough for you.

The final example is very similar to what happens with my --rcfile suggestion, except the shell you get dumped into on completion won't be the same one that your commands ran in. It'll be a sub process.

GazL 02-25-2009 06:43 AM

Quote:

Originally Posted by bergman (Post 3456685)
thanks :)

I didnt consider the possibility that I may need the prompt back at some point.

Could you explain what this part of the script does? I notice that when I look into the script after creation, this part is missing?

Code:

cat > ~/.rkhunter.bashrc <<_EOF

Ah sorry, that's not actually part of the script, it was me trying to be helpful. hehe. If you cut and paste those commands directly into your terminal it'll create that .rkhunter.bashrc file for you.

It's called a 'Here Document'
Basically, what it's saying is:

run cat redirecting the output (>) to ~/.rkhunter.bashrc
taking the input from the following lines until you find the characters _EOF (<<_EOF)

If you look at man bash and search for "Here Documents" it'll explain it all in gory detail. They're a very useful tool at times.

bergman 02-25-2009 08:18 AM

oh I see.

Thanks again, Ive learned some interesting things from this thread :)

catkin 02-25-2009 09:43 AM

Quote:

Originally Posted by GazL (Post 3456129)
You could put your rkhunter commands in a rc file,
Code:

cat > ~/.rkhunter.bashrc <<_EOF
[ -f ~/.bashrc ] && source ~/.bashrc
rkhunter --update
rkhunter -c
_EOF

And then call it with something along the lines of this,
Code:

xterm  -e "/usr/bin/bash --rcfile ~/.rkhunter.bashrc"
[snip]

A thousand "thank you"s GazL :)

I've been looking for a way to achieve that functionality for ages (and not understanding why the graphical terminal programs don't facilitate it).

Adapting your technique for GNOME on ubuntu 8 only required changing xterm to gnome-terminal and /usr/bin/bash to /bin/bash.

Sweet! Best

Charles

alan_ri 02-25-2009 11:16 AM

Quote:

Originally Posted by GazL (Post 3456765)
That will only work if the script doesn't end alan, the bash process will exit when it hits EOF on the script. it's actually little different than,
Code:

xterm -e "/path/to_the/script"

You have to know that I had a ~/.bashrc file in mind.One can use 15 bashrc files instead the default one and load a new version of Bash in the current terminal whenever one wants and xterm will not close when the script is executed.The script doesn't have to end with _EOF and I didn't have your script in mind.My script would look a little different if I needed to make one.Since you mentioned it;will you explain where you see the difference between;
Code:

xterm -e " command1 ; command2 ; bash"
and
Code:

xterm -e "bash /path/to_the/script"
if you have in mind that script is made only from command 1 and command 2.

...and yes,
Code:

xterm -e "/path/to_the/script"
is very different then
Code:

xterm -e "bash /path/to_the/script"

GazL 02-25-2009 01:06 PM

Quote:

Originally Posted by alan_ri (Post 3457146)
Since you mentioned it;will you explain where you see the difference between;
Code:

xterm -e " command1 ; command2 ; bash"
and
Code:

xterm -e "bash /path/to_the/script"

In the first example you're passing 3 commands to xterm. the third of which is another instance of bash which will keep the xterm open.
In the second you're passing 1 command to xterm which is bash and providing bash with an input script file to run. When bash gets to the end-of-file in that input file it will exit, which in turn will cause the xterm to exit. That doesn't happen in the first example as the final bash doesn't receive and end-of-file.

In the context of what the original poster was trying to achieve (i.e. open a xterm and run some commands in it but leave it open) the difference is clear. one will exit on completion and the other remains open.

Having said that, it's still a bit of a hack, and using the --rcfile parameter could probably be considered more correct.


All times are GMT -5. The time now is 10:06 AM.