Opening a new terminal window & executing a command - c

I've been trying to open a new terminal window from my application and execute a command on this second window as specified by the user. I've built some debugging software and I would like to execute the user's program on a separate window so my debugging output doesn't get intermixed with the programs output.
I am using fork() and exec(). The command I am executing is gnome-terminal -e 'the program to be executed'.
I have 2 questions:
Calling gnome-terminal means the user has to be running a gnome graphical environment. Is there a more cross-platform command to use (I am only interested in Linux machines though)?
After the command finishes executing the second terminal also finishes executing and closes. Is there any way to pause it, or just let it continue normal operation by waiting for input?

You probably want something like xterm -hold.

1) gnome-terminal should work reasonably also without the whole gnome environonment, anyway the old plain "xterm" is enough.
2) you can execute a short bash script that launch your program and at the end reads a line:
bash -c 'my program ... ; read a'
(or also 'xterm -e ...')

Related

what's mean “&” in the parameter of command line? [duplicate]

I am a system administrator and I have been asked to run a linux script to clean the system.
The command is this:
perl script.pl > output.log &
so this command is ending with a & sign, is there any special significance of it?
I have basic knowledge of shell but I have never seen this before.
The & makes the command run in the background.
From man bash:
If a command is terminated by the control operator &, the shell
executes the command in the background in a subshell. The shell does
not wait for the command to finish, and
the return status is 0.
When not told otherwise commands take over the foreground. You only have one "foreground" process running in a single shell session. The & symbol instructs commands to run in a background process and immediately returns to the command line for additional commands.
sh my_script.sh &
A background process will not stay alive after the shell session is closed. SIGHUP terminates all running processes. By default anyway. If your command is long-running or runs indefinitely (ie: microservice) you need to pr-pend it with nohup so it remains running after you disconnect from the session:
nohup sh my_script.sh &
EDIT: There does appear to be a gray area regarding the closing of background processes when & is used. Just be aware that the shell may close your process depending on your OS and local configurations (particularly on CENTOS/RHEL):
https://serverfault.com/a/117157.
In addition, you can use the "&" sign to run many processes through one (1) ssh connections in order to to keep minimum number of terminals. For example, I have one process that listens for messages in order to extract files, the second process listens for messages in order to upload files: Using the "&" I can run both services in one terminal, through single ssh connection to my server.
These processes running through the "&" will also "stay alive" after ssh session is closed. Pretty neat and useful if the ssh connection to the server is interrupted and no terminal multiplexer (screen, tmux, byobu) was used.
I don’t know for sure but I’m reading a book right now and what I am getting is that a program need to handle its signal ( as when I press CTRL-C). Now a program can use SIG_IGN to ignore all signals or SIG_DFL to restore the default action.
Now if you do $ command & then this process running as background process simply ignores all signals that will occur. For foreground processes these signals are not ignored.
If you have a command which executes and doesn't return status 0(control of prompt) quickly.
For example:
command gedit launches the default editor gedit UI.
commandeclipse launches eclipse IDE.
Such commands keep throwing the logs of activities in the terminal and don't return the command prompt.
Question is, how to run such commands in background so that, we will get back command terminal and we can use terminal for other tasks.
Answer is: by appending & after such command.
user#mymachine:~$ <command> &
Examples:
user#mymachine:~$ edit &
user#mymachine:~$ eclipse &

Launching program w/posix_openpt() call from script

I have a program that relies on a psuedo terminal that uses
term = posix_openpt()
grantpt(term)
unlockpt(term)
open(term)
to open a psuedo terminal. I'll call this program psuedoTerminal.bin
I want to open psuedoTerminal.bin from a bash script, in the following fashion (or similar)
#!/bin/bash
/bin/sh -c psuedoTerminal.bin &
The problem is when my program arrives to the posix_openpt() call, the behaviour is erratic, sometimes CPU consumption arrives to 100%, but it never works. I believe ssh, telnet, etc suffer from the same problems on opening psuedo terminals from inside scripts.
How can I run this program from inside a script? Thanks for your help.

How to get this click and run effect in linux?

