Start multiple commands to execute corresponding commands and programs under linux

  
        

Author: Zhangchang Chang

In some cases, often you need to launch multiple terminals and allow the terminal to perform automatic execution of commands, so as to achieve the purpose of improving operational efficiency. In linux gnome-terminal start terminal command,

gnome-terminal -x followed by the executed command, bash is the linux boot subshell command, bash -c " the command string to be executed ", by embedding Set gnome-terminal -x bash -c ......, means to start a new terminal,

and execute bash -c in the newly started terminal, that is, start the subshell, and execute it by the subshell -c The command string enclosed in double quotes. At the same time to ensure that the gnome-terminal newly opened terminal does not exit, after the bash-c command string

add bash to let it have a process running, in this case add the erl command, directly into erl The terminal, of course, will not quit.

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); }

Execute ./a.out 3 This program uses asynchronous multi-process mode, fork sub-process, through system (..) system call execution corresponding The command, the program needs to pay attention to (1) how the main function takes parameters, the parameters are from argv[1], the default parameter of argv[0] is “0”, actually the length of the argv array Is the number of external parameters plus 1; (2) fork out of the child process, will still execute the for loop, so to avoid this phenomenon, after the fork child process is executed, exit (0) let the child process exit, start the next child Process; (3) fork return value is 0 in the child process, in the parent process Process number generated child process; atoi (4) c language () can be converted to integer strings.

Bash shell version
#!/bin/bash for((i=0;i<$1;i++)) do ((port=2+i)) gnome-terminal -x bash -c "cd /home;ulimit -n 1000000;erl +K true -run io format $port" & done

The script is more lightweight and flexible, note that & is switched to run in the background to prevent blocking the current process for The next execution, it can be seen that the shell is also a multi-process model, and the js script is a single-process model.
Reprinted to indicate the source.

Copyright © Windows knowledge All Rights Reserved