如何从单个命令行窗口调用多个程序并保持它们运行
今天,海云青飞 需要从命令行调用两个程序,一个程序有输出信息
我可以开启两个命令行窗口,分别调用这两个程序,但这显然不是好方法
我并没有时间去详细研究计算机技术,这没有关系,因为现在有了 AI
先上结论,后面再放出 AI 给出的详细方案
我在 Windows 上安装了 Msys2,因此,对我来说以下命令通用于 Windows 和 Linux 系统
-
Windows 系统,让
.sh
文件关联到sh.exe
也就是,我们点击
.sh
后缀文件时,让 Msys2 中的sh.exe
打开它。这样做的效果就是,我们点击.sh
文件就会自动执行其中的命令 -
创建文件
run_two_programs.sh
里面内容如下:
#!/bin/sh cd ~/tuenhai.com/software/program2 ./good_program -c config.json & # sudo 表示申请管理员权限 sudo ~/tuenhai.com/program1 &
海云青飞 问 AI 如何同时调用二个命令行程序
海云青飞 问 ChatGPT 4o mini:
Is there a way to call two programs from the command line in Linux and keep them running? I can open two terminal windows to call them, but that's not a good idea. It would be better if I could do this in a bash script or use a small program
ChatGPT 4o mini 的回复
Yes, you can definitely run multiple programs from a single command line or script and keep them running in the background. Here are a few methods you can use:
Using &
to Run Programs in the Background
You can run two programs simultaneously by appending &
to each command. This will start each program in the background:
./program1 &
./program2 &
In this example, program1
and program2
will start running in the background. The &
symbol makes the shell run the command in the background, allowing you to continue using the terminal or run additional commands
Using nohup
to Keep Programs Running After Logout
If you want the programs to keep running even after you log out of the terminal, you can use nohup
:
nohup ./program1 > program1.log 2>&1 &
nohup ./program2 > program2.log 2>&1 &
The nohup
command stands for "no hang up," and it prevents the program from being terminated when the terminal session ends. The > program1.log 2>&1
part redirects both the standard output and error output to a log file
Using a Bash Script
You can put the above commands into a bash script file. Create a file named run_programs.sh
with the following content:
#!/bin/bash
# Run programs in the background
./program1 &
./program2 &
Make the script executable:
chmod +x run_programs.sh
Then run your script:
./run_programs.sh
Using tmux
or screen
If you want to run programs in separate sessions within the same terminal window, you can use tmux
or screen
With tmux
:
-
Start a new tmux session:
tmux
-
Split the terminal into two panes (Ctrl-b, then % for vertical split or " for horizontal split)
-
Run your programs in each pane
-
Detach from the tmux session (Ctrl-b, then d). The programs will continue running
With screen
:
-
Start a new screen session:
screen
-
Start your first program
-
Create a new window (Ctrl-a, then c) and start your second program
-
Detach from the screen session (Ctrl-a, then d). The programs will continue running
Using systemd
Service (Advanced)
If you need the programs to start automatically on boot or as a service, you can create systemd
service files. This is more advanced and useful for long-running services
-
Create a service file in
/etc/systemd/system/
likemyprogram1.service
:[Unit] Description=My Program 1 [Service] ExecStart=/path/to/program1 Restart=always [Install] WantedBy=multi-user.target
-
Enable and start the service:
sudo systemctl enable myprogram1.service sudo systemctl start myprogram1.service
Repeat for other programs as needed
Choose the method that best fits your needs! If you need further help, feel free to ask
相关内容
- WinSW: wraps and manages any application as a Windows service
- Run as administrator
- NSSM - the Non-Sucking Service Manager
- Windows Service Manager (SrvMan)
- go-serviceman: Cross-platform service management for Mac, Linux, and Window
2024-09-09
独立思考最难得,赞赏支持是美德!(微信扫描下图)