简体   繁体   中英

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. I would like to launch them using a single shell script.

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.

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:

     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.

    Remark: When I fg this process, the file is generated.

  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" &

that should work.

You can use screen package. in most of Linux distributions, this package is already installed, but if not you can install it via the repository easily.

on Ubuntu and Debian

apt install screen

on CentOS and Fedora

yum install screen

To start a screen session, simply type screen in your console:

screen

You can detach from the screen session at any time by typing:

Ctrl+a d

The program running in the screen session will continue to run after you detach from the session. To find the session ID list the current running screen sessions with:

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:

screen -r 10835

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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