简体   繁体   English

使用自己的 shell 脚本启动器在 linux 中运行多个后台进程

[英]Running several background processes in linux using own shell script launcher

I would like to run several application instances in a remote Linux server which would run in a parallel way and also after logging out.我想在远程 Linux 服务器中运行多个应用程序实例,该服务器将以并行方式运行,并且在注销后也是如此。 I would like to launch them using a single shell script.我想使用单个 shell 脚本启动它们。

Let's say that I want to run the following commands in a parallel way:假设我想以并行方式运行以下命令:

matlab -nodisplay -r "matlab_test"
matlab -nodisplay -r "matlab_test2"

And let's say these processes update files named "test_file.mat" and "test_file2.mat" respectively as frequently as possible.假设这些进程尽可能频繁地分别更新名为“test_file.mat”和“test_file2.mat”的文件。

I tried several methods that I googled out, but here are the most representative ones.我尝试了几种我在谷歌上搜索的方法,但这里是最具代表性的方法。

  1. I tried the following combination, in order to use it in the future shell script:我尝试了以下组合,以便在将来的 shell 脚本中使用它:

     matlab -nodisplay -r "matlab_test" & bg 1

    Result: the output file "test_file.mat" was not generated at all, so I assume that the process was put to the background, but it was not running there.结果:根本没有生成 output 文件“test_file.mat”,所以我假设该进程被置于后台,但它没有在那里运行。

    Remark: When I fg this process, the file is generated.备注:当我fg这个过程时,文件就生成了。

  2. I also tried:我也试过:

     nohup matlab -nodisplay -r "matlab_test" &

    Result: the command prompt was occupied, which, as I assume, prevents from calling the next commands.结果:命令提示符被占用,正如我所假设的那样,它阻止了调用下一个命令。

Try nohup matlab "matlab_test" &试试nohup matlab "matlab_test" &

that should work.那应该工作。

You can use screen package.您可以使用屏幕package。 in most of Linux distributions, this package is already installed, but if not you can install it via the repository easily.在大多数 Linux 发行版中,这个 package 已经安装,但如果没有,您可以通过存储库轻松安装它。

on Ubuntu and Debian在 Ubuntu 和 Debian

apt install screen

on CentOS and Fedora在 CentOS 和 Fedora

yum install screen

To start a screen session, simply type screen in your console:要启动屏幕 session,只需在控制台中输入 screen:

screen

You can detach from the screen session at any time by typing:您可以随时通过键入以下内容从屏幕 session 分离:

Ctrl+a d

The program running in the screen session will continue to run after you detach from the session.从 session 分离后,屏幕 session 中运行的程序将继续运行。 To find the session ID list the current running screen sessions with:要查找 session ID,请列出当前正在运行的屏幕会话:

screen -ls
There are screens on:
    10835.pts-0.linuxize-desktop   (Detached)
    10366.pts-0.linuxize-desktop   (Detached)
2 Sockets in /run/screens/S-linuxize.

and If you want to restore screen session:如果要恢复屏幕 session:

screen -r 10835

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM