如何在开机时自动运行多个命令行程序
把程序设置成系统服务,程序就会在开机时自动启动,但是有的程序原生不支持设置成系统服务,这时应该怎么办?
用 Windows Terminal 自动运行多个命令行程序
海云青飞 发现借助 Windows Terminal 可以方便地实现开机时自动运行多个命令行程序
首先自然是安装 Windows Terminal
Ctrl + ,
打开Settings
Default terminal application
: Windows TerminalLaunch on machine startup
: onWhen terminal starts
: Open window from a previous sessionNew instance behavior
: Attach to the most recently used windowSave
然后在 Terminal 的不同 tab 中运行不同的程序,并且在电脑关机时不要关闭这些 tab。关机时可用 ctrl + shift + w
关闭不必要的 tab,再用 Alt + F4
关闭 Terminal
2024-11-16 更新
Windows starup 目录结合 Windows Terminal 在开机时自动运行多个命令行程序
用上面的方法,电脑关机前要确保 Windows terminal 的相应 tab 在运行相关命令行程序,这会带来心智负担,下面就来改进一下
-
创建一个脚本文件 如
runApp.sh
,内容类似下面:#!/bin/bash # 延时秒数 sleep 10s # 在已经存在的 Windows Terminal 创建新 tab,并运行一个 bash 脚本,文件名是 scriptA wt -w 0 new-tab bash -c scriptA sleep 5s wt -w 0 new-tab bash -c scriptB
-
为
runApp.sh
创建 shortcut 快捷方式runApp
-
把
runApp
放到 startup 目录打开 startup 目录的方法,按
Windows + R
键,输入shell:startup
-
Windows Terminal 设置
When terminal starts
: Open a tan with the defualt probilen-
Launch on machine startup
: off
2024-11-26 更新
用 bash 文件运行多个命令行程序
今天,2024-09-09 海云青飞 需要从命令行调用两个程序,一个程序有输出信息
我可以开启两个命令行窗口,分别调用这两个程序,但这显然不是好方法
我并没有时间去详细研究计算机技术,这没有关系,因为现在有了 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
独立思考最难得,赞赏支持是美德!(微信扫描下图)