【问题标题】:Is there a way to run multiple bash scripts with one script at the same time?有没有办法用一个脚本同时运行多个 bash 脚本?
【发布时间】:2019-06-01 21:12:15
【问题描述】:

我正在使用 python 和 selenium 运行 Instagram 机器人。我使用 bash 脚本运行带有帐户凭据(用户名、密码、主题标签等)的 python 脚本。我运行了多个 Instagram,因此我制作了该文件的多个副本。有没有办法把它放在一个我可以点击并运行的文件中? 要打开多个运行其分配帐户的终端?

我已经尝试将它们添加到一个大文件中,但脚本在前一个完成之前不会运行。

此外,由于我使用的是 selenium,因此在 python 中尝试多线程有些困难,但如果有人能指出我可以从哪里开始,我不介意走那条路。

#!/bin/sh
cd PycharmProjects/InstaBot
python3 W.py

【问题讨论】:

标签: python linux bash ubuntu terminal


【解决方案1】:

我强烈建议大家阅读Bash Job Control

如果你的瓶颈与 CPU 无关,那么进入多线程是荒谬的。

for script in PycharmProjects/InstaBot/*.py; do
  python3 "$script" &
done
jobs

【讨论】:

    【解决方案2】:

    shell 中只有一个进程可以在前台模式下运行。因此,该命令仅在前一个完成时才会执行。 在命令行末尾添加“&”符号告诉 shell 该命令应该在后台执行。这样,shell 将启动 python 并继续运行而无需等待。

    这将同时执行两个实例,但它们都会输出到同一个终端:

        #!/bin/sh
        cd PycharmProjects/InstaBot
        python3 W.py first_credentials &
        python3 W.py second_credentials &
    

    您可以使用相同的技术为每个 python 脚本启动一个新的终端进程:

        #!/bin/sh
        cd PycharmProjects/InstaBot
        gnome-terminal   -e "python3 W.py first_credentials"  &
        gnome-terminal   -e "python3 W.py second_credentials" &
    

    【讨论】:

      猜你喜欢
      • 2023-01-13
      • 2017-12-12
      • 2012-06-22
      • 2022-01-08
      • 1970-01-01
      • 2011-09-18
      • 2015-02-16
      • 2016-08-11
      • 1970-01-01
      相关资源
      最近更新 更多