I have a c program
#include <stdio.h>
int main ()
{
printf("Hello");
}
On Windows:
I compile that program on windows and get a.exe
Now when I double-click on a.exe
Command windows opens
a.exe is run
and automatically close that windows.
To overcome this i have two solution
1> Create a batch file with the following content:
a.exe
pause
2> or add getch() function in my code
On LINUX
Now i want the same thing to happen in linux
I have compiled that program on linux get a.out and whenever i click on that nothing happens?
I have made one shell script:
#!/bin/bash
./a.out &
pause
and run that script by clicking on it but still nothing happened?
The thing you're not seeing is that it won't open in anything at all; the program will just execute quietly in the background.
To make the output visible, you'll need to make a shell script to run the program in a terminal, which can be as simple as this:
#!/bin/bash
gnome-terminal -x "`dirname \"$0\"`"/a.out
Mark the script as executable, and now you should be able to double-click it and see your program open in a terminal window. Note that tying the program to a particular terminal emulator (like gnome-terminal) is probably a bad idea, and you should rethink why you want this behaviour.
Linux doesn't distinct between terminal and GUI applications.
What you need to do is to actually configure the program to run in a terminal, or just run a terminal and execute the program in it.

Is it possible to run a program from terminal and have it continue to run after you close the terminal?

I have written a program which I run after connecting to the box over SSH. It has some user interaction such as selecting options after being prompted, and usually I wait for the processes it carries out to finish before logging out which closes the terminal and ends the program. But now the process is quite lengthy and I don't want to wait whilst being logged in, so how could I implement a workaround for this in C please?
You can run a program in the background by following the command with "&"
wget -m www.google.com &
Or, you could use the "screen" program, that allows you to attach-deattach sessions
screen wget -m www.google.com
(PRESS CTRL+D)
screen -r (TO RE ATTACH)
http://linux.die.net/man/1/screen
The process is sent the HUP signal when the shell exits. All you have to do is install a signal handler that ignores SIGHUP.
Or just run the program using nohup.
The traditional way to do this is using the nohup(1) command:
nohup mycmd < /dev/null >& output.log &
Of course if you don't care about the output you can send it to /dev/null too, or you could take input from a file if you wanted.
Doing it this way will protect your process from a SIGHUP that would normally cause it to exit. You'll also want to redirect stdin/stdout/stderr like above, as you'll be ending your ssh session.
Syntax shown above is for bash.
you can use screen command. here is a tutorial. note you might need to install it to your systems.
There are many options :-) TIMTOWTDI… However, for your purposes, you might look into running a command-line utility such as dtach or GNU screen.
If you actually want to implement something in C, you could re-invent that wheel, but from your description of the problem, I doubt it should be necessary…
The actual C code to background a process is trivial:
//do interactive stuff...
if(fork())
exit(0);
//cool, I've been daemonized.
If you know the code will never wind up on a non-linux-or-BSD machine, you could even use daemon()
//interactive...
daemon(0, 0);
//background...

execute terminal from c program

To run a c program you do something like this
bash> gcc test.c -o test
and then
bash> ./test
How can i do to make test.c execute a terminal in another window??
xterm -e "./test"
This will execute 'test' in a new xterm window. Assuming Linux of course.
You can fork a new process and use system() function. This will work on most of Linux distributions. Just check the terminal properties to know the command to execute a new terminal. "gnome-terminal" works for me(Ubuntu, Redhat).
int main()
{
if(!fork())// child process
system("gnome-terminal");
else
{
//do rest of the things here in parent process......
}
}
After fork(), a new terminal window will open as a separate process.
Depends on which system you're on and which terminal you have in mind, but here's how to do it if you're on gnome (ubuntu for instance)
gnome-terminal -x sh -c "./test"
If you don't want the window to close immediately after ./test finishes, you do
gnome-terminal -x sh -c "./test; cat"
You want to open a window for a new terminal, or what do you want to do?
Your question isn't really clear.
If you want to run some commands, you need to cope with sys calls to launch a new process.
On Windows there's the system() function, but I'm not sure it exists on Linux or other posix systems.
Based on your use of the word terminal im guessing you are using osx.
You can use applescript to get the behavior:
tell application "Terminal"
activate
do script with command "cd _directory_; ./test"
end tell
If you want the program to launch a window, have the program call popen to launch the command [or write to a temporary file and launch the script]

Resources