Zhangchangchang
In some cases, it is often necessary to start multiple terminals at the same time and let the terminal run its own initiative to run the corresponding command, thereby improving operational efficiency. Under Linux gnome-terminal start the terminal command,
Gnome-terminal-x followed by the command run, Bash is the command of the Linux boot child shell, bash-c "command string to run", by nesting gnome-terminal-x bash-c ..., which means to start a new terminal,
and run Bash-c in the newly launched terminal, that is, start the child shell, run the string of the double-quoted strings behind the-C by the sub-shell. At the same time to ensure that the Gnome-terminal City terminal does not exit, to Bash-c after the command string
Add bash let it have a process in the execution, in this case add the Erl command, directly into the Erl terminal, of course, will not exit.
C language version:
#include <unistd.h>
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
int main (int argc,char * argv[])
{
int count = atoi (argv[1]), I;
For (i=0;i<count;i++)
{
pid_t pid = fork ();
if (pid = = 0 )
{
//printf ("child=%d", Getpid ());
System ("Gnome-terminal-x bash-c ' ulimit-n 1000000;cd/home;erl '");
exit (0);
}
}
exit (0);
}
Run ./a.out 3
The program uses asynchronous multi-process mode, fork sub-process, through the system (.... System calls run the corresponding command, the program should note that (1) The main function how to take the number of arguments, with the number of arguments from argv[1] started, argv[0] The default parameter is "0", in fact argv array length is the number of external parameters plus 1, (2) fork out of the child process, The For loop is then run, so to avoid this behavior, exit (0) causes the child process to exit, starts the next subprocess, and (3) the fork return value is 0 in the child process, the process number of the child process that is generated in the parent process ; (4) C language atoi () can convert a string to an integer type.
Bash Shell Edition
#!/bin/bashfor ((i=0;i<$1;i++)) do ((port=2+i)) gnome-terminal-x bash-c "cd/home;ulimit-n 1000000;erl +K true-run io F Ormat $port "&done
The script is lighter and more flexible, note that & is switched to the background to prevent the next execution of the for of the current process, so the shell is a multi-process model, and the JS script is a single process model.
Reproduced in the source.
Linux commands and programs to start multiple terminals by command