并行或串行运行命令

并行或串行运行命令

如果我样要启动两个服务, 并且两个服务并没有相关性, 那么这时候最好是使用并行.

一般情况会想到以下命令:

serve1 & serve2

但是这个命令在 widnows 上还是会顺序执行, 必须 serve1 运行结束后才会执行 serve2, 但是由于它是一个服务, 是不会结束的!

方案

concurrently

  • 不能直接运行其他命令
  • 不能支持串行
> node -v
v14.15.5

> npx concurrently "node -v"  
6.2.1

> npx concurrently --raw "echo 123"
TypeError: commandInfo.command.match is not a function

tasksfile

  • 需要创建配置文件
  • 中文输出会乱码 #119
> chcp 936 && node tasksfile.js command
活动代码页: 936
ping baidu.com -t
���

���� Ping baidu.com [220.181.38.148] ���� 32 �ֽڵ����:
4�� 220.181.38.148 �Ļظ�: �ֽ�=32 ʱ��=48ms TTL=51
4�� 220.181.38.148 �Ļظ�: �ֽ�=32 ʱ��=47ms TTL=51

npm-run-all

  • 只支持 npm 命令, 不能直接运行其他命令 #203

parallelshell

  • 无法运行
> npx parallelshell "echo 1" "echo 2" "echo 3"
child_process.js:435
    throw new ERR_INVALID_ARG_TYPE('options.cwd', 'string', options.cwd);

结果

使用 qs 既可以运行 npm 命令, 也可以运行系统命令.
例如:

> qs -p "ping baidu.com" "ping qq.com"

-p 表示并行运行

如果需要把命令保存起来下次使用, 可以这样:

> qs -n t1 -p "ping baidu.com" "ping qq.com"

以后就可以任何目录使用 qs -s t1 来使用这个命令了.

参考

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章