Ubuntu 下使用shell脚本同时启动多个程序
新建start.sh的脚本
#!/bin/sh
cd test1
./test1 fork &
cd ../
cd test2
./test2
脚本中,在可执行程序后添加 fork &
建立两个文件夹test1和test2,分别在下面写test1.cpp和test2.cpp的程序代码。
- test1.cpp:
#include<iostream>
#include <unistd.h>
using namespace std;
int main(){
cout<<"test1 start"<<endl;
sleep(10);
cout<<"test1 end"<<endl;
}
- test2.cpp:
#include<iostream>
#include <unistd.h>
using namespace std;
int main(){
cout<<"test2 start"<<endl;
sleep(10);
cout<<"test2 end"<<endl;
}
运行start.sh的结果:
表明是同时运行的