要在Linux系統(tǒng)中實(shí)現(xiàn)任務(wù)的并行執(zhí)行,可以結(jié)合使用Systemd和Crontab。下面是使用這兩個(gè)工具的步驟:
編寫任務(wù)腳本,例如 task1.sh
和 task2.sh
。
在 /etc/systemd/system/
目錄下創(chuàng)建一個(gè)新的服務(wù)單元配置文件,例如 parallel-tasks.service
。
[Unit]
Description=Parallel Tasks
[Service]
ExecStart=/bin/bash /path/to/task1.sh &
ExecStart=/bin/bash /path/to/task2.sh &
[Install]
WantedBy=multi-user.target
執(zhí)行以下命令以啟用和啟動(dòng) Systemd 服務(wù):
sudo systemctl enable parallel-tasks
sudo systemctl start parallel-tasks
執(zhí)行以下命令以編輯 Crontab 文件:
crontab -e
在 Crontab 文件中添加以下內(nèi)容,以在每分鐘執(zhí)行 Systemd 任務(wù):
* * * * * /usr/bin/systemctl start parallel-tasks
現(xiàn)在,你的任務(wù)將在 Linux 系統(tǒng)中并行執(zhí)行。Crontab 每分鐘執(zhí)行 Systemd 任務(wù),Systemd 啟動(dòng)并行執(zhí)行的任務(wù)腳本。請注意,你需要替換示例中的文件名和路徑為你實(shí)際使用的文件名和路徑